Simple Dice Game-
- Random numbers from 1-6 that the die could land on
x=Input (number): 1-6
- The guess that the player wants to use to guess number die will lie on
- The number of tries the player has taken to guess the random number generated on the die
MATLAB Game Commands:
An if statement starts the game when the player inputs a value that is less than 1 (x<=0) and else if greater than 6 (x>=7). There are 2 statements correlated to output if the player types in a number that’s not in the range of possible die numbers. When this happens nothing is added to the number of tries the player takes to guess the random roll of the die. The statements that are outputted have fprintf(‘Statement’) in its coding and there’s an end for the entire if statement. There is then a while statement in the coding listed after the if statement. This runs when the player finally inputs an x value within the range it was coded in. The while statement ensures that the game keeps running when the player doesn’t guess the correct random number on the die within the range of x(x~=die). An if statement follows it giving hint fprintf statements if x was above or below die. The variable ‘y’ has 1 added to it every time this while loop is completed or when the player takes another try at their guess. Two ‘end’s are used for both the while and if statement. After the while statement has ended there is an fprintf statement for when the player inputs the original number generated by die. This is when the game is completed and evaluates the number of tries the player took to get it correct. There’s an if statement that creates fprintf statements based on if the final y value was 1 or more.
- If the player’s input is less than or equal to 0 MATLAB produces the statement
“Are you kidding me..”-
- If the player’s input is above 6 then the code outputs this statement
“Your guess was too high, guess again”
- The statement following the input of a number from 1-6 that’s higher than the random number generated on the die
“Your guess was too low, guess again”
- The statement following the input of a number from 1-6 that’s lower than the random number generated on the die
If the player gets one of the 2 statements then y has 1 added to it
“Correct, you win!!”
- outputted if the player’s guess equals the random number generated on the die
- the player must input a 1 for yes or a 0 for no on whether they want to start the game
- 6 random numbers from 1-10
- the number that the player wants all the die to be
- how many times the dice have been rolled
- the number of die that match side
- number of die picked out of the total dice they begun with
n,i- tracks the specific number of roll that is completed.
The game starts by asking if the player wants to play the game in which they input a number either 1 or 0 to start the first if statement. For the first part of the game to work the player has to enter 1 and then rolll will equal the 10 random numbers from 1-6 generated on each die. The player then has to input a number from 1-6 that the player wants to keep as the ‘side’ variable. If the player inputs 0 then an fprinft statement is produced saying, “Oof well game over.” A return is at the end of the if statement before it ends to loop the player back to the beginning if the player inputs a 0 to end the game.
The next statement is an if statement within a for statement. The variable “i” keeps track of the number of “rolll” generated. If the roll produces numbers equal to side then the k, i, and dicerolled all increase by 1. If not then only “i” and “dicerolled” increase by 1. There are two “end’s” that end both the for and if statement. The number of rolls is equated to random numbers from 1-6 with a total number of random numbers being 10 minus the amount of dice that matched side.
A while statement makes sure that as long as there are dice left, then the dice is rolled. A for statement within it starts by defining the variable n as numbers from 1 to the number of dice left in each loop. An if statement follows to see whether the roll produces the side. The variable total then increases if the roll produces the side in how many ever dice. The total, k, n, and dicerolled is increased by 1. If the roll doesn’t produce the number assigned to side then only n and dicerolled increases by 1. A total of 3 ends are used to close the while, for, and if loop. Throughout this loop a fprintf statement is made to show how many more die is left to win with or whether the player has won.