Question: I need help with my MATLAB code. The games score does not come out right for the computer vs player stuck in the mud game.

I need help with my MATLAB code. The games score does not come out right for the computer vs player stuck in the mud game. Here are the instructions for the game and at the bottom is my code. Stuck in the mud is a popular dice game in UK. The game uses five (5) 6-sided dice to play. The players play in turns. Choose one player to start the game. The player will roll all five (5) dice. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2 or a 5 becomes stuck in the mud. If this throw does not contain any 2s or 5s, the score is incremented by the sum of the dice values. The player needs to set aside any 2s and 5s and throw the remaining dice. Again, if any 2s or 5s are rolled, the score will not be incremented for this throw. Throws without 2s and 5s are added to the previous total score. Continue in this way until all the dice are stuck. Save the score and pass the dice to the next player. Players can agree a total number of rounds to play in advance. Total up the score. The player with the highest score wins the game. Write a MATLAB program to simulate the Stuck in the Mud game 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 like 0, 1.2, -1, 999, and so on... The program should not play if the user enters a 0 or any negative value. The program should accurately play the number of rounds specified by the user. The player and the computer play in turns for each round. The program can always pick one side to start the game first, either the player side or the computer side. Randomly pick a side to start the rotation is optional. Print the current round number clearly in the command window. If the player side starts first, the program will automatically roll all five (5) dice for the player. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2 or a 5 becomes stuck in the mud. If this throw does not contain any 2s or 5s, the score is incremented by the sum of the dice values. The player needs to set aside any 2s and 5s and throw the remaining dice. Again, if any 2s or 5s are rolled, the score will not be incremented for this throw. Throws without 2s and 5s are added to the previous total score. Continue in this way until all the dice are stuck. The dice rolled for the player, the stuck dice, and the scores during the process should clearly be printed in the command window. The program then automatically roll all five (5) dice for the computer. Follow the game rules until all five (5) dice are stuck. The dice rolled for the computer, the stuck dice, and the scores during the process should also be clearly printed in the command window. Accurately track the total scores for the player and the computer. After all the rounds have been played, select a winner based on the highest total score. It is also possible that the game ends in a tie.Here is my code. Thank you for your help.

disp('Games do not make you violent, lag does. Got lag? Kill the lag with a dice game.'); disp('Enter the number of rounds to play: '); rounds = input('');

if(rounds <= 0) disp('The game ends with a tie'); quit end

player_total_score=0; comp_total_score=0;

for i=1:rounds fprintf('ROUND %d !!! ',i); n = 5; roll = 1; mud = []; player_score = 0; while n > 0 fprintf(' ROLL %d ',roll);

roll = roll+1; r = randi([1 6],1,n); fprintf('Rolling: '),disp(r);

count2 = sum(r == 2); count5 = sum(r == 5); if (count2 > 0) || (count5 >0) mud = [ mud , repmat(2,1,count2) repmat(5,1,count5)] ; n =n - count2-count5; else player_score = player_score+ sum(r); end fprintf('Stuck in the mud :');disp(mud); fprintf('game score: %d ',player_score);

fprintf('Number of dice stuck: %d ',size(mud,2));

end player_total_score = player_total_score+ player_score;

fprintf('The player scores: %d ',player_score); fprintf('The Player Total scored: %d ',player_total_score); %computer turn disp(' The computer goes next '); comp_score =0; n = 5; roll = 1; mud = []; while n > 0 fprintf(' ROLL %d ',roll);

roll = roll+1; r = randi([1 6],1,n); fprintf('Rolling: '),disp(r);

count2 = sum(r == 2); count5 = sum(r == 5); if (count2 > 0) || (count5 >0) mud = [ mud , repmat(2,1,count2) repmat(5,1,count5)] ; n =n - count2-count5; else player_score = player_score+ sum(r); end fprintf('Stuck in the mud :');disp(mud); fprintf('game score: %d ',comp_score);

fprintf('Number of dice stuck: %d ',size(mud,2));

end comp_total_score = comp_total_score+ comp_score; fprintf('The Computer scores: %d ',comp_score); fprintf('The Computer Total scored: %d ',comp_total_score); end if(player_total_score > comp_total_score) disp(' The computer wins!'); elseif (player_total_score < comp_total_score) disp(' The computer wins!'); else disp(' The game ends with a tie'); end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!