Rock Paper Scissors



Paper = 1;

paper = 1;

Rock = 2;

rock = 2;

Scissors = 3;

scissors = 3;

%Set numerical values to rock paper and scissors so they can used use later

%in the code.


player_money = 100;

%Give the player 100 dollars to begin with.


play_again = 1;

%Set the variable play_again to 1 so the game can play initially.


while play_again == 1

%A while loop is used with a majority of the game code in it because this

%allows for the game to be played over and over again.

    player_move = input(‘\nRock, Paper, Scissors, shoot! What is your move? ‘);

    %Have the player input their move.


    if player_money ~= 0

        fprintf(‘You have $%.0f.\n’,player_money)

        player_bet = input(‘How much money would you like to bet? ‘);

        %If the player has money, they can proceed to bet what they like.

        if player_bet > player_money

            fprintf(‘Invalid bet, you do not have that much money!\n’)

            player_bet = input(‘Please input a valid bet: ‘);

            %If the player bets more money than what they have, they will

            %be prompted to bet again.



        fprintf(‘You dont have any money, and therefore cannot bet.\n’)

        %If the player has no money, they will told that they cannot bet.



    computer_move = randi([1 3],1,1);

    %The computers move is determined by choosing rock, paper, or scissors



    if computer_move == 1

        fprintf(‘Computers move is Paper.\n’)

    elseif computer_move == 2

        fprintf(‘Computers move is Rock.\n’)


        fprintf(‘Computers move is Scissors.\n’)


    %The computers move is printed out so the player knows what the

    %computer did.


    player_win = 0;

    computer_win = 0;

    %These variables are declared as 0 before the outcome is determined to

    %make sure the variable associated to the loser is equal to 0.


    if player_move == 3 && computer_move == 1

        fprintf(‘Scissors cut paper! Player wins! :)\n’)


        player_win = 1;

    elseif player_move == 3 && computer_move == 2

        fprintf(‘Rock crushes scissors! Computer wins! :(\n’)


        computer_win = 1;

    elseif player_move == 2 && computer_move == 1

        fprintf(‘Paper covers rock! Computer wins! :(\n’)


        computer_win = 1;

    elseif player_move == 2 && computer_move == 3

        fprintf(‘Rock crushes scissors! Player wins! :)\n’)


        player_win = 1;

    elseif player_move == 1 && computer_move == 2

        fprintf(‘Paper covers rock! Player wins! :)\n’)


        player_win = 1;

    elseif player_move == 1 && computer_move == 3

        fprintf(‘Scissors cut paper! Computer wins! :(\n’)


        computer_win = 1;

    else player_move == computer_move;

        fprintf(‘Same move! Tie!\n’)



    %The outcome of the game is determined, with each outcome displaying

    %who won in the command window, as well as showing an image that

    %displays the outcome in a more visual way. Also, the player_win and

    %computer_win is set to 1 if that respective player has won. This is

    %used for determining how much money is lost.


    if player_money ~= 0

        if player_win == 1

            player_money = player_money + player_bet;

            %If the player wins the game, they gain the money they bet on

            %top of their original amount of money.

        elseif computer_win == 1

            player_money = player_money – player_bet;

            %If the player loses, they lose the money they bet.


            player_money = player_money;

            %If there is a tie, the player does not gain or lose any money.



        player_money == 0;

        %If the player has no money to begin with, their money total is set

        %to 0.



    fprintf(‘You have $%.0f.\n’,player_money)

    %The players new money total is displayed.


    yes = 1;

    Yes = 1;

    no = 0;

    No = 0;

    %Numerical values for yes and no are determined


    play_again = input(‘Would you like to play again? ‘);

    %The player is asked if they would like no play again. If yes, the

    %play_again variable is set to 1, and the while loop loops for another

    %iteration of the game. If no, the play_again variable is set to 0 and

    %the game ends.