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

編程怎麼做猜拳游戲

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

閱讀全文

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

熱點內容
app充值請聯系itunes 瀏覽:678
矢量app和cdr哪個好 瀏覽:85
系統文件壞了如何修復 瀏覽:20
鍵盤系統文件誤刪 瀏覽:738
白金英雄壇所有版本 瀏覽:842
ps文件轉hsj 瀏覽:382
哪個網站電影 瀏覽:490
ps4游戲文件格式名稱 瀏覽:290
caxa教程2007 瀏覽:832
新點是什麼小說網站 瀏覽:753
魔獸世界冰封王座3版本轉換器 瀏覽:418
蘋果3dtouch軟體 瀏覽:979
qq視頻在哪個文件夾裡面 瀏覽:740
請帖製作網站java源碼 瀏覽:257
自己的網站怎麼做兼職 瀏覽:608
醫院基礎數據是什麼 瀏覽:744
為什麼數據線沒有typec介面 瀏覽:39
蘋果手機文件管家 瀏覽:187
qq頭像男生水裡 瀏覽:633
聯想電腦初始密碼 瀏覽:517

友情鏈接