Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Topics: Writing a “classic” Program in MATLAB Game Specification: Write a progra

ID: 3759272 • Letter: T

Question

Topics: Writing a “classic” Program in MATLAB

Game Specification:

Write a program that allows a single Player (the user) to play a simple three dice game of chance against "The Odds".

There is a single player, with three six sided die.

The sides of each die are labeled with the numbers from 1 to 6, we will call this the value of the die.

A game is made up of rounds, a single round is played as such:

The player rolls their three dice – you may use randi()

The dice are displayed, in some reasonable format.

A determination is made as to whether or not the player won the round, this determination is made via the following rules:

A Triple is when all the dice have the same number on their top faces. If the player has any Triple then they win the round.

A Straight is when the numbers on the three dice faces can be arranged to form a consecutive sequence like 1 2 3 or 3 4 5 If the player has any Straight then they win the round.

A Pair is when any (exactly) two dice have the same number on their top faces. If the player has any Pair then they neither win nor lose the round.

A Junker is then anything that is not a Triple, a Straight or a Pair. If the player has any Junker then they lose the round.

The result of the round (with respect to the Player) is reported.

The player is asked if they wish to play another round.

Once the player indicates that they do not wish to play another round: Before exiting, the program displays a short report stating how many rounds were played, of those - how many were won and how many were lost.

Outline:

Create a MATLAB Script .m file

Write the necessary MATLAB commands to meet the game specification given above

Make sure to test your “program” when it is done

You really need to run your program a number of time to do this thoroughly

Notes(s):

You will need to use the

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

to assign a random die roll value.

Your Script will be many lines long

Sample Run(s):

Welcome to Computer Dice

-------------------------------------

You will first roll your dice

Next the outcome of your roll will

be determined:

any Triple and you Win

any Straight and you Win

any Pair and you just roll again

anything else and you Lose

-------------------------------------

Player

--------

2 5 1

Sorry, you lose!

Do you wish to play again [y, n] : 5

Do you wish to play again [y, n] : yes

Do you wish to play again [y, n] : y

Player

--------

2 2 2

Congrats, you win!

Do you wish to play again [y, n] : y

Player

--------

4 6 4

Its a push!

Do you wish to play again [y, n] : y

Player

--------

5 3 2

Sorry, you lose!

Do you wish to play again [y, n] : y

Player

--------

1 3 2

Congrats, you win!

Do you wish to play again [y, n] : n

Computer Dice Results

---------------------

You played 5 rounds

Rounds won :2

Rounds lost:2

---------------------

Explanation / Answer

function DiceGame( numb)

% receive player's rolled dice numbers

fprintf(1, ' '); % get a new line
fprintf( 1, ' Welcome to Computer Dice ');
fprintf( 1, ' -------------------------------------');
fprintf( 1, ' You will first roll your dice ');
fprintf( 1, ' Next the outcome of your roll will be determined:');
fprintf( 1, ' any Triple and Congrats, You win');
fprintf( 1, ' any Straight and Congrats, You win');
fprintf( 1, ' any Pair and you just roll again');
fprintf( 1, ' anything else and you Lose');
fprintf( 1, ' -------------------------------------');
% no need to receive the 3 dice value as input as we generate those numbers randomly
% n1 = input ('Enter the first dice value');
% n2 = input ('Enter the second dice value');
% n3 = input ('Enter the third dice value');
n1 = randi(6); % // a random integer from 1 to 6 inclusive
n2 = randi(6); % // a random integer from 1 to 6 inclusive
n3 = randi(6); % // a random integer from 1 to 6 inclusive
while usrIp == y
if ( n1 == n2 )   % it is a draw (neither win, nor loose), (pair) play againfprintf
   fprintf( 1, ' It is a push / Draw as you have a paired dice');
if ( n1 == n2 && n2 == n3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;

if ( n1 == 2 && n2 == 3 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 4 && n2 == 5 && n3 == 6 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
% 1 2 3 starts
elseif ( n1 == 1 && n2 == 2 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 1 && n2 == 3 && n3 == 2 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 2 && n2 == 1 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 2 && n2 == 3 && n3 == 1 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 1 && n3 == 2 )
   fprintf( 1, ' Congrats, You win')
elseif ( n1 == 3 && n2 == 2 && n3 == 1 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
% 1 2 3 ends
% 2 3 4 starts
elseif ( n1 == 4 && n2 == 2 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 4 && n2 == 3 && n3 == 2 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 2 && n2 == 4 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 2 && n2 == 3 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 4 && n3 == 2 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 2 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
% 2 3 4 ends

% 3 4 5 starts
elseif ( n1 == 4 && n2 == 5 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 4 && n2 == 3 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 5 && n2 == 4 && n3 == 3 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 5 && n2 == 3 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 4 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 5 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
%   3 4 5 ends

%   4 5 6 starts
elseif ( n1 == 4 && n2 == 5 && n3 == 6 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 4 && n2 == 6 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 5 && n2 == 4 && n3 == 6 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 5 && n2 == 6 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 6 && n2 == 4 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 6 && n2 == 5 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
else disp(' Sorry, You loose');
%    4 5 6 ends

% an efficient alternative to the above lengthy coding
% the sum of 3 consecutive numbers = 6,9,12,15
% that is 1+2+3 = 6;     2+3+4 = 9;
% 3 + 4 + 5 = 12;        4 + 5 + 6 = 15;
% ofcourse if 3 dice falls as 3 3 3 then the sum is 9 but that is a win by a different rule as well
% hence it works
sum = n1 + n2 + n3;
if ( sum == 6 || sum == 9 || sum == 12 || sum == 15 )   % this line can replace 48 lines of above code
   % disp(' Congrats, You win')
   fprintf(' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 4 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 1 && n3 == 2 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;

if ( n1 == 2 && n2 == 3 && n3 == 4 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 3 && n2 == 4 && n3 == 5 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
elseif ( n1 == 4 && n2 == 5 && n3 == 6 )
   fprintf( 1, ' Congrats, You win')
   wonRnds = wonRnds + 1;
else
   disp(' Sorry, You loose')
   lostRnds = lostRnds + 1;

disp(' Do you wish to play again [y,n]: ')
usrIp=input();
rndCnt = rndCnt + 1;

end % end of while usr Input == y
disp(' You played ')
disp ( rndCnt )
disp(' rounds')
disp(' Rounds won: ')
disp(wonRnds)
disp(' Rounds lost: ')
disp(lostRnds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote