Java入门案列之猜拳小游戏

这篇文章主要为大家详细介绍了Java入门案列之猜拳小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近正在学习Java基础知识,终于完成了第一个小demo,记录下来,算是一个小总结与整理,也希望可以帮助到别人。

先看看我写了哪些类:

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入门案列之猜拳小游戏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java