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;
	}
}

你可能感兴趣的:(java)