温馨提示×

温馨提示×

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

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

怎么用Java写一个猜拳小游戏

发布时间:2021-09-14 23:49:45 来源:亿速云 阅读:128 作者:chen 栏目:编程语言

这篇文章主要介绍“怎么用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写一个猜拳小游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

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

AI