PE 84 Monopoly odds (随机大模拟大富翁)

我不是女神ヾ 2022-07-14 00:55 147阅读 0赞

Monopoly odds

Problem 84

In the game, Monopoly, the standard board is set up in the following way:











































































GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3 JAIL
H2
C1
T2
U1
H1
C2
CH3
C3
R4
R2
G3
D1
CC3
CC2
G2
D2
G1
D3
G2J F3 U2 F2 F1 R3 E3 E2 CH2 E1 FP

A player starts on the GO square and adds the scores on two 6-sided dice to determine the number of squares they advance in a clockwise direction. Without any further rules we would expect to visit each square with equal probability: 2.5%. However, landing on G2J (Go To Jail), CC (community chest), and CH (chance) changes this distribution.

In addition to G2J, and one card from each of CC and CH, that orders the player to go directly to jail, if a player rolls three consecutive doubles, they do not advance the result of their 3rd roll. Instead they proceed directly to jail.

At the beginning of the game, the CC and CH cards are shuffled. When a player lands on CC or CH they take a card from the top of the respective pile and, after following the instructions, it is returned to the bottom of the pile. There are sixteen cards in each pile, but for the purpose of this problem we are only concerned with cards that order a movement; any instruction not concerned with movement will be ignored and the player will remain on the CC/CH square.

  • Community Chest (2/16 cards):

    1. Advance to GO
    2. Go to JAIL
  • Chance (10/16 cards):

    1. Advance to GO
    2. Go to JAIL
    3. Go to C1
    4. Go to E3
    5. Go to H2
    6. Go to R1
    7. Go to next R (railway company)
    8. Go to next R
    9. Go to next U (utility company)
    10. Go back 3 squares.

The heart of this problem concerns the likelihood of visiting a particular square. That is, the probability of finishing at that square after a roll. For this reason it should be clear that, with the exception of G2J for which the probability of finishing on it is zero, the CH squares will have the lowest probabilities, as 5/8 request a movement to another square, and it is the final square that the player finishes at on each roll that we are interested in. We shall make no distinction between “Just Visiting” and being sent to JAIL, and we shall also ignore the rule about requiring a double to “get out of jail”, assuming that they pay to get out on their next turn.

By starting at GO and numbering the squares sequentially from 00 to 39 we can concatenate these two-digit numbers to produce strings that correspond with sets of squares.

Statistically it can be shown that the three most popular squares, in order, are JAIL (6.24%) = Square 10, E3 (3.18%) = Square 24, and GO (3.09%) = Square 00. So these three most popular squares can be listed with the six-digit modal string: 102400.

If, instead of using two 6-sided dice, two 4-sided dice are used, find the six-digit modal string.

代码:

  1. import java.math.*;
  2. import java.util.*;
  3. public class Main {
  4. int[] board = new int[40];
  5. int currentSquare = 0;
  6. int totalRolls = 0;
  7. int CHcard = 0;
  8. int CCcard = 0;
  9. Random generator = new Random();
  10. //特殊的位置
  11. int cc1 = 2;
  12. int cc2 = 17;
  13. int cc3 = 33;
  14. int jail = 10;
  15. int go2jail = 30;
  16. int ch1 = 7;
  17. int ch2 = 22;
  18. int ch3 = 36;
  19. int go = 0;
  20. //记录当前和前2次的投骰子的情况
  21. int[] prevPrevRoll = new int[2];
  22. int[] prevRoll = new int[2];
  23. int[] currRoll = new int[2];
  24. int numberOfTriples = 0;
  25. Main()
  26. {
  27. System.out.println("开始模拟大富翁:");
  28. for(int i = 0; i < board.length; i++){
  29. board[i] = 0;
  30. }
  31. prevRoll[1] = 30;//保证你不能立刻进监狱,G2J:30
  32. }
  33. public int Roll(){
  34. //两个骰子,4个面
  35. int die1 = generator.nextInt(4) + 1;
  36. int die2 = generator.nextInt(4) + 1;
  37. //记录当前和前2次的投骰子的情况
  38. prevPrevRoll[0] = prevRoll[0];
  39. prevPrevRoll[1] = prevRoll[1];
  40. prevRoll[0] = currRoll[0];
  41. prevRoll[1] = currRoll[1];
  42. currRoll[0] = die1;
  43. currRoll[1] = die2;
  44. int total = die1 + die2;
  45. totalRolls++;
  46. int possibleMove = (currentSquare + total)%40;
  47. if(tooManyDoubles(currRoll, prevRoll, prevPrevRoll))
  48. {
  49. //投到相同的就重新设置上一次投的情况
  50. currRoll[1] = -1;
  51. numberOfTriples++;
  52. currentSquare = jail;
  53. board[currentSquare]++;
  54. return currentSquare;
  55. }
  56. //机会卡
  57. else if(possibleMove == 7 || possibleMove == 22 || possibleMove == 36 ){
  58. currentSquare = Chance(possibleMove);
  59. board[currentSquare]++;
  60. return currentSquare;
  61. }
  62. //宝箱卡
  63. else if(possibleMove == cc1 || possibleMove == cc2 || possibleMove == cc3){
  64. currentSquare = ComChest(possibleMove);
  65. board[currentSquare]++;
  66. return currentSquare;
  67. }
  68. //进监狱
  69. else if(possibleMove == go2jail){
  70. currentSquare = jail;
  71. board[currentSquare]++;
  72. return currentSquare;
  73. }
  74. //什么都没有发生,继续移动
  75. else
  76. {
  77. currentSquare = (currentSquare + total)%40;
  78. board[currentSquare]++;
  79. return currentSquare;
  80. }
  81. }
  82. //连续三次投骰子都投到3次
  83. public boolean tooManyDoubles(int[] a, int[] b, int[] c){
  84. if(a[0] == a[1]){
  85. if(b[0] == b[1]){
  86. if(c[0] == c[1]){
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. /*
  94. * 宝箱卡 (2/16 张卡):
  95. 回到起点“GO”
  96. 进入监狱“JAIL”
  97. */
  98. //抽宝箱里的卡
  99. public int ComChest(int currentCell){
  100. int card = generator.nextInt(16); //一共16张
  101. switch(card)
  102. {
  103. case 1:currentCell = go; break;
  104. case 2:currentCell = jail; break;
  105. default : break;//题目说其他卡片无视效果
  106. }
  107. return currentCell;
  108. }
  109. /*
  110. * 机会卡 (10/16 张卡):
  111. 回到起点“GO”
  112. 进入监狱“JAIL”
  113. 移动到“C1”
  114. 移动到“E3”
  115. 移动到“H2”
  116. 移动到“R1”
  117. 移动到下一个“R”(铁路公司)
  118. 移动到下一个“R”
  119. 移动到下一个“U”(公共服务公司)
  120. 后退三步
  121. */
  122. //抽机会卡
  123. public int Chance(int currentCell){
  124. int card = generator.nextInt(16);
  125. switch(card){
  126. case 1: currentCell = go; break; //go
  127. case 2: currentCell = jail; break; //jail
  128. case 3: currentCell = 11; break; //c1
  129. case 4: currentCell = 24; break; //e3
  130. case 5: currentCell = 39;break; //h2
  131. case 6: currentCell = 5; break; //r1
  132. case 7: currentCell = nextRR(currentCell); break; //next railway
  133. case 8: currentCell = nextRR(currentCell); break; //next railway
  134. case 9: currentCell = nextUtil(currentCell); break; //next utility
  135. case 10: currentCell = back3Squares(currentCell); break; //back 3 squares
  136. default:break; //其他无效果,不变
  137. }
  138. return currentCell;
  139. }
  140. //移动到下一个“U”(公共服务公司)
  141. public int nextUtil(int currentCell)
  142. {
  143. if(currentCell >= 12 && currentCell < 28)
  144. {
  145. return 28;
  146. }
  147. else
  148. {
  149. return 12;
  150. }
  151. }
  152. //移动到下一个“R”
  153. public int nextRR(int currentCell){
  154. if(currentCell >= 35 || currentCell < 5)
  155. {
  156. return 5;
  157. }
  158. else if(currentCell >= 5 && currentCell < 15)
  159. {
  160. return 15;
  161. }
  162. else if(currentCell >= 15 && currentCell < 25)
  163. {
  164. return 25;
  165. }
  166. else
  167. {
  168. return 35;
  169. }
  170. }
  171. //后退三步
  172. //注意:特殊情况
  173. public int back3Squares(int currentCell){
  174. if(currentCell == 2){
  175. return 39;
  176. }
  177. //特殊情况
  178. else if(currentCell == ch3){
  179. return ComChest(currentCell);
  180. }
  181. else
  182. {
  183. return currentCell - 3;
  184. }
  185. }
  186. public static void main(String[] args) {
  187. Main m = new Main();
  188. //模拟一千万步
  189. int itterations = 10000000;
  190. for(int i = 0; i < itterations; i++){
  191. m.Roll();
  192. }
  193. //打印百分比在3%以上的情况
  194. for(int i = 0; i < m.board.length; i++)
  195. {
  196. float percent = (float)m.board[i]/(float)itterations;
  197. percent *= 100;
  198. if(percent > 3)
  199. {
  200. System.out.println("位置:" + i + "占百分比: " + percent);
  201. }
  202. }
  203. //注意:每一次模拟都不一样,因为随机模拟
  204. System.out.println("连续3次投出两个骰子相同次数:"+m.numberOfTriples);
  205. }
  206. }

发表评论

表情:
评论列表 (有 0 条评论,147人围观)

还没有评论,来说两句吧...

相关阅读

    相关 寻找富翁 (25 分)

    胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人。假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁。 输入格式: 输入首先