In: Computer Science
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);
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