人机猜拳

Posted 倾城月光﹋淡如水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了人机猜拳相关的知识,希望对你有一定的参考价值。

1.选取对战角色

  选择猜拳的对手

2.猜拳

  开始对战,用户选择出拳,与对手进行比较,提示胜负信息

3.记录分数

  每局猜拳结束,获胜一方加1分(平局都不加分),结束游戏时,显示对战次数及对战最终结果

创建类方法

 1 import java.util.Scanner;
 2 
 3 public class Cai {
 4     int se1;
 5     int se2;
 6     String choice1;
 7     String choice2;
 8     String choiceMan;
 9     boolean a1;
10     boolean a2;
11     int random = (int) (Math.random() * 3);
12 
13     public void A() {
14         Scanner input = new Scanner(System.in);
15         do {
16             System.out.print("\n请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
17             se1 = input.nextInt();
18             a1 = false;
19             switch (se1) {
20             case 1:
21                 choice1 = "剪子";
22                 break;
23             case 2:
24                 choice1 = "石头";
25                 break;
26             case 3:
27                 choice1 = "布";
28                 break;
29             default:
30                 System.out.println("请做出正确选择!");
31                 a1 = true;
32                 break;
33             }
34         } while (a1 == true);
35     }
36 
37     public void B() {
38         if (random == 0.0) {
39             random = 3;
40         }
41         switch (random) {
42         case 1:
43             choice2 = "剪子";
44             break;
45         case 2:
46             choice2 = "石头";
47             break;
48         case 3:
49             choice2 = "布";
50             break;
51         }
52     }
53 
54     public void C() {
55         Scanner input = new Scanner(System.in);
56         do {
57             System.out.print("请选择对方的角色(1.刘备 2.孙权 3.曹操):");
58             se2 = input.nextInt();
59             a2 = false;
60             switch (se2) {
61             case 1:
62                 choiceMan = "刘备";
63                 break;
64             case 2:
65                 choiceMan = "孙权";
66                 break;
67             case 3:
68                 choiceMan = "曹操";
69                 break;
70             default:
71                 System.out.println("请做出正确选择!");
72                 a2 = true;
73                 break;
74             }
75         } while (a2 == true);
76     }
77 
78     public void D() {
79         System.out.println("--------------------欢迎进入游戏世界------------------");
80         System.out.println("\n\n");
81         System.out.println("\t\t***************");
82         System.out.println("\t\t**c猜拳,开始**");
83         System.out.println("\t\t***************");
84         System.out.println("\n\n");
85         System.out.println("出拳规则:1.剪刀\t2.石头\t3.布");
86     }
87 }

使用main方法执行

 1 import java.util.Scanner;
 2 
 3 public class Cai01 {
 4     public static void main(String[] args) {
 5         Cai cen = new Cai();
 6         Scanner input = new Scanner(System.in);
 7         String choice = null;
 8         int bot = 0;
 9         cen.D();
10         cen.C();
11         System.out.println("你选择了" + cen.choiceMan + "对战\n");
12         System.out.print("要开始吗?(开始按y)");
13         do {
14             choice = input.next();
15             if (choice.equals("y")) {
16                 cen.A();
17                 System.out.println("你出拳:" + cen.choice1);
18                 cen.B();
19                 System.out.println(cen.choiceMan + "出拳:" + cen.choice2);
20                 System.out.print("结果说:");
21                 if (cen.se1 == cen.random) {
22                     System.out.println("和局,真衰!嘿嘿,等着瞧吧!\n");
23                 } else if ((cen.se1 == 1 && cen.random == 3)
24                         || (cen.se1 == 2 && cen.random == 1)
25                         || (cen.se1 == 3 && cen.random == 2)) {
26                     System.out.println("恭喜,你赢了!\n");
27                 } else {
28                     System.out.println("^_^,你输了,真笨!\n");
29                 }
30                 System.out.print("是否开始下一轮?(开始按y):");
31                 bot++;
32             } else {
33                 break;
34             }
35         } while (choice.equals("y"));
36         System.out.println("-------------------------");
37         System.out.println(cen.choiceMan + " VS " + "匿名");
38         System.out.println("对战次数:" + bot);
39         System.out.println("结果:呵呵,笨笨,下次加油啊!");
40         System.out.println("-------------------------");
41     }
42 }

 

以上是关于人机猜拳的主要内容,如果未能解决你的问题,请参考以下文章

shell案例分享之猜拳人机大战!

1.人机猜拳

java人机猜拳游戏002user

人机猜拳

人机猜拳

《人机猜拳》