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

Monopoly odds

Problem 84

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

GOA1CC1A2T1R1B1CH1B2B3JAIL
H2
C1
T2
U1
H1
C2
CH3
C3
R4
R2
G3
D1
CC3
CC2
G2
D2
G1
D3
G2JF3U2F2F1R3E3E2CH2E1FP

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.



代码:

import java.math.*;
import java.util.*;

public class Main {
    
    int[] board = new int[40];
    int currentSquare = 0; 
    int totalRolls = 0;
    int CHcard = 0;
    int CCcard = 0;
    Random generator = new Random();
    
    //特殊的位置
    int cc1 = 2;
    int cc2 = 17;
    int cc3 = 33;
    int jail = 10;
    int go2jail = 30;
    int ch1 = 7;
    int ch2 = 22;
    int ch3 = 36;
    int go = 0;
    
    //记录当前和前2次的投骰子的情况
    int[] prevPrevRoll = new int[2];
    int[] prevRoll = new int[2];
    int[] currRoll = new int[2];
    int numberOfTriples = 0;
    
    Main()
    {
        System.out.println("开始模拟大富翁:");
        for(int i = 0; i < board.length; i++){
            board[i] = 0;
        }
        prevRoll[1] = 30;//保证你不能立刻进监狱,G2J:30
    }
    
    public int Roll(){
        //两个骰子,4个面
        int die1 = generator.nextInt(4) + 1;
        int die2 = generator.nextInt(4) + 1; 

        //记录当前和前2次的投骰子的情况
        prevPrevRoll[0] = prevRoll[0]; 
        prevPrevRoll[1] = prevRoll[1];
        prevRoll[0] = currRoll[0];
        prevRoll[1] = currRoll[1];
        currRoll[0] = die1; 
        currRoll[1] = die2;
        int total = die1 + die2;
        totalRolls++;
        
        int possibleMove = (currentSquare + total)%40;
        
        if(tooManyDoubles(currRoll, prevRoll, prevPrevRoll))
        {
            //投到相同的就重新设置上一次投的情况
            currRoll[1] = -1;
            numberOfTriples++;
            currentSquare = jail;
            board[currentSquare]++;
            return currentSquare;
        }
        //机会卡
        else if(possibleMove == 7 || possibleMove == 22 || possibleMove == 36 ){ 
            
            currentSquare = Chance(possibleMove);
            board[currentSquare]++;
            return currentSquare;
        }
        //宝箱卡
        else if(possibleMove == cc1 || possibleMove == cc2 || possibleMove == cc3){
            
            currentSquare = ComChest(possibleMove);
            board[currentSquare]++;
            return currentSquare;
        }
        //进监狱
        else if(possibleMove == go2jail){
            
            currentSquare = jail;
            board[currentSquare]++;
            return currentSquare;
        }
         //什么都没有发生,继续移动
        else
        {
            currentSquare = (currentSquare + total)%40; 
            board[currentSquare]++;
            return currentSquare;
        }
    }
    
    //连续三次投骰子都投到3次
    public boolean tooManyDoubles(int[] a, int[] b, int[] c){       
        if(a[0] == a[1]){
            if(b[0] == b[1]){
                if(c[0] == c[1]){            
                	return true;
                }
            }
        }
        return false;
    }
    /*
     * 宝箱卡 (2/16 张卡):
	回到起点“GO”
	进入监狱“JAIL”
     */
    //抽宝箱里的卡
    public int ComChest(int currentCell){
        int card = generator.nextInt(16); //一共16张
        switch(card)
        {
            case 1:currentCell = go; break; 
            case 2:currentCell = jail; break; 
            default : break;//题目说其他卡片无视效果
        }
        return currentCell;
    }
     /*
      * 机会卡 (10/16 张卡):
		回到起点“GO”
		进入监狱“JAIL”
		移动到“C1”
		移动到“E3”
		移动到“H2”
		移动到“R1”
		移动到下一个“R”(铁路公司)
		移动到下一个“R”
		移动到下一个“U”(公共服务公司)
		后退三步
      */
     //抽机会卡
    public int Chance(int currentCell){
        int card = generator.nextInt(16);
        switch(card){
            case 1: currentCell = go; break; //go
            case 2: currentCell = jail; break; //jail
            case 3: currentCell = 11; break; //c1
            case 4: currentCell = 24; break; //e3
            case 5: currentCell = 39;break; //h2
            case 6: currentCell = 5; break; //r1
            case 7: currentCell = nextRR(currentCell); break; //next railway
            case 8: currentCell = nextRR(currentCell); break; //next railway
            case 9: currentCell = nextUtil(currentCell); break; //next utility
            case 10: currentCell = back3Squares(currentCell); break; //back 3 squares
            default:break; //其他无效果,不变
        }  
        return currentCell;
    }
    //移动到下一个“U”(公共服务公司)
    public int nextUtil(int currentCell)
    {
        if(currentCell >= 12 && currentCell < 28)
        {
            return 28;
        }
        else
        {
            return 12;
        }
    }
    //移动到下一个“R”
    public int nextRR(int currentCell){
        if(currentCell >= 35 || currentCell < 5)
        {
            return 5;
        }
        else if(currentCell >= 5 && currentCell < 15)
        {
            return 15;
        }
        else if(currentCell >= 15 && currentCell < 25)
        {
            return 25;
        }
        else
        {
            return 35;
        }
        
    }
    //后退三步
    //注意:特殊情况
    public int back3Squares(int currentCell){
        if(currentCell == 2){
            return 39;
        }
         //特殊情况
        else if(currentCell == ch3){
            return ComChest(currentCell);
        }
        else
        {
            return currentCell - 3;
        }
    }

    public static void main(String[] args) {
        Main m = new Main();
        //模拟一千万步
        int itterations = 10000000; 
        for(int i = 0; i < itterations; i++){
            m.Roll();
        }
        
        //打印百分比在3%以上的情况
        for(int i = 0; i < m.board.length; i++)
        {
            float percent = (float)m.board[i]/(float)itterations;
            percent *= 100;
            if(percent > 3)
            {
                System.out.println("位置:" + i + "占百分比: " + percent);
            }
        }
        //注意:每一次模拟都不一样,因为随机模拟
        System.out.println("连续3次投出两个骰子相同次数:"+m.numberOfTriples);
    }
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Java 实现大富翁游戏的完整源码: ```java import java.util.Random; import java.util.Scanner; public class Monopoly { private static final int BOARD_SIZE = 30; // 定义游戏板大小 private static final int MAX_ROUNDS = 50; // 定义最大回合数 private final int[] board; // 游戏板 private int currentPos; // 当前玩家位置 private int currentPlayer; // 当前玩家编号 private int currentRound; // 当前回合数 private final Random random = new Random(); // 随机数生成器 private final Scanner scanner = new Scanner(System.in); // 输入读取器 public Monopoly(int numPlayers) { board = new int[BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) { board[i] = random.nextInt(100); // 初始化游戏板 } currentPos = 0; currentPlayer = 1; currentRound = 1; System.out.printf("欢迎来到大富翁游戏!\n本次游戏有 %d 个玩家参与\n", numPlayers); } // 打印游戏板 private void printBoard() { System.out.print("游戏板:"); for (int i = 0; i < BOARD_SIZE; i++) { System.out.printf("%d ", board[i]); } System.out.println(); } // 掷骰子并移动玩家 private void rollDice() { int steps = random.nextInt(6) + 1; // 随机生成 1~6 的整数 System.out.printf("玩家 %d 掷出了 %d 点\n", currentPlayer, steps); currentPos = (currentPos + steps) % BOARD_SIZE; // 计算新位置 System.out.printf("玩家 %d 移动到了第 %d 格\n", currentPlayer, currentPos + 1); } // 处理该格子的事件 private void handleEvent() { int prize = board[currentPos]; // 获取奖金 System.out.printf("玩家 %d 获得了 %d 元奖金\n", currentPlayer, prize); } // 判断游戏是否结束 private boolean isGameOver() { if (currentRound > MAX_ROUNDS) { System.out.printf("游戏结束,最大回合数 %d 已达到\n", MAX_ROUNDS); return true; } if (currentPos == BOARD_SIZE - 1) { System.out.printf("玩家 %d 获胜!\n", currentPlayer); return true; } return false; } // 开始游戏 public void start() { while (true) { System.out.printf("第 %d 回合,轮到玩家 %d\n", currentRound, currentPlayer); printBoard(); rollDice(); handleEvent(); if (isGameOver()) { break; } currentPlayer = (currentPlayer % 2) + 1; // 切换到下一个玩家 currentRound++; System.out.println("按 Enter 键继续..."); scanner.nextLine(); // 等待用户输入 } } public static void main(String[] args) { Monopoly game = new Monopoly(2); // 创建一个有两个玩家的游戏 game.start(); } } ``` 运行该程序将启动一个大富翁游戏,有两个玩家参与。程序随机生成奖金和骰子点数,并在游戏板上移动玩家。当玩家到达终点或者回合数超过最大值时,游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值