Java如何实现猜拳小游戏

发布时间:2021-08-04 09:15:45 作者:小新
来源:亿速云 阅读:107

这篇文章主要为大家展示了“Java如何实现猜拳小游戏”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java如何实现猜拳小游戏”这篇文章吧。

先看看我写了哪些类:

Java如何实现猜拳小游戏

Player:玩家类;

ComputerPlayer:机器人玩家类,主要用来实现机器人随机出拳;

Game:游戏类,主要实现游戏规则的逻辑,以及正式游戏的逻辑;

TestGuessBox:代码测试类;

Player类:

//玩家类
public class Player {
 private String name; //玩家昵称
 private int score; //玩家积分
 private String box; //玩家出的
 
 //玩家构造函数,传入玩家昵称与玩家初始积分
 Player(String name,int score){
 this.name=name;
 this.score=score;
 }
 
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getScore() {
 return score;
 }
 public void setScore(int score) {
 this.score = score;
 }
 public String getBox() {
 return box;
 }
 public void setBox(String box) {
 this.box = box;
 }
 
 
}

ComputerPlayer类

public class ComputerPlayer extends Player {
 
 
 //机器人玩家构造函数,传入机器人昵称与初始积分
 ComputerPlayer(String name, int score) {
 super("机器人"+name, score);
 // TODO Auto-generated constructor stub
 }
 
 /**
 * 实现机器人玩家随机出拳的逻辑
 */
 void punch() {
 String[] box = {"剪刀","石头","布"};
 int index =(int) (Math.random()*3);
 
 this.setBox(box[index]);
 }
}

Game类

import java.util.Scanner;
 
public class Game {
 Player p; //玩家
 ComputerPlayer cp; //机器人玩家
 
 //构造函数,得到一个玩家和一个机器人玩家
 Game(Player p, ComputerPlayer cp) {
 this.p = p;
 this.cp = cp;
 }
 
 //开始游戏
 void start() {
  System.out.println("玩家"+p.getName()+
   "和"+cp.getName()+"开始游戏咯");
  System.out.println("您的初始积分为:"+p.getScore()+
   "\n"+cp.getName()+"的积分是:"+cp.getScore());
  Scanner sc=new Scanner(System.in);
  while(true) {
  
  System.out.println("请出拳(剪刀石头布,exit退出游戏):");
  String pbox=sc.next();
  if(filter(pbox)) { //过滤器
   if(pbox.equals("exit")) { //退出游戏
   break;
   }else {
   p.setBox(pbox);
   cp.punch();
   System.out.println("您出了:"+p.getBox());
   System.out.println(cp.getName()+"出了:"+cp.getBox());
   int result = ruler(p,cp);
   if(result>0) {
    System.out.println("您赢了,赢得10积分");
    p.setScore(p.getScore()+10);
    cp.setScore(cp.getScore()-10);
   }
   else if(result<0) {
    System.out.println("您输了,扣除10积分");
    p.setScore(p.getScore()-10);
    cp.setScore(cp.getScore()+10);
   }
   else {
    System.out.println("您和机器人打平了!");
   }
   }
  }
  
  else {
  System.out.println("输入了无法识别的关键字,请重新输入:");
  continue; //退出本次循环,进入下一次循环
  }
  
  } 
  
  System.out.println("本轮结束,积分情况如下:");
  System.out.println("您的当前积分:"+p.getScore());
  System.out.println(cp.getName()+"的当前积分:"
  +cp.getScore());
  
 }
 
 /**
 * 游戏规则
 * 
 * @param p1 玩家1
 * @param cp2 机器玩家2
 * @return 0为打平,1为玩家赢,-1为机器玩家赢
 */
 int ruler(Player p1, Player cp2) {
 
 if (p1.getBox().equals("剪刀")) {
  if (cp2.getBox().equals("石头"))
  return -1;
  else if (cp2.getBox().equals("布"))
  return 1;
 } else if (p1.getBox().equals("石头")) {
  if (cp2.getBox().equals("剪刀"))
  return 1;
  else if (cp2.getBox().equals("布"))
  return -1;
 } else if (p1.getBox().equals("布")) {
  if (cp2.getBox().equals("剪刀"))
  return -1;
  else if (cp2.getBox().equals("石头"))
  return 1;
 }
 return 0;
 }
 
 /**
 * 过滤器
 * @param s 需要过滤的文字
 * @return
 */
 boolean filter(String s) {
 if (s.equals("剪刀") || s.equals("石头") || s.equals("布") || s.equals("exit")) {
  return true;
 } else
  return false;
 }
}

TestGuessBox类

public class TestGuessBox {
 
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Player p =new Player("小七月",100);
 ComputerPlayer cp=new ComputerPlayer("小丑八怪",100);
 Game game=new Game(p,cp);
 game.start();
 
 }
 
}

以上是“Java如何实现猜拳小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. java如何制作猜拳小游戏
  2. python如何实现人机猜拳小游戏

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

java

上一篇:怎么用日志记录Linux用户执行的每一条命令

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

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

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