学以致用——Java源码——抛双骰儿游戏改进版(Craps Game Modification with wagering)

小编 2026-07-15 阅读:1465 评论:0
  package exercises.ch6Methods; import java.security.SecureRandom; import java.util.Scanner; /** *...

 

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

 

很遗憾,你已输光了!努力工作,赚钱后再来吧!

 

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表