package exercises.ch6Methods;
import java.security.SecureRandom;
import java.util.Scanner;
/**
*
* 6.33 (Craps Game Modification) Modify the craps program of Fig. 6.8 to allow wagering.
* Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager.
* Check that wager is less than or equal to bankBalance, and if it’s not,
* have the user reenter wager until a valid wager is entered. Then, run one game of craps.
* If the player wins, increase bankBalance by wager and display the new bankBalance.
* If the player loses, decrease bankBalance by wager, display the new bank- Balance,
* check whether bankBalance has become zero and, if so, display the message "Sorry. You busted!"
* As the game progresses, display various messages to create some “chatter,” such as
* "Oh, you're going for broke, huh?" or "Aw c'mon, take a chance!" or "You're up big.
* Now's the time to cash in your chips!". Implement the “chatter” as a separate method that
* randomly chooses the string to display.
*
*/
public class CrapsWithWager
{
// create secure random number generator for use in method rollDice
private static final SecureRandom randomNumbers = new SecureRandom();
// enum type with constants that represent the game status
private enum Status {CONTINUE, WON, LOST};
// constants that represent common rolls of the dice
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_LEVEN = 11;
private static final int BOX_CARS = 12;
// plays one game of craps
public static void main(String[] args)
{
int myPoint = 0; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
int playFlag;
int bankBalance = 1000;
int wager = 0;
Scanner input =new Scanner(System.in);
do {
System.out.print("抛双骰儿游戏,请按1开始游戏(输入-1退出游戏):");
playFlag = input.nextInt();
if(playFlag ==-1)
{System.out.print("已退出程序");
break;
}
//在此插入一句随机生成的聊天信息,增强游戏气氛
chat();
System.out.printf("你当前的余额为%d,请输入下注金额(整数,输入-1退出游戏):", bankBalance);
wager = input.nextInt();
while(wager <0 || wager > bankBalance)
{System.out.print("请输入有效的下注金额:");
wager = input.nextInt();
}
int sumOfDice = rollDice(); // first roll of the dice
// determine game status and point based on first roll
switch (sumOfDice)
{
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
bankBalance += wager;
break;
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus = Status.LOST;
if (bankBalance - wager >= 0)
bankBalance -= wager;
else
bankBalance = 0;
break;
default: // did not win or lose, so remember point
gameStatus = Status.CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
System.out.printf("哦,平手!点数为: %d%n", myPoint);
break;
}
// while game is not complete
while (gameStatus == Status.CONTINUE) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if (sumOfDice == myPoint) // win by making point
{gameStatus = Status.WON;
bankBalance += wager;
}
else
{
if (sumOfDice == SEVEN) // lose by rolling 7 before point
{
gameStatus = Status.LOST;
if (bankBalance - wager >= 0)
bankBalance -= wager;
else
bankBalance = 0;
}
}
}
// display won or lost message
if (gameStatus == Status.WON)
System.out.printf("恭喜你,你赢了!你当前账户余额为:%d%n", bankBalance);
else
System.out.printf("哦哦,你输了!你当前账户余额为:%d%n", bankBalance);
if (bankBalance == 0)
System.out.printf("%n很遗憾,你已输光了!努力工作,赚钱后再来吧!");
System.out.println(); //输入空行,开始下一局游戏
}while (playFlag != -1 && bankBalance >0); //余额为0时,自动退出游戏
input.close();
}
// roll dice, calculate sum and display results
public static int rollDice()
{
// pick random die values
int die1 = 1 + randomNumbers.nextInt(6); // first die roll
int die2 = 1 + randomNumbers.nextInt(6); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf("你抛出的点数为: %d + %d = %d%n",
die1, die2, sum);
return sum;
}
// 输出随机聊天信息
public static void chat()
{
int msgNum = 1 + randomNumbers.nextInt(4); // 生成随机消息编号
//表示随机聊天信息的常数
final String MSG1 = "不要小看抛双骰儿,这里面有大学问!";
final String MSG2 = "今天点子有点儿背?积德行善会转运哦!";
final String MSG3 = "小赌怡情,大赌伤身哦!";
final String MSG4 = "运气是什么,账户余额翻倍靠的就是运气!";
//提示下注
switch (msgNum){
case 1:
System.out.println(MSG1);
break;
case 2:
System.out.println(MSG2);
break;
case 3:
System.out.println(MSG3);
break;
case 4:
System.out.println(MSG4);
break;
}
}
} // end class Craps
运行结果:
抛双骰儿游戏,请按1开始游戏(输入-1退出游戏):1 小赌怡情,大赌伤身哦! 你当前的余额为1000,请输入下注金额(整数,输入-1退出游戏):1000 你抛出的点数为: 2 + 4 = 6 哦,平手!点数为: 6 你抛出的点数为: 4 + 1 = 5 你抛出的点数为: 5 + 5 = 10 你抛出的点数为: 3 + 1 = 4 你抛出的点数为: 2 + 6 = 8 你抛出的点数为: 1 + 4 = 5 你抛出的点数为: 4 + 1 = 5 你抛出的点数为: 4 + 2 = 6 恭喜你,你赢了!你当前账户余额为:2000
抛双骰儿游戏,请按1开始游戏(输入-1退出游戏):1 运气是什么,账户余额翻倍靠的就是运气! 你当前的余额为2000,请输入下注金额(整数,输入-1退出游戏):2000 你抛出的点数为: 4 + 6 = 10 哦,平手!点数为: 10 你抛出的点数为: 3 + 2 = 5 你抛出的点数为: 3 + 2 = 5 你抛出的点数为: 2 + 6 = 8 你抛出的点数为: 5 + 4 = 9 你抛出的点数为: 2 + 3 = 5 你抛出的点数为: 4 + 5 = 9 你抛出的点数为: 3 + 4 = 7 哦哦,你输了!你当前账户余额为:0
很遗憾,你已输光了!努力工作,赚钱后再来吧!
|