導航:首頁 > 編程知識 > 編程怎麼做猜拳游戲

編程怎麼做猜拳游戲

發布時間:2023-07-18 04:40:37

㈠ 用java做一個剪刀,石頭,布的人機猜拳游戲。

編寫這個小游戲 我們需要幾個類

1、第一個 Person 類

import java.util.Scanner;

/**

* @right 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Person

* @description 用戶類 用來計算用戶輸入

*/

public class Person {

public static final Person me = new Person();

private int n = 0;

/**

* @right 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description 用戶輸入的 指令

* @return

*/

public int input() {

System.out.println("請輸入:石頭,剪刀,布 輸入:@退出 退出系統");

@SuppressWarnings("resource")

Scanner scanner = new Scanner(System.in);

String s = scanner.next();

// s 裡面存著 用戶輸入的 指令 切記這里不要使用 s.equals() 而是寫 "指令".equals() 這么寫 是為了避免空指針

if ("石頭".equals(s)) {

n = 1;

} else if ("剪刀".equals(s)) {

n = 2;

} else if ("布".equals(s)) {

n = 3;

} else if ("@退出".equals(s)) {

System.out.print("系統退出了");

System.exit(0);

}

return n;

}

}

2、Computer 類

/**

* @right 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Computer

* @description 游戲中電腦類 用來產生隨機數

*/

public class Computer {

public static final Computer me = new Computer();

/**

* @right 2018 sugarsLab.com All rights reserved.

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description TODO

* @return {int} 返回值為int 類型

*/

public int random() {return (int) (Math.random() * 3 + 1);}

}

3、Game類

/**

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @ClassName Game

* @description 游戲類 用來計算游戲結果

*/

public class Game {

/**

* @author jingfei.wu

* @date 2018年11月16日

* @version 1.0

* @description 返回 人機交互結果

* @param n

* {int} 用戶輸入 的標識 石頭 為 1 剪刀 為 2 布 為 3

* @param m

* {int} 電腦產生的隨機數 石頭 為 1 剪刀 為 2 布 為 3

*/

public void result(int n, Integer m) {

String res = "";

if (m.intValue() == 1)

res = "石頭";

else if (m.intValue() == 2)

res = "剪刀";

else

res = "布";

if (n == m) {

System.out.println("平了 computer出" + res);

} else {

if (m == 1) {

if (n == 2)

System.out.println("你輸了 computer出 " + res);

else if (n == 3)

System.out.println("你贏了 computer出 " + res);

} else if (m == 2) {

if (n == 1)

System.out.println("你贏了 computer出 " + res);

else if (n == 3)

System.out.println("你輸了 computer出 " + res);

} else if (m == 3) {

if (n == 1)

System.out.println("你輸了 computer出 " + res);

else if (n == 2)

System.out.println("你贏了 computer出 " + res);

}

}

}

public static void main(String[] args) {

while (true) {

Game gamer = new Game();

gamer.result(Person.me.input(), Computer.me.random());

}

}

}

如下是程序運行截圖

㈡ C語言猜拳游戲代碼 幫忙 改改 能運行就行 五局三勝制是關鍵

#include <stdio.h>
#include <math.h>
#include <time.h>
int main()
{
int a,b,i,s,s1;
i=1;
s=0;
s1=0;
srand((unsigned)time(NULL));
printf("猜拳游戲\n0石頭1剪刀2布\n***********\n");
printf("規則:共有5局");
printf("請\n出\n拳\n..\n");
do
{
scanf("%d",&b);
a=rand()%3;
if(a==0&&b==0)
{printf("電腦出的是拳頭,您出的是拳頭,平\n");s=s+0;}
else if(a==0&&b==1)
printf("電腦出的是拳頭,您出的是剪刀,負\n");
else if(a==0&&b==2)
{printf("電腦出的是拳頭,您出的是布,贏\n");s=s+1;}
else if(a==1&&b==0)
{printf("電腦出的是剪刀,您出的是石頭,贏\n");s=s+1;}
else if(a==1&&b==1)
{printf("電腦出的是剪刀,您出的是剪刀,平\n");s=s+0;}
else if(a==1&&b==2)
printf("電腦出的是剪刀,您出的是布,負\n");
else if(a==2&&b==0)
printf("電腦出的是布,您出的是石頭,負\n");
else if(a==2&&b==1)
{printf("電腦出的是布,您出的是剪刀,贏\n");s=s+1;}
else if(a==2&&b==2)
{printf("電腦出的是布,您出的是布,平\n");s=s+0;}
else
{printf("存在錯誤\n");s=s+0;}
}while(i++>5);
printf("\n您的得分為:%d分\n",s);
if(s>2)
printf("您贏了");
else
printf("您輸了");
return 0;
}
估計這樣就達到你的目的了

閱讀全文

與編程怎麼做猜拳游戲相關的資料

熱點內容
數控編程數學應該是什麼水平 瀏覽:862
杭州賣廠房去什麼網站 瀏覽:292
cpm1h用什麼編程軟體 瀏覽:419
js文件不起作用 瀏覽:716
word怎麼插入繪圖畫布 瀏覽:279
讀取sql文件 瀏覽:512
pdfjs列印不全 瀏覽:392
編程要建什麼文件夾 瀏覽:422
vb獲取文件夾下所有文件名 瀏覽:799
研究生大數據學費多少錢一年 瀏覽:810
如何看懂車床電氣數據牌 瀏覽:882
什麼網站能查招聘公路監理 瀏覽:461
微信走步計數器怎麼用 瀏覽:349
怎麼刪除qq接收的文件 瀏覽:225
cad文件怎麼不顯示集中標注 瀏覽:77
網路設計規劃書 瀏覽:695
穩安特app在哪裡下載 瀏覽:895
安卓版有道詞典63 瀏覽:38
企業網站建設有哪些基礎 瀏覽:682
讀取數據要哪個文件夾 瀏覽:461

友情鏈接