怎么在java中利用面向对象编写一个人机猜拳小游戏
创始人
2024-12-27 10:21:27
0次

Computer.java类

package com.whf.guess.game;
/** 
 * @author :辰
 * @version 
 * 创建时间:2017年2月24日 上午8:38:29 
 * 
 */
public class Computer {

  String name = "电脑";//名字
  int score = 0;//积分

  public int showFist() {
    //产生随机数

    int show = (int) ((Math.random()*10)%3+1); // 产生随机数,表示电脑出拳

    //输出出拳结果并返回
    switch (show) {
    case 1:
      System.out.println(name+"你出拳:剪刀");
      break;
    case 2:
      System.out.println(name+"你出拳:石头");
      break;
    case 3:
      System.out.println(name+"你出拳:布");
      break;
    }

    return show;
  }



}

Person.java 类

package com.whf.guess.game;

import java.util.Scanner;

/** 
 * @author :辰
 * @version
 * 创建时间:2017年2月24日 上午8:45:41 
 * 
 */
public class Person {

  String name = "匿名"; //名字
  int 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(name+"你出拳:剪刀");
      break;
    case 2:
      System.out.println(name+"你出拳:石头");
      break;
    case 3:
      System.out.println(name+"你出拳:布");
      break;
    }
    return show;
  }

}

Game.java类。包含游戏的比赛核心代码

package com.whf.guess.game;

import java.util.Scanner;

/** 
 * @author :辰
 * @version
 * 创建时间:2017年2月24日 上午8:37:06 
 * 
 */
public class Game {

  Person person ; //甲方

  Computer computer;// 乙方

  int count;// 对战次数

  public void inital(){
    person = new Person();
    computer = new Computer();
    count = 0;
  }
  //开始游戏
  public void startGame(){
    System.err.println("-------------欢迎进入猜拳游戏---------------");
    System.out.println();
    System.out.println("******************************************");
    System.out.println("——————出拳规则:1.剪刀  2.石头  3.布——————————");
    System.out.println("-------------》》》猜拳开始《《《------------");
    System.err.println("-------------------------------------------");
    Scanner input = new Scanner(System.in);
    String exit="n";//退出系统
    do {
      inital();//初始化
      //选择对手
      System.out.println("选择你的对手1.马云   2.马化腾   3.马大姐  ");
      int role = input.nextInt();
      if (role ==1) {
        computer.name="马云";
      }else if (role == 2) {
        computer.name="马化腾";
      }else if (role == 3) {
        computer.name="马大姐";
      }

      //游戏玩家输入姓名
      System.out.println("请输入你的姓名:");
      person.name=input.next();
      System.out.println(person.name+"  VS  "+computer.name+"对战\n");
      //开始游戏
      System.out.println("要开始吗?(y/n)");
      //开始一局游戏
      String start = input.next();
      int perFist;//游戏用户出拳
      int comFist;//计算机出拳
      while (start.equals("y")) {
        //出拳
        perFist = person.showFist();
        comFist = computer.showFist();
        //判断胜负
        if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) {
          System.err.println("结果:和局,真衰!\n");//平局
        }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) {
          System.err.println("结果:你赢了,真帅!\n");//胜利
          person.score++;
        }else {
          System.err.println("结果是:^_^,你输了,下载再战!\n");//计算机赢
          computer.score++;
        }
        count++;
        System.out.println("是否进行下一轮(y/n)");
        start = input.next();

      }
      //显示结果
      showResult();
      System.out.println("\n要开始下一局吗?(y/n):");
      exit = input.next();
      System.out.println();

    } while (!exit.equals("n"));

    System.out.println("系统退出");
  }
  private void showResult() {
    // TODO Auto-generated method stub
    //显示对战次数
    System.out.println("------------------------------");
    System.out.println(computer.name+"  VS  "+person.name);
    System.out.println("对战次数:"+count);
    //显示最终得分
    System.out.println("\n姓名\t得分");
    System.out.println(person.name+"\t"+person.score);
    System.out.println(computer.name+"\t"+computer.score+"\n");

    //显示对战结果
    int result = calcResult();
    if (result==1) {
      System.err.println("结果:打成平手,下次再和你一分高下");
    }else if (result==2) {
      System.err.println("结果:恭喜恭喜你赢得比赛");
    }else
    {
      System.err.println("结果:不气馁,下次再来");//计算机赢
    }
    System.out.println("--------------------------");


  }

  //计算比赛结果
  private int calcResult() {
    // TODO Auto-generated method stub
    if (person.score == computer.score) {
      return 1;//战平
    }else if (person.score > computer.score) {
      return 2;//游戏玩家赢
    }else {
      return 3;//惜败
    }
  }

}

StartGuess.java类

package com.whf.guess.game;
/** 
 * @author :辰
 * @version
 * 创建时间:2017年2月24日 上午9:35:36 
 * 
 */
public class StartGuess {
  public static void main(String[] args) {
    Game game = new Game();

    game.inital();

    game.startGame();
  }

}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...