Over 7/Under 7 Code Description

List of variables:

money

bet

val

roll

functions:

randi() – creates a vector of length 2 with values ranging from 1 to 6

The variable “money” is used to store the amount the user wants to start with. “state” is used as a binary variable. the while loop runs until “state=1” if the value of “state” is changed the loop breaks. “bet” stores the amount the user wants to bet. If the bet is more than the money the user currently has or is less than 0 a message of “Invalid Input” is displayed to the screen. TheContinue command causes the loop to return to its starting position. The variable “val” contains the value after the user has been asked to enter 1,0 or 2 for over seven, under seven or equal to 7 respectively. “roll” is a vector that contains the value of the rolled dice. The values of the rolled dice are obtained from the command “Randi”. The two values inside “roll” are added and are reassigned to “roll”.

Next. The winning condition is check which is if the “roll”>7 and the user has entered 1 or if the “roll” <7 and the user has entered 0. When this happens “bet” is added to “money”. “Else if” is a statement which is used to only check if the first “If” statement is not met. The next condition which is checked is if the “roll” =7 and the user entered 2. when this happens the betting, amount is quadrupled(“bet*4”) and added to the “money”. Else the user’s guess is wrong and the “bet” is subtracted from “money”. The next condition checks whether the total money is <=0. If this condition is met the variable “state” is set to 0 to end the while loop. In the end “Game over” is displayed to the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *