java beetle_Java小项目-BeetleGame Demo

规则:

玩家:两人

对象:要成为第一位拥有一只完整甲虫的玩家。完成的甲虫具有躯干、头部、六条腿、两只眼睛、两根触角和一条尾巴。

设置:每位玩家开始玩游戏时,甲虫身上不具有任何器官。

玩法:轮到你时,投骰子,并按结果采取动作

1:

如果你的甲虫已经有一个躯干,就将骰子传递给下一个玩家,否则,添加一个躯干,并再投掷一次骰子

2:

如果你的甲虫已经有头部或者没有躯干,就将骰子传递给下一个玩家。否则,添加一个头部,并再投掷一次骰子

3:

如果你的甲虫已经有6条腿或者没有躯干,就将骰子传递给下一位玩家。否则,添加两条腿,并在投掷一次骰子

4:

如果你的甲虫已经有两只眼睛或者没有头部,就将骰子传递给下一个玩家,否则,添加一支眼睛,并再投掷一次骰子

5:

如果你的甲虫已经有两根触角或者没有头部,就将骰子传递给下一个玩家。否则,添加一根触角,并再投掷一次骰子

6:

如果你的甲虫已经有一条尾巴或者没有躯干,就将骰子传递给下一个玩家,否则,添加一条尾巴。

Beetle class

/**

* Created by root on 16-2-22.

*/

public class Beetle {

private boolean body;//甲壳虫的组成 身体

private int eyes;//甲壳虫的组成 眼睛

private int feelers;//触角

private boolean head;//头部

private int legs;//腿

private boolean tail;//尾巴

public Beetle() {//因为系统默认值和模型初始值形同所以不必对字段初始化

}

public boolean addBody() {//

if (body) return true;//如果有头部则交换

else {

this.body = true;//增加头部

return false;//不交换继续投掷骰子

}

}

public boolean addHead() {

if (head||(!body)) return true;//交换

else {

this.head = true;

return false;

}

}

public boolean addTail() {

if (tail||(!body)) return true;

else {

this.tail = true;

return false;

}

}

public boolean addEyes() {

if (eyes>1||(!head)) {

return true;

} else{

eyes++;//增加一个眼睛

return false;}

}

public boolean addFeelers() {

if (feelers>1||(!head)) {

return true;

} else{

feelers++;

return false;}

}

public boolean addLegs() {

if (legs >5||(!body)) {

return true;

} else{legs+=2;

return false;}

}

public boolean isComplete() {

if (body && head && tail && (eyes == 2) && (feelers == 2) && (legs == 6)) {

return true;

} else return false;

}

@Override

public String toString() {

return "Beetle{" +

"body=" + body +

", eyes=" + eyes +

", feelers=" + feelers +

", head=" + head +

", legs=" + legs +

", tail=" + tail +

'}';

}

}

Die class

public class Die {

private int topFace;

public Die(){//Constructor

this.topFace=1;//骰子一开始正面是1

}

public int getTopFace() {

return topFace;//访问器

}

public void setTopFace(int topFace) {

this.topFace = topFace;

}

public void roll(){

this.topFace=((int)(Math.random()*6))+1;

}

}

BeetleGame class

public class BeetleGame {

Beetle bug1;

Beetle bug2;

Die die;

public boolean takeTurn(int player, Beetle bug) {//如果要交换返回true

die.roll();

switch (die.getTopFace()) {

case 1:

return bug.addBody();

case 2:

return bug.addHead();

case 3:

return bug.addLegs();

case 4:

return bug.addEyes();

case 5:

return bug.addFeelers();

default:

return bug.addTail();

}

}

public int play() {

int player = 1;

Beetle bug = bug1;

while (!bug.isComplete()){

if(takeTurn(player,bug)){

if(player==1){

player=2;

bug=bug2;

}

else {

player=1;

bug=bug1;

}

}

}

return player;

}

public BeetleGame() {

bug1 = new Beetle();

bug2 = new Beetle();

die = new Die();

}

public static void main(String[] args) {

BeetleGame game = new BeetleGame();

System.out.println(game.play());

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值