Question

In: Computer Science

MATLAB code for the following question. In the game of Monopoly, a pair of dice are...

MATLAB code for the following question.

In the game of Monopoly, a pair of dice are rolled to move a player's piece around the board. If a double is rolled (the dice show the same number), the player receives another roll of the dice. If a double is rolled a second time, a third roll of the dice is received. If a double is rolled on the third occasion, the player forfeits their turn (and goes to Jail). Write a program which simulates two players having 50 turns each (a turn is however many dice rolls a player has in a row before it's the other player's turn) and determines:

a) the total number of spaces moved by each player; and

b) how many times a player rolls a double which results in movement of a piece.

Use the randi function.

For fun, incorporate the rule that a player cannot leave Jail (cannot move) unless they either roll a double or three turns have passed without rolling a double.

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Code:

player1Turns=0;
player2Turns=0;
totalSpacePlayer1=0;
totalSpacePlayer2=0;
totalDoublesPlayer1=0;
totalDoublesPlayer2=0;
turn=1;
timesOfDouble1=0;
timesOfDouble2=0;
jailed1=false;
jailed2=false;
turnsInjail1=0;
turnsInjail2=0;
while player2Turns<50
if turn == 1
player1Turns=player1Turns+1;
dice1=randi(6);
dice2=randi(6);
fprintf("%d : %d %d Player 1\n",player1Turns,dice1,dice2);
if jailed1
if (dice1==6 && dice2==6) || turnsInJail1==2
jailed1=false;
turnsInJail1=0;
fprintf("Player 1 released");
else
turnsInJail1=turnsInJail1+1;
end
else
if dice1==6 && dice2==6
if timesOfDouble1==2
turn=2;
jailed1=true;
timesOfDouble1=0;
fprintf("Player 1 jailed");
else
timesOfDouble1=timesOfDouble1+1;
player1Turns=player1Turns-1;
end
else
if timesOfDouble1~=0
totalDoublesPlayer1=totalDoublesPlayer1+dice1+dice2;
timesOfDouble1=0;
end
totalSpacePlayer1=totalSpacePlayer1+dice1+dice2;
turn = 2;
end
end
  
else
player2Turns=player2Turns+1;
dice1=randi(6);
dice2=randi(6);
fprintf("%d : %d %d Player 2\n",player1Turns,dice1,dice2);
if jailed2
if (dice1==6 && dice2==6) || turnsInJail2==2
jailed2=false;
turnsInJail2=0;
else
turnsInJail2=turnsInJail2+1;
end
else
if dice1==6 && dice2==6
if timesOfDouble2==2
turn=2;
jailed2=true;
timesOfDouble2=0;
else
timesOfDouble2=timesOfDouble2+1;
player2Turns=player2Turns-1;
end
else
if timesOfDouble2~=0
totalDoublesPlayer2=totalDoublesPlayer2+dice1+dice2;
timesOfDouble2=0;
end
totalSpacePlayer2=totalSpacePlayer2+dice1+dice2;
turn = 1;
end
end
end
end
fprintf("Space moved by 1: %d\n",totalSpacePlayer1);
fprintf("Space moved by 2: %d\n",totalSpacePlayer2);
fprintf("Double result in move player 1: %d\n",totalDoublesPlayer1);
fprintf("Double result in move player 2: %d\n",totalDoublesPlayer2);

Output:

More results:


Related Solutions

You pay $1 to play a game. The game consists of rolling a pair of dice....
You pay $1 to play a game. The game consists of rolling a pair of dice. If you observe a sum of 7 or 11 you receive $4. If not, you receive nothing. Compute the expected value and standard deviation for this game?
A gambler plays a dice game where a pair of fair dice are rolled one time...
A gambler plays a dice game where a pair of fair dice are rolled one time and the sum is recorded. The gambler will continue to place $2 bets that the sum is 6, 7, 8, or 9 until she has won 7 of these bets. That is, each time the dice are rolled, she wins $2 if the sum is 6, 7, 8, or 9 and she loses $2 each time the sum is not 6, 7, 8, or...
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are lots of ways to win: (1, 6), (2, 5), and so on. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people’s eyes glaze over at...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots...
In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are many ways to win: (1, 6), (2, 5), and soon. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people's eyes glaze over at the first...
Suppose that a game of chance is played with a pair of fair 10-sided dice (with...
Suppose that a game of chance is played with a pair of fair 10-sided dice (with the sides numbered 1 to 10). In the game, you can pick any number from 1 to 10 and the two dice are then “rolled” in a cage. If $1 is bet and exactly one of the number that you picked is rolled you win $1, and if both of the dice are the number that you picked you win $20 (in each of...
You are rolling a pair of balanced dice in a board game. Rolls are independent. You...
You are rolling a pair of balanced dice in a board game. Rolls are independent. You land in a danger zone that requires you to roll doubles (both faces show the same number of spots) before you are allowed to play again. 1. What is the probability of rolling doubles on a single toss of the dice? A) 25/36 B) 5/36 C) 1/6 D) 1/36 2. What is the probability that you do not roll doubles on the first toss,...
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice,...
for monopoly board game, Suppose you are on “GO”. On your next roll of the dice, what is the probability that: a. You land on Jail b. You land on any Light Blue property (Connecticut Ave, Vermont Ave, or Oriental Ave.) c. You land on Mediterranian Ave d. You land on Chance or Community Chest 2. Suppose you are on “Jail” a. What space (or spaces) is/are most likely to be landed on on your next roll? Justify your response....
In the carnival game​ Under-or-Over-Seven, a pair of fair dice is rolled​ once, and the resulting...
In the carnival game​ Under-or-Over-Seven, a pair of fair dice is rolled​ once, and the resulting sum determines whether the player wins or loses his or her bet. For​ example, using method​ one, the player can bet $2.00 that the sum will be under​ 7, that​ is, 2,​ 3, 4,​ 5, or 6. For this​ bet, the player wins $2.00 if the result is under 7 and loses $2.00 if the outcome equals or is greater than 7.​ Similarly, using...
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum....
Consider the game consisting of rolling a pair of fair 6-sided dice and recording the sum. It will cost you $1 to play the game. If the sumis less than 5, then you will win $3. However, you do not get your $1 back so your profit is $2. If you roll a sum of exactly8, thenyou will win $4. However, you do not get your $1 back so your profit is $3. Otherwise, you lose your $1. A.What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT