猜拳游戏案例

来源:互联网 发布:自学英语口语的软件 编辑:程序博客网 时间:2024/05/29 04:09

猜拳是由石头、剪刀、布组成,可以选择你的对手是谁,这里设定了3个对手(总裁、副总、总监),然后选择你的出拳,通过使用数字1、2、3来分别代表着3种状态。然后我们还需要加上计分的功能。这里我们建立4个类,人、机、主方法、测试类。

当然啦,我们都知道人类和机类的基本方法都差不多,因为我们要获取到键盘的输入内容,所以我们要使用这个Scanner ,这个在util包下面,我们直接导入就可以了。这里直接贴出的代码:

import java.util.Scanner;public class Person{private String name;private int score;public Person(){name="sdksdk0";score=0;}public Person(String n){name=n;score=0;}public int showFist(){Scanner input=new Scanner(System.in);System.out.println("\n请出拳:1 剪刀 2 石头  3  布(输入相应数字):");int show=input.nextInt();switch(show){case 1:System.out.println("你出拳:剪刀");break;case 2:System.out.println("你出拳:石头");break;case 3:System.out.println("你出拳:布");break;}return show;}public String getName(){return name;}public void setName(String name){this.name=name;}public int getScore(){return score;}public void setScore(int score){this.score=score;}}
机类的话,自然需要一个随机数了,这样才可以随机比较。
import java.util.Random;public class Android{private String name;private int score;public Android(String n){name=n;score=0;}public Android(){name="Android";score=0;}public int showFist(){Random r=new Random();int show=r.nextInt(3)+1;switch(show){case 1:System.out.println("你出拳:剪刀");break;case 2:System.out.println("你出拳:石头");break;case 3:System.out.println("你出拳:布");break;}return show;}public String getName(){return name;}public void setName(String name){this.name=name;}public int getScore(){return score;}public void setScore(int score){this.score=score;}}


下面最重要的是我们的主方法类了。
我们需要来定义人、机、计分方法。

    private Person person;
private Android android;
private int count;

功能初始化
public void initial(){
person=new Person();
android=new Android();
count=0;
}

定义显示结果:

if(result==1){System.out.println("结果:平分秋色");} else if(result==2){System.out.println("结果:"+person.getName()+"你赢了");} else if(result==3){System.out.println("结果:"+person.getName()+"你输了"+android.getName()+"赢了");}
在这几种情况下是玩家赢了:
1 剪刀 2 石头  3  布

用户1对机器3,用户2对机器1,用户3对机器2.

如果用户输入的和机器产生的一样就是平局了,其余情况就是机器赢了。

至于计算分数的话就这样就可以了android.setScore(android.getScore()+1);

完整代码如下:

import java.util.Scanner;public class Referee{private Person person;private Android android;private int count;//初始化功能public void initial(){person=new Person();android=new Android();count=0;}//计算 总分public int calcResult(){if(person.getScore()==android.getScore()){return 1;} else if(person.getScore()>android.getScore()){return 2;} else{return 3;}}//显示总结果public void showResult(){System.out.println("======================");System.out.println(person.getName()+"VS"+android.getName());System.out.println("总对战次数:"+count);int result=calcResult();if(result==1){System.out.println("结果:平分秋色");} else if(result==2){System.out.println("结果:"+person.getName()+"你赢了");} else if(result==3){System.out.println("结果:"+person.getName()+"你输了"+android.getName()+"赢了");}System.out.println("======================");}public void startGame(){System.out.println("======================");System.out.println("\n\t************************");System.out.println("请选择你的对手  1 总裁  2  副总  3  总监");Scanner input=new Scanner(System.in);int role=input.nextInt();switch(role){case 1:android.setName("总裁");break;case 2:android.setName("副总");break;case 3:android.setName("总监");break;}System.out.println("\n要开始吗?[Y/N]");String con=input.next();int perFist;int androidFist;while(con.equals("Y")){//perFist = person.showFist();perFist=person.showFist();androidFist=android.showFist();if(perFist==androidFist){System.out.println("结果:平局");} else if((perFist==1&&androidFist==3)||(perFist==2&&androidFist==1)||(perFist==3&&androidFist==2)){System.out.println("结果:"+person.getName()+"赢了!");person.setScore(person.getScore()+1);} else{System.out.println("结果:"+person.getName()+"输了");android.setScore(android.getScore()+1);}count++;System.out.print("\n是否开始下一轮(Y/N)");con=input.next();}showResult();}}
最后,我们只需要一个测试类就可以了

public class StartGame{public static void main(String[] args){Referee r=new Referee();r.initial();r.startGame();}}

当然咯,如果你想要把测试类都写在一起也是可以正常编译出来的,不过不建议这样做,这样的话对代码的重构性就不好了。使用命令javac StartGame.java进行编译,然后 java StartGame 运行。





2 0