美文网首页
Java:猜拳游戏

Java:猜拳游戏

作者: 任任任任师艳 | 来源:发表于2017-09-01 20:41 被阅读0次

package game;
import java.util.Scanner;

public class Person {
    String name;
    int score;
    
    public Person() {
        
    }
    public Person(int score) {
        this.score = score;
    }
    
    // 1. 石头  2. 剪刀  3. 布
    int play() {
        System.out.println("请出拳:(1石头2剪刀3布)");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        switch (num) {
        case 1: {
            System.out.println(this.name + "出拳:石头") ;
            break;
        }
        case 2: {
            System.out.println(this.name + "出拳:剪刀");
            break;
        }
        default:
            System.out.println(this.name + "出拳:布");
            break;
        }
        return num;
    }   
}

package game;
import java.util.Random;

public class Computer {
    String name;
    int score;
    
    // 构造方法
      public Computer() { // 默认构造方法
        
     }
      public Computer (int score) {
        this.score = score;
    }
      
    // 出拳
    int play() {
        Random rand = new Random();
        // 1. 石头 2. 剪刀 3.布
        int num = rand.nextInt(3)+1;
        switch (num) {
        case 1: {
            System.out.println(this.name + "出拳:石头") ;
            break;
        }
        case 2: {
            System.out.println(this.name + "出拳:剪刀");
            break;
        }
        default:
            System.out.println(this.name + "出拳:布");
            break;
        }
        return num;
    }
}

package game;
import java.util.Scanner;

public class Memu {
    Computer computer;
    Person p;
    
    // 构造方法
    public Memu() {
        this.init();
    }
    // 初始化游戏
    void init() {
        // 创建机器对象
        this.computer = new Computer(0);
        System.out.println("请输入电脑的名字:");
        Scanner sc = new Scanner(System.in);
        this.computer.name = sc.next();
        
        // 创建一个人对象
        this.p = new Person(0);
        System.out.println("请输入你的名字:");
        this.p.name = sc.next();
    }
    // 开始游戏
    void start() {
        // 人先出拳
        int pNum = this.p.play();
        // 机器出拳
        int cNum = this.computer.play();
        // 计算分数
        this.calculate(pNum, cNum);
        System.out.println("游戏是否结束(exit)");
        Scanner sc = new Scanner(System.in);
        if ("exit".equals(sc.next())) {
            // 展示结果
            this.show();
            return;
        } else {
            start();
        }
    }
    // 计算分数  num : 人出拳的值   num1 机器出拳的值
    void calculate( int num, int num1) {
        if (num == num1) { // 平局
            System.out.println("平局");
            return;
        }
        if ((num == 1 && num1 == 2) || (num == 2 && num1 == 3) ||
                (num==3&&num1==1)) { // 人胜利
            System.out.println("人胜利");
            this.p.score++;
        } else { // 机器胜利
            System.out.println("机器胜利");
            this.computer.score++;
        }
    }
   // 展示结果
   void show() {
       System.out.println(this.p.name + "得分:" + this.p.score);
       System.out.println(this.computer.name + "得分:" +
       this.computer.score);
   } 
}

package game;

public class Test {
    public static void main(String[] args) {
        
        Memu memu = new Memu();
        memu.start();
    
    }

}

相关文章

  • Java:猜拳游戏

    package game;import java.util.Scanner; package game;impor...

  • Java 猜拳游戏 基础版本

  • 2018-07-27

    这几天一直在学习java,用java做了一个小游戏,记录一下自己的成长,嘿嘿 下面是游戏简介: 人机猜拳游戏 欢迎...

  • 猜拳游戏

    我妈突然就走不动了,她的双腿无力,站也站不起来,手掐上大腿一点反应也没有。 我望着对面的山,寻思着哪座山头草多些,...

  • 猜拳游戏

    我记得小时候,有一天爸爸说单位要出去旅游,每位员工可以带一个孩子。那时候出去旅游可是新鲜事物,极富吸引力,自然我和...

  • 猜拳游戏-学Java从游戏开始2

    实践是最好的老师,它能教会我们许多光“看”书而看不到的知识。 内容: “石头、剪刀、布”是一种常见的猜拳游戏。游戏...

  • 猜拳小游戏

    import random a = random.randint(1,100) i = 0 for i in ra...

  • 猜拳小游戏

    门开了,孔医生和岳同学回到家中,我一如昨天微笑欢迎他们回家。 我发现岳同学本来应该是很亮的眼睛,像今天的天气一样被...

  • 猜拳小游戏

    猜拳游戏 github:https://github.com/lvzhihao100/RoitGuess简书:ht...

  • 用java开发经典猜拳游戏,绝地翻盘堪称逆天!

    首先给大家普及一下面向对象(此次猜拳游戏开发需要用到面向对象),Java面向对象是Java的一个核心,也是初学者的...

网友评论

      本文标题:Java:猜拳游戏

      本文链接:https://www.haomeiwen.com/subject/uydhjxtx.html