% Initialize the index for arrays as 1.
% While loop set to run for all index values greater than or equal to
% 1. By turning each component of the game into a vector, the game can
% be replayed as many times as the user wants. The while loop allows
% for each corresponding value in each array to be compared for
% Each die is “rolled” using the randi function, generating two random
% numbers between 1 and 6. Each “roll” is stored in the corresponding index
% position according to the index of the pass in the while loop.
fprintf(‘Guess if the sum of the two dice when rolled will be a over, under, or equal to 7.\n Enter a 0 for over 7, 1 for under 7, and 2 for equal to 7.\n’)
guess(i)=input(‘Enter your guess: ‘);
% User guess is stored in a vector.
% Sum of dice also stored as a vector. Rolls of each dice of corresponding
% index added together.
if s(i)>7 && guess(i)==0
% If sum of dice at index value is greater than 7 and the user guessed
% greater than 7 (input 0 for this round), user guessed correct.
% Command window cleared after each round to avoid cluttering window
% for user.
fprintf(‘You guessed correct, the value is over 7!\n’)
elseif s(i)<7 && guess(i)==1
fprintf(‘You guessed correct, the value is under 7!\n’)
% If sum of dice at index value is less than 7 and the user guessed
% less than 7 (input 1 for this round), user guessed correct.
elseif s(i)==7 && guess(i)==2
fprintf(‘You guessed correct, the value is equal to 7!\n’)
% If sum of dice at index value is equal to 7 and the user guessed
% equal to 7 (input 2 for this round), user guessed correct.
fprintf(‘You guessed incorrect. The sum of the dice was %i.\n’,s(i))
% If guess does not equal sum of dice, user is informed of roll number
% and that they were incorrect.
play_again(i)=input(‘Would you like to play again? Enter 1 for yes, or 0 for no:’);
% User is asked if they would like to play again. This was again created as
% a vector to correspond to each round.
% If user chooses to play again (input 1 for this pass), index is
% incremented by 1 to move to next pass of while loop.
fprintf(‘Thanks for playing!’)
% If user chooses not to play again (input 0), break out of while loop
% and display “Thanks for playing!”. This finishes the game.