The game was created using a while loop that allows for the game to be replayed easily. The game asked for the user to input a number, 1 if the user guessed that the sum of the two dice would be over 7, they input 0, for under 7, input 1, and for equal to 7, input 2. This input was entered into an array of all of the user’s guesses at whatever index the while loop is on.
Two separate vectors were created for each die. Each die was “rolled” by using the “randi” function that generates a random integer between 1 and 6. These values were stored in separate arrays at the corresponding index values, and these values were then added together for a total of the roll. An if-elseif-else statement was then used to compare the user’s guess for the given round to the sum of the dice. If the guess matched the roll value, the user was informed that they were correct. If not, the user was informed that they were incorrect and shown the roll value.
To replay the game, the user was asked to input 1 for yes or 0 for no. This value was again stored in an array, with the index corresponding to the index of the while loop. If the user input 1, the while loop index was increased by 1, starting the game over. If they entered 0 or anything else, the game breaks from the while loop, ending the game.
- i – index of while loop (count of how many consecutive plays user has done)
- dice1 – array containing randomly generated die values of first dice
- dice2 – array containing randomly generated die values of first dice
- guess – array containing input user guesses
- s – array containing sum of dice
- play_again – array containing user input for whether or not they would like to play again