java入门游戏_Java入门案列之猜拳小游戏

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

先看看我写了哪些类:

a35ee4a9d50d138f9f6042026a899990.png

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();

}

}

很简单,理清了思路就很容易写了。如果有错误请指出。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值