java程序员生存手册-craps 游戏-一个简单的游戏


import java.util.Random;


public class CrapsGame {

/**
*
*一个简单的赌*博游戏,游戏规则如下:
*玩家掷两个骰子,点数为1到6,如果第一次点数和为7或11,则玩家胜,
*如果点数和为2、3或12,则玩家输,
*如果和为其它点数,则记录第一次的点数和,然后继续掷骰,直至点数和等于第一次掷出的点数和,则玩家胜,
*如果在这之前掷出了点数和为7,则玩家输。
*it looks difficult,but it's easy actually
*/
public static void main(String[] args) {
CrapsGame cg=new CrapsGame();
cg.begin();
}

public void begin(){
int step01=sumTwoDices();
switch (step01){
case 7:
case 11:
System.out.println("player win");
break;//don't forget this
case 2:
case 3:
case 12:
System.out.println("player lose");
break;
default:
while(true){
int step02=sumTwoDices();
if(step02==7){
System.out.println("play lose");
break;
}
if(step02==step01){
System.out.println("play win");
break;
}
}
}
}

//two dices, counting from 1 to 6
//return the sum of them
public int sumTwoDices(){
int sum=0;
Random random=new Random();
//between 0 (inclusive) and n (exclusive)
int diceA=random.nextInt(6)+1;
int diceB=random.nextInt(6)+1;
System.out.println(diceA+" "+diceB);
sum=diceA+diceB;
return sum;
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值