使用面向对象的编程思想设计编写一个猜拳游戏

使用面向对象的编程思想设计编写一个猜拳游戏,实现玩家和电脑猜拳的过程,玩家和电脑都具备猜拳方法,玩家的出拳使用键盘输入实现,电脑出拳使用随机数,、

要求:每个回合双方各处一次拳,每个回合结束后系统提示结果信息并提示玩家是否要继续,退出游戏打印呈现游戏的结果(游戏总局,玩家获胜、平、输的次数,玩家胜率),通过胜率决定最终的胜利

玩家类

/**
*玩家类
*/
public class Player{
  int winCount;
  int drawCount;
  Scanner sc;

/**
*玩家初始化
*/
  public void int(){
    //将实例化写入方法中,在使用是只用调用一次,不用多次new
    sc=new Scanner(System.in);
  }

/**
*玩家出拳的方法
*/
  public int showFinger(){
    System.out.println("请选择您的出拳:1代表石头,2代表剪刀,3代表布");
    int result=sc.nextInt();
    return result;
  }
}

电脑类

/**
* 电脑类
*/
public class Computer(){
  Random random;

/**
* 电脑类初始化
*/
  public void init(){
    random=new Random();
  }

/**
*电脑出拳的方法
*/
  public int showFinger(){
    //生成0-2加上1,即1-3(不用变量接收,直接返回)
    return random.nextInt(3)+1;
  }
}

游戏类(封装了游戏的完整过程)

public class Game{
  //玩家对象
  Player player;
  //电脑对象
  Computer computer;
  //记录总局数
  int count;
  Scanner sc;
/**
* 初始化方法
*/
  public void init(){
    //实例化玩家对象
    player=new Player();
    //初始化玩家
    player.init();
    //实例化电脑对象
    computer=new Computer();
    //初始化电脑
    computer.init();
    sc=new Scanner(System.in);
    //初始化总局数
    count=0;
  }

/**
* 将出拳数字改为字符串的方法
*/
  public String changeToString(int result){
    String str="";
    switch(result){
      case 1:
        str="石头"
        break;
      case 2:
        str="剪刀"
        break;
      case 3:
      str="布"
        break;
    }
  return str;
  }

/**
* 描述游戏逻辑的方法
*/
  public void start(){
    String answer;
    do{
      //进入一次循环就总局数加一
      count++;
      //调用玩家出拳的方法
      int playerResult=player.showFinger();
      //将出拳结果数字改为String
      System.out.println("玩家出拳:"+changeToString(playerResult));
      //调用电脑出拳的方法
      int computerResult=player.showFinger();
      //将出拳结果数字改为String
      System.out.println("玩家出拳:"+changeToString(computerResult));
      //玩家胜
      if(playerResult==1;computerResult==2||playerResult==2;computerResult==3||playerResult==3;computerResult==1){
        System.out.println("玩家赢了");
      }
      //平局
      else if(playerResult==computerResult)
        System.out.println("平局");
      }
      //玩家输
      else(playerResult==computerResult)
        System.out.println("玩家输了");
      }
      System.out.println("是否继续游戏:y/n");
      answer=sc.next();
    }while(answer.equals("y"));
  }

/**
* 显示游戏结果的方法
*/
  public void showResult(){
    //输的次数
    int loseCount=count-player.winCount-player.drawCount;
    System.out.println("游戏总场数:"+count);
    System.out.println("胜局:"+player.winCount);
    System.out.println("平局:"+player.drawCount);
    System.out.println("败局:"+loseCount);
    System.out.println("胜率:"+(int)(player.winCount/(double)(count*100))+"%");
    if(winCount>loseCount){
      System.out.println("恭喜获得最终胜利!");
    }
    else if(winCount=loseCount){
      System.out.println("平局");
    }
    else if(winCount<loseCount){
      System.out.println("很遗憾,您输了!");
    }
  }

启动游戏的APP

public class GameAPP{
  public static void main(String[] args){
    //实例化游戏类
    Game game=new Game();
    //调用初始化方法
    game.init();
    //调用游戏逻辑开始的方法
    game.start();
  }
}
原文地址:https://www.cnblogs.com/gfl-1112/p/12676900.html