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

編程怎麼做猜拳游戲

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

閱讀全文

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

熱點內容
王者什麼時候版本更新 瀏覽:123
win10桌面圖標太小亂跑 瀏覽:73
寫公文時保密數據如何反映 瀏覽:767
arch設置ip配置文件 瀏覽:641
燒杯APP中的氣體和液體在哪裡 瀏覽:3
找同行工作在哪個網站 瀏覽:254
linuxdirfd 瀏覽:290
電腦列印一張文件多少錢 瀏覽:212
斗戰神遊戲修復工具 瀏覽:736
系統更新網路無法鏈接不上電腦沒反應 瀏覽:851
什麼是二層網路架構 瀏覽:402
中醫看書好的app 瀏覽:516
win10紙牌變成英文 瀏覽:135
源碼文件圖片路徑怎麼形成的 瀏覽:432
ai變數寬度配置文件點不了 瀏覽:460
強制安裝app去哪裡舉報 瀏覽:719
微信登錄數據保存在哪裡 瀏覽:321
pic編程哪裡下載 瀏覽:549
蘋果ipad官方壁紙 瀏覽:154
華創網路信號怎麼樣 瀏覽:495

友情鏈接