java项目实现猜拳小游戏

所属分类: 软件编程 / java 阅读数: 69
收藏 0 赞 0 分享

本文实例为大家分享了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;
  }
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多