java人机猜拳分析_Java人机猜拳思路及实现

Java人机猜拳游戏编写思路及实现

创建四个类:

机器人类

玩家类

游戏类

运行类

一、机器人类:

1.首先创建两个成员变量,分别作为机器人的名字与积分:

String name;

int jifen;1

2

2.然后创建方法“pcname”用于定义机器人的名字:

写个switch循环来判断如果你按1234的话谁来与你对战

3.然后再创建一个方法“chuquan”来定义机器人出什么,石头还是剪刀。。:

所以写个1-3的随机数

int suiji=((int)(Math.random()*3)+1);1

4.最后switch判断,1的话是石头,2剪刀,3布

机器人完整代码:

import java.util.Scanner;

public class pc { String name; int jifen;

Scanner input=new Scanner(System.in); public void pcname(){ int pcjuese=input.nextInt(); switch (pcjuese) { case 1: name="张飞"; System.out.println("你选择了"+name+"对战"); break; case 2: name="赵云"; System.out.println("你选择了"+name+"对战"); break; case 3: name="貂蝉"; System.out.println("你选择了"+name+"对战"); break; default: System.out.println("还没这个人"); } } public int chuquan(){ int suiji=((int)(Math.random()*3)+1); switch (suiji){ case 1: System.out.println(name+"出拳:石头"); break; case 2: System.out.println(name+"出拳:剪刀"); break; case 3: System.out.println(name+"出拳:布"); break; default: } return suiji; }

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

二、玩家类:

1.和机器人一样,定义两个成员变量,用于作为名字和积分:

String name="1";

int jifen=0;1

2

2.创建一个“renname”方法,用于定义玩家的昵称

3.创建一个”chuquan“方法,用于定义玩家出拳

然后switch进行判断,按1出石头,按2出剪刀,按3出布

玩家完整代码:

import java.util.Scanner;

public class ren

{ String name="1"; int jifen=0; Scanner input=new Scanner(System.in); public void renname(){ System.out.println("起一个昵称吧:"); name=input.next(); System.out.println("你的昵称为:"+name); } public int chuquan(){ System.out.println("出拳:1.石头 2.剪刀 3.布"); int show=input.nextInt(); switch (show){ case 1: System.out.println(name+" 出石头"); break; case 2: System.out.println(name+" 出剪刀"); break; case 3: System.out.println(name+" 出布"); break; default : System.out.println("你准备出个大炮呢?"); } return show; }

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

三、游戏类

1.定义两个成员变量和new两个对象:

int count; //进行了几局游戏

int pingju; //平局场次

ren Ren =new ren();

pc computer =new pc();1

2

3

4

2.创建“game”方法,用来调用机器人类和玩家类,以及实现游戏功能

3.首先调用Ren.name()来用于让玩家创建昵称

3.1.然后调用computer.name()来用于让玩家选择和谁对战

4.一切ok,开始进行功能实现:

首先创建两个变量,一个是机器人出拳,一个是玩家出拳

int pcquan; //pc出拳

int personquan; //人出拳1

2

y/n开始游戏和退出游戏

开始进行游戏功能实现编写

5.循环的时候把机器人和玩家出拳的方法赋值给上面两个定义出拳的变量

6.功能ok后创建一个方法,用于定义计分板,这个就很简单了,直接附上游戏类完整代码:

import java.util.Scanner;

public class w { int count; //进行了几局游戏 int pingju; //平局场次 ren Ren =new ren(); pc computer =new pc(); Scanner input=new Scanner(System.in);

public void game(){ System.out.println("规则:1.石头 2.剪刀 3.布"); Ren.renname(); System.out.print("请选择对方角色(1.张飞 2.赵云 3.貂蝉): "); // 选择对方角色 computer.pcname(); System.out.println("要开始嘛?(y/n)"); String yn=input.next(); int pcquan; //pc出拳 int personquan; //人出拳 while (yn.equalsIgnoreCase("y")){ personquan=Ren.chuquan(); //人出拳 pcquan=computer.chuquan(); //pc出拳 if((pcquan==1&personquan==1)|(pcquan==2&personquan==2)|(pcquan==3&personquan==3)){ System.out.println("平局"); pingju++; }else if((pcquan==1&personquan==3)|(pcquan==2&personquan==1)|(pcquan==3&personquan==2)){ System.out.println(Ren.name+"赢了"); Ren.jifen++; }else { System.out.println(computer.name+"赢了"); computer.jifen++; } count++; //表示进行了一局游戏 System.out.println("开始下一轮嘛?(y/n)"); yn=input.next(); if (yn.equalsIgnoreCase("n")){ System.out.println("游戏结束"); result(); } }

} //比赛结果 public void result(){ System.out.println("-------------------------------"); System.out.println(Ren.name+" VS "+computer.name); System.out.println("对战次数:"+count); System.out.println("平局"+pingju+"场"); System.out.println(Ren.name+"赢了"+Ren.jifen+"场"); System.out.println(computer.name+"赢了"+computer.jifen+"场"); if(Ren.jifen> computer.jifen){ System.out.println(Ren.name+"赢了"); }else if (computer.jifen> Ren.jifen){ System.out.println(computer.name+"赢了"); }else { System.out.println("平局"); } }

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

三、运行类

这个就不多说了,直接上代码

public class test

{ public static void main(String[] args) {

w game=new w();

ren ren=new ren(); game.game(); //game.result(); }

}1

2

3

4

5

6

7

8

9

10

11

文章来源: blog.csdn.net,作者:月菱歌,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_34719518/article/details/111085184

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值