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

編程怎麼做猜拳游戲

發布時間: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;
}
估計這樣就達到你的目的了

閱讀全文

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

熱點內容
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65
word怎麼插入羅馬數字i 瀏覽:315
哪個網站可以找到法人代表 瀏覽:106
蘋果5s日版a1453支持什麼網路 瀏覽:297
微信打開文件如何設置 瀏覽:218
漫畫書app中非可視組件是什麼 瀏覽:3
d盤文件隱藏怎麼恢復 瀏覽:287
5s怎麼設置聯通4g網路 瀏覽:15
壓縮後文件會少多少 瀏覽:859
資料庫事物實現原理 瀏覽:944
國務院app怎麼查詢疫情情況 瀏覽:105

友情鏈接