当前位置:网站首页>猜拳游戏专题训练

猜拳游戏专题训练

2022-06-27 19:20:00 continueLR

 

目录

今天的任务是通过控制台方式实现一个人机对战的猜拳游戏,用户通过输入(1.剪刀 2.石头 3.布),机器随机生成(1.剪刀 2.石头 3.布),胜者积分,n 局以后通过积分的多少判定胜负。1. 定义机器类,以及拳头属性(此属性只有三个值:剪刀,石头,布。这里的值可以使用数值代替)2. 定义生成随机数的方法(让机器生成剪刀,石头,布的值),赋值给第一步的拳头属性3. 定义测试类,获取用户输入的剪头石头布的值,和随机生成的值比较4. 测试中,定义变量保存胜者积分

 踩坑原因:空指针异常


今天的任务是通过控制台方式实现一个人机对战的猜拳游戏,用户通过输入(1.剪刀 2.石头 3.布),机器随机生成(1.剪刀 2.石头 3.布),胜者积分,n 局以后通过积分的多少判定胜负。
1. 定义机器类,以及拳头属性(此属性只有三个值:剪刀,石头,布。这里的值可以使用数值代替)
2. 定义生成随机数的方法(让机器生成剪刀,石头,布的值),赋值给第一步的拳头属性
3. 定义测试类,获取用户输入的剪头石头布的值,和随机生成的值比较
4. 测试中,定义变量保存胜者积分

public static void main(String[] args) {
        PerComGame game = new PerComGame();
        game.menu();
    }
}
class Person{
    private int score;//人获得的积分
    public Person(){
    }
    public Person(int score){
        this.score = score;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int inputShow(){//人出拳的方法
        Scanner input = new Scanner(System.in);
        while (true){
            System.out.println("请输入您猜拳的结果:(1.剪刀 2.石头 3.布)");
            int num = input.nextInt();
            switch (num){
                case 1:
                    System.out.println("用户出拳:剪刀");
                    break;
                case 2:
                    System.out.println("用户出拳:石头");
                    break;
                case 3:
                    System.out.println("用户出拳:布");
                    break;
                default:
                    System.out.println("您输入有误,游戏重新开始!");
                    continue;
            }
            return num;
        }
    }
}
class Computer{
    private int score;//电脑得的积分

    public Computer() {
    }

    public Computer(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
    public int inputShow(){
        int num = (int)(Math.random()*3+1);
        switch(num){
            case 1:
                System.out.println("机器出拳:剪刀");
                break;
            case 2:
                System.out.println("机器出拳:石头");
                break;
            case 3:
                System.out.println("机器出拳:布");
                break;
        }
        return num;
    }
}
 class PerComGame{
    private int js;
    private Person person;
    private Computer computer;
    public PerComGame(){
        this.js = 0;
        this.person = new Person();
        this.computer = new Computer();
    }
    public PerComGame(int js,Person person,Computer computer){
        this.js = 0;
        this.person = new Person();
        this.computer = new Computer();
    }

     public int getJs() {
         return js;
     }

     public void setJs(int play) {
         this.js = js;
     }

     public Person getPerson() {
         return person;
     }

     public void setPerson(Person person) {
         this.person = person;
     }
     public Computer getComputer() {
         return computer;
     }
     public void setComputer(Computer computer) {
         this.computer = computer;
     }
     //输出菜单方法
     public void menu(){
         Scanner input = new Scanner(System.in);
         System.out.println("------欢迎开始猜拳游戏------");
         System.out.println("开始游戏,用户先开始");
         loop:while(true){
             startGame();
             js++;
             System.out.println("是否继续游戏(y/n)");
             String c = input.next();
             if(c.equals ("n") || c.equals("否")){//结束游戏
                 break loop;
             }
         }
         System.out.println("本次游戏已结束!");
         System.out.println("最终对战结果如下:");
         System.out.println("用户\tVS\t机器");
         System.out.println("总共游戏局数:" + js);
         System.out.println("用户的积分:" + person.getScore());
         System.out.println("机器的积分:" + computer.getScore());
         System.out.println("由以上积分得最终赢家为  " + winner());
     }
     //游戏运行方法
     public void startGame(){
         int pNum = person.inputShow();
         int cNum = computer.inputShow();
         //比较结果
         if((pNum == 1 && cNum == 3) || (pNum == 2 && cNum == 1) || (pNum == 3 && cNum == 2)){//用户赢
             System.out.println("用户赢!");
             person.setScore(person.getScore() + 1);
         }else if(pNum == cNum){//平局
             System.out.println("二者平局!");
         }else{
             System.out.println("机器赢!");
             computer.setScore(computer.getScore() + 1);
         }
     }
     //判断赢家方法
     public String winner(){
         if(person.getScore() > computer.getScore()){
             return "用户";
         }else if (person.getScore() == computer.getScore()){
             return "用户和机器打平";
         }else{
             return "机器赢";
         }
     }

 踩坑原因:空指针异常

 

 在创建person对象时,并没有用new对象的形式,而是写了一个空的无参构造方法。

修改结果

 

原网站

版权声明
本文为[continueLR]所创,转载请带上原文链接,感谢
https://blog.csdn.net/continueLR/article/details/118275560