學以致用——Java原始碼——拋雙骰兒遊戲改進版(Craps Game Modification with wagering)
阿新 • • 發佈:2018-12-30
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開始遊戲(輸入 運氣是什麼,賬戶餘額翻倍靠的就是運氣! 你當前的餘額為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
很遺憾,你已輸光了!努力工作,賺錢後再來吧!
|