温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java项目如何实现猜拳小游戏

发布时间:2020-07-21 14:47:40 来源:亿速云 阅读:162 作者:小猪 栏目:编程语言

小编这次要给大家分享的是java项目如何实现猜拳小游戏,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

项目名称

猜拳小游戏

项目描述

玩家与电脑进行猜拳游戏,玩家行为采用输入方式,电脑行为采用随机形式。

代码实现

测试类

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

主类:实现主方法

public class Game {
  private People people;
  private Computer computer;
  public Game(){
    people = new People("zs");
    computer = new Computer("computer");
  }
  public void start(){
    boolean flag = true;
    while (flag) {
      System.out.println("开始游戏:");
      int count = 0;
      while (count < 3) {
        String peopleFist = people.doFist();
        String comFist = computer.doFist();
        //people赢
        if (peopleFist.equals("石头") && comFist.equals("剪刀") ||
            peopleFist.equals("剪刀") && comFist.equals("布") ||
            peopleFist.equals("布") && comFist.equals("石头")) {
          System.out.println(people.getName() + "赢了");
          people.addScore(1);
        } else if (peopleFist.equals("石头") && comFist.equals("石头") ||
            peopleFist.equals("剪刀") && comFist.equals("剪刀") ||
            peopleFist.equals("布") && comFist.equals("布")) {
          System.out.println("平局");
        } else if (peopleFist.equals("石头") && comFist.equals("布") ||
            peopleFist.equals("剪刀") && comFist.equals("石头") ||
            peopleFist.equals("布") && comFist.equals("剪刀")) {
          System.out.println(computer.getName() + "赢了");
          computer.addScore(1);
        }
        count++;
      }
      if (people.getScore() > computer.getScore()) {
        System.out.println(people.getName() + "赢了 " + people.getScore() + ":" + computer.getScore());
      } else if (people.getScore() == computer.getScore()) {
        System.out.println("平局");
      } else if (people.getScore() < computer.getScore()) {
        System.out.println(computer.getName() + "赢了 " + computer.getScore() + ":" + people.getScore());
      }
      System.out.println("是否开始新游戏:");
      Scanner scanner = new Scanner(System.in);
      String str = scanner.next();
      if (str.equals("否")) {
        flag = false;
      }else {
        people.setScore();
        computer.setScore();
      }
    }
  }
}

玩家

public class People {
  private String name;
  private int score;
  public People(String name){
    this.name = name;
    score = 0;
  }
  public String getName(){
    return name;
  }
  public void addScore(int score){
    this.score += score;
  }
  public int getScore(){
    return score;
  }
  public int setScore(){
    this.score = 0;
    return score;
  }
  public String doFist(){
    System.out.println("请出拳:");
    Scanner scanner = new Scanner(System.in);
    String fist = scanner.next();
    return fist;
  }
}

电脑

public class Computer {
  private String name;
  private int score;
  public Computer(String name){
    this.name = name;
    score = 0;
  }
  public String getName(){
    return name;
  }
  public void addScore(int score){
    this.score += score;
  }
  public int getScore(){
    return score;
  }
  public int setScore(){
    this.score = 0;
    return score;
  }
  public String doFist(){
    Random random = new Random();
    int n = random.nextInt(3);
    String fist;
    if(n == 0){
      fist = "石头";
    }else if(n == 1){
      fist = "剪刀";
    }else {
      fist = "布";
    }
    System.out.println("对方出的是:"+fist);
    return fist;
  }
}

看完这篇关于java项目如何实现猜拳小游戏的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI