Question

In: Computer Science

Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY...

Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY dice game for any amount of players, keep scores for all the players, clearly show the game progress for every round, every player, and every roll in the command window, automatically ends the game when the first player accumulates 50 points or more game score, and automatically select the game winner and the winning game score.

i have coded most of the game, i need help with " any amount of players" how can i make the game work for as many players as user wants.

clc

clear

x1 = randi ([ 1,6],1);

x2 = randi ([ 1,6],1);

vec = [x1,x2];

s = sort (vec);

score =0;

round =1;

fprintf ('\tHello and welcome to your dice game!\n')

fprintf ('\tLets play, roll your dice !\n')

fprintf ('\tYou can choose as many players as you want!')

fprintf ('\t------------------------------------\n\n\n')

fprintf ('If you roll double 6, you get 25 points.\n')

fprintf ('If you roll a double 3 you go back to 0 points.\n')

fprintf ('Any other double other than 3, or 6, will get you 5 points.\n' )

fprintf ('The first to get to 50 points wins.\n')

%input = numOfPlayers ('enter how many players');

disp('Your dice numbers are: ')

disp (s)

fprintf ('This is the: %.f round \n and the score is: %.f \n First roll was: %.f\n and seccond roll was: %.f' ,round ,score ,x1 ,x2)

while score ~= 50;

if s(1) == 3 && s(2) == s(1) ;

disp (' youre score has gone back to 0')

score = 0;

elseif s(1) == 6 && s(2) == s(1)

disp ('You just got 25 points')

score= (score+25);

elseif s(1) == s(2);

disp ('You get 5 points !')

score = (score +5);

elseif score > 50

disp ('game is over you won your score is: %.f ! ',score)

else disp (' You did not get points, play again !')

end

if score ~= 50

x1 = randi ([ 1,6],1);

x2 = randi ([ 1,6],1);

vec = [x1,x2];

s = sort (vec);

round = round + 1;

fprintf('This is the: %.f round \n\t and the score is: %.f \n\t First roll was: %.f\n\t and seccond roll was: %.f' ,round ,score ,x1 ,x2 ) ;

end

end

fprintf('Your score is: %.f you won !', score);

Solutions

Expert Solution

Please find the matlab Code for variable number of players which is asked as console input

************************************************CODE STARTS************************************************************************

clc

clear

% information for the game

fprintf ('\tHello and welcome to your dice game!\n')

fprintf ('\tLets play, roll your dice !\n')

fprintf ('\tYou can choose as many players as you want!')

fprintf ('\t------------------------------------\n\n\n')

fprintf ('If you roll double 6, you get 25 points.\n')

fprintf ('If you roll a double 3 you go back to 0 points.\n')

fprintf ('Any other double other than 3, or 6, will get you 5 points.\n' )

fprintf ('The first to get to 50 points wins.\n')

% Ask for Input

numOfPlayers = input ('enter how many players: ');

% numOfPlayers = 2;

% Vector to keep scores

score = zeros(1,numOfPlayers);

%when someone reaches score 50 we will make this variable 1

gameOver = 0;

%count round number

roundCounter = 1;

%until game is completed

while gameOver ~= 1

X = sprintf('Starting Round %d', roundCounter);

disp(X)

disp(' ')

%give chance to each player

for player = 1:numOfPlayers

disp(' ')

X = sprintf('Player %d playing',player);

disp(X)

x1 = randi ([ 1,6],1);

x2 = randi ([ 1,6],1);

vec = [x1,x2];

s = sort (vec);

X = sprintf('Player %d rolled dice1=%d AND dice2=%d',player, s(1), s(2));

disp(X)

if s(1) == 3 && s(2) == s(1)

disp (' youre score has gone back to 0')

score(player) = 0;

elseif s(1) == 6 && s(2) == s(1)

disp ('You just got 25 points')

score(player) = (score(player) + 25);

elseif s(1) == s(2)

disp ('You get 5 points !')

score(player) = (score(player) + 5);

end

% newScore = calculateScore(scores(player), s);

if score(player) >= 50

X = sprintf ('game is over Player %d has Won with score %d! ',player, score(player));

disp(X)

gameOver = 1;

break

end

X = sprintf('Player %d score is %d', player, score(player));

disp(X)

end

roundCounter = roundCounter + 1;

end

************************************************************CODE ENDS************************************************

*************************************************OUTPUTS STARTS ************************************************************

Since length of the output of this code depends on how quickly a player reaches 50. I have pasted the codes outputs

Hello and welcome to your dice game!

Lets play, roll your dice !

You can choose as many players as you want! ------------------------------------

If you roll double 6, you get 25 points.

If you roll a double 3 you go back to 0 points.

Any other double other than 3, or 6, will get you 5 points.

The first to get to 50 points wins.

enter how many players:

3

Starting Round 1

Player 1 playing

Player 1 rolled dice1=1 AND dice2=3

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=4 AND dice2=5

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=1 AND dice2=2

Player 3 score is 0

Starting Round 2

Player 1 playing

Player 1 rolled dice1=5 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=1 AND dice2=4

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=3 AND dice2=6

Player 3 score is 0

Starting Round 3

Player 1 playing

Player 1 rolled dice1=1 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=3 AND dice2=3

youre score has gone back to 0

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=5 AND dice2=6

Player 3 score is 0

Starting Round 4

Player 1 playing

Player 1 rolled dice1=4 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=3 AND dice2=6

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=4 AND dice2=4

You get 5 points !

Player 3 score is 5

Starting Round 5

Player 1 playing

Player 1 rolled dice1=3 AND dice2=5

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=4 AND dice2=6

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=4 AND dice2=6

Player 3 score is 5

Starting Round 6

Player 1 playing

Player 1 rolled dice1=3 AND dice2=3

youre score has gone back to 0

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=3 AND dice2=5

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=2 AND dice2=4

Player 3 score is 5

Starting Round 7

Player 1 playing

Player 1 rolled dice1=1 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=4 AND dice2=6

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=2 AND dice2=5

Player 3 score is 5

Starting Round 8

Player 1 playing

Player 1 rolled dice1=2 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=3 AND dice2=4

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=2 AND dice2=3

Player 3 score is 5

Starting Round 9

Player 1 playing

Player 1 rolled dice1=4 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=2 AND dice2=3

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=4 AND dice2=5

Player 3 score is 5

Starting Round 10

Player 1 playing

Player 1 rolled dice1=5 AND dice2=6

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=1 AND dice2=1

You get 5 points !

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=5

Player 3 score is 5

Starting Round 11

Player 1 playing

Player 1 rolled dice1=1 AND dice2=3

Player 1 score is 0

Player 2 playing

Player 2 rolled dice1=4 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=4 AND dice2=5

Player 3 score is 5

Starting Round 12

Player 1 playing

Player 1 rolled dice1=1 AND dice2=1

You get 5 points !

Player 1 score is 5

Player 2 playing

Player 2 rolled dice1=2 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=5

Player 3 score is 5

Starting Round 13

Player 1 playing

Player 1 rolled dice1=4 AND dice2=4

You get 5 points !

Player 1 score is 10

Player 2 playing

Player 2 rolled dice1=3 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=5

Player 3 score is 5

Starting Round 14

Player 1 playing

Player 1 rolled dice1=2 AND dice2=2

You get 5 points !

Player 1 score is 15

Player 2 playing

Player 2 rolled dice1=4 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=6

Player 3 score is 5

Starting Round 15

Player 1 playing

Player 1 rolled dice1=2 AND dice2=5

Player 1 score is 15

Player 2 playing

Player 2 rolled dice1=5 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=5

Player 3 score is 5

Starting Round 16

Player 1 playing

Player 1 rolled dice1=2 AND dice2=2

You get 5 points !

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=5 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=2

You get 5 points !

Player 3 score is 10

Starting Round 17

Player 1 playing

Player 1 rolled dice1=3 AND dice2=4

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=5 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=2

You get 5 points !

Player 3 score is 15

Starting Round 18

Player 1 playing

Player 1 rolled dice1=2 AND dice2=3

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=2 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=6

Player 3 score is 15

Starting Round 19

Player 1 playing

Player 1 rolled dice1=4 AND dice2=6

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=3 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=4

Player 3 score is 15

Starting Round 20

Player 1 playing

Player 1 rolled dice1=2 AND dice2=5

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=2 AND dice2=4

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=5 AND dice2=6

Player 3 score is 15

Starting Round 21

Player 1 playing

Player 1 rolled dice1=1 AND dice2=6

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=1 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=4

Player 3 score is 15

Starting Round 22

Player 1 playing

Player 1 rolled dice1=2 AND dice2=3

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=3 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=2

Player 3 score is 15

Starting Round 23

Player 1 playing

Player 1 rolled dice1=1 AND dice2=5

Player 1 score is 20

Player 2 playing

Player 2 rolled dice1=3 AND dice2=4

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=4 AND dice2=5

Player 3 score is 15

Starting Round 24

Player 1 playing

Player 1 rolled dice1=2 AND dice2=2

You get 5 points !

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=6

Player 3 score is 15

Starting Round 25

Player 1 playing

Player 1 rolled dice1=1 AND dice2=3

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=3 AND dice2=4

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=4

Player 3 score is 15

Starting Round 26

Player 1 playing

Player 1 rolled dice1=4 AND dice2=5

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=4

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=2

Player 3 score is 15

Starting Round 27

Player 1 playing

Player 1 rolled dice1=3 AND dice2=6

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=3 AND dice2=3

youre score has gone back to 0

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=3 AND dice2=4

Player 3 score is 15

Starting Round 28

Player 1 playing

Player 1 rolled dice1=2 AND dice2=4

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=5

Player 2 score is 0

Player 3 playing

Player 3 rolled dice1=3 AND dice2=5

Player 3 score is 15

Starting Round 29

Player 1 playing

Player 1 rolled dice1=1 AND dice2=2

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=2

You get 5 points !

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=6

Player 3 score is 15

Starting Round 30

Player 1 playing

Player 1 rolled dice1=5 AND dice2=6

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=4

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=6

Player 3 score is 15

Starting Round 31

Player 1 playing

Player 1 rolled dice1=3 AND dice2=6

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=4 AND dice2=6

Player 3 score is 15

Starting Round 32

Player 1 playing

Player 1 rolled dice1=2 AND dice2=6

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=6

Player 3 score is 15

Starting Round 33

Player 1 playing

Player 1 rolled dice1=3 AND dice2=4

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=6

Player 3 score is 15

Starting Round 34

Player 1 playing

Player 1 rolled dice1=2 AND dice2=5

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=4

Player 3 score is 15

Starting Round 35

Player 1 playing

Player 1 rolled dice1=5 AND dice2=6

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=3 AND dice2=6

Player 3 score is 15

Starting Round 36

Player 1 playing

Player 1 rolled dice1=1 AND dice2=2

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=6

Player 3 score is 15

Starting Round 37

Player 1 playing

Player 1 rolled dice1=1 AND dice2=4

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=3

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=5 AND dice2=6

Player 3 score is 15

Starting Round 38

Player 1 playing

Player 1 rolled dice1=1 AND dice2=5

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=2 AND dice2=5

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=5

Player 3 score is 15

Starting Round 39

Player 1 playing

Player 1 rolled dice1=1 AND dice2=5

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=2

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=1 AND dice2=4

Player 3 score is 15

Starting Round 40

Player 1 playing

Player 1 rolled dice1=2 AND dice2=3

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=1 AND dice2=2

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=4 AND dice2=5

Player 3 score is 15

Starting Round 41

Player 1 playing

Player 1 rolled dice1=4 AND dice2=5

Player 1 score is 25

Player 2 playing

Player 2 rolled dice1=5 AND dice2=6

Player 2 score is 5

Player 3 playing

Player 3 rolled dice1=2 AND dice2=4

Player 3 score is 15

Starting Round 42

Player 1 playing

Player 1 rolled dice1=6 AND dice2=6

You just got 25 points

game is over Player 1 has Won with score 50!

*************************************************OUTPUTS ENDS ************************************************************

Please comment for any further assistance


Related Solutions

Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud , with additional features that can: • Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player. • Greet the player when the game starts. • Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs...
Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
We will simulate a dice game in which 2 dice are thrown. If the roll is...
We will simulate a dice game in which 2 dice are thrown. If the roll is 7 or 11, you win. If the roll is 2, 3, or 12, you lose If the roll is any other value, it establishes a point. If with a point established, that point is rolled again before a 7, you win. If, with a point established, a 7 is rolled before the point is rolled again you lose. Build your algorithm incrementally. First write...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Write a modular program to simulate the Game of Life and investigate the patterns produced by...
Write a modular program to simulate the Game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array; and still others may produce ‘gliders’ that detach themselves from the society and sail off into space! Since the game requires an array of cells that continually expands/shrinks, you would want to use multi-dimensional arrays and...
write c program to generate 6 numbers to simulate rolling of a dice . use while...
write c program to generate 6 numbers to simulate rolling of a dice . use while loop to run 100 and 10000 times. print out how many times it generates 1, 2,3,4,5,6.
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT