Rock Paper Scissors

The beginning of the code declares numerical values for rock, paper, and scissors so the three options of the game can be used later in the code as simple integers. Next, the player is given $100 to start off with. A majority of the code for the game is in a while loop that continues iterations as long as the play_again variable is equal to 1, so the play_again variable is set to 1 to allow the while loop to run for the first time. Inside the while loop, the player is asked to input their move using the input command. After this, some technicalities are addressed. An if command is used to determine if the player does not have $0, which if they have more than $0, the player is prompted to make a bet using an input command. To check to make sure if the player did not bet more money than they have, another if command is used. If the player’s bet is greater than their total amount of money, they are asked to input another valid bet using an input command. Finally, if the player has $0, they are told that they cannot bet and the code proceeds. Next, the computer’s move is determined by using a randi command that randomly chooses an integer from 1 to 3. The computer’s move is then displayed in the command window using the correlating rock, paper, and scissors values. Next, the variables player_win and computer_win are set to 0. These variables are used to determine the player’s monetary gain/loss at the end. When the player wins, player_win is set to 1 and when the computer wins, computer_win is set to 1. However, both need to be reset to 0 before the outcome is determined to make sure only one of these variables is equal to 1. Next, the outcome of the game is determined based on the moves of the computer and the player. The outcome of the game is printed in the command window and an image is displayed using the imshow command, providing a more visual result of the game. Also, player_win and computer_win are set to 1 depending on whether the player or computer won. Next, the monetary gain/loss of the player is determined using an if command. If the player’s money total is not equal to 0, another if command is used to determine if the player lost or gained money, or neither. If player_win is equal to 1, meaning the player won, the amount of money the player bet is added to their money total. If computer_win is equal to 1, meaning the computer won, the amount of money the player bet is subtracted from their money total. Otherwise, neither player_win nor computer_win are equal to 1, meaning a tie took place. The player’s money total remains the same in this case. If the player’s money total is equal to 0, it remains as such. Next, the player’s new money total is printed into the command window. Integer values are then given to the words yes and no, where yes is equal to 1 and no is equal to 0. The player is then prompted if they want to play again, where they answer either yes or no. The play_again variables is then determined to be either 1 (yes) or 0 (no) using an input command. If the player said yes, play_again is equal to 1 and the while loop iterates again. If the player said no, play_again is equal to 0 and the while loop is ended.

 

Variables Used

Paper & paper = The integer value for the move paper, which was set to 1.

Rock & rock = The integer value for the move rock, which was set to 2.

Scissors & scissors = The integer value for the move scissors, which was set to 3.

Player_money = The amount of money that the player has.

Play_again = A variable used to determine if the game needed to be replayed using a while loop. If the player wanted to play again, this was set to 1 and the game was replayed.

Player_move = The move of the player, which was 1, 2 or 3 depending on if paper, rock or scissors was selected, respectively.

Player_bet = The amount of money that the player wants to bet on the game.

Computer_move = The move of the computer, which was 1, 2 or 3 depending on if paper, rock or scissors was selected, respectively.

Player_win = Used to determine if the player has won. If this is set to 1, the player has won.

Computer_win = Used to determine if the computer has won. If this is set to 1, the computer has won.

Yes = Integer value for if the player says yes, which was set to 1.

No = Integer value for if the player says yes, which was set to 1.

 

Commands Used

While Loop – A loop that iterates commands inside of it for as long as a certain condition is true.

If-Else – A command that performs an action if a condition is true, otherwise will perform another action else if another condition is true. This can be repeated for as many else ifs as needed.

Input – A string of text is printed into the command window, usually a question, where the user is asked to type something in which is then defined to a variable.

Fprintf – Prints a string of text into the command window.

Imshow – Displays an image from a file in the current selected folder.