Question

The game lets players know whether they have won, lost, or tied a particular game. Modify...

The game lets players know whether they have won, lost, or tied a particular game. Modify the game so the players are told the number of games they have won, lost or tied since the start of game play. Implement this change by adding three variables named $wins, $lost, and $ties to the program's Main Script Logic section, assigning each an initial value of 0. Next, modify the analyze_results method by adding programming logic that increments the values of $wins, $lost, and $ties, depending on the results of game play. Lastly, modify the display_results method so that it displays the values of $wins, $lost, and $ties each time it executes. At the end of each round of play, the game analyzes the player's and the computer's input and determines the result. A message is the displayed, informing the player whether the game was won, lost, or tied. It is up to the player, however, to determine how that result was computed, which is done by comparing the player's and the computer's moves, both of which are displayed on the screen, to the game's rules.

#--------------------------------------------------------------------------
#
# Script Name: RPS_ExerciseE.rb
# Version: 1.0
# Author: x
# Date: x
#
# Description: This Ruby game s a computerized version of the classic
# children’s Rock, Paper, Scissors game, in which the player goes
# head-to-head against the computer.
#
#--------------------------------------------------------------------------


# Define custom classes ---------------------------------------------------

#Define a class representing the console window
class Screen

def cls #Define a method that clears the display area
puts ("\n" * 25) #Scroll the screen 25 times
puts "\a" #Make a little noise to get the player's attention
end
  
def pause #Define a method that pauses the display area
STDIN.gets #Execute the STDIN class's gets method to pause script
#execution until the player presses the enter key
end
  
end

#Define a class representing the Rock, Paper, scissors game
class Game

#This method displays the game's opening message
def display_greeting
  
Console_Screen.cls #Clear the display area
  
#Display welcome message
print "\t\t\tLet's Play Rock, Paper, Scissors!" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +
"continue. "
  
Console_Screen.pause #Pause the game

end

#Define a method to be used to present game instructions
def display_instructions
  
Console_Screen.cls #Clear the display area
puts "INSTRUCTIONS:\n\n" #Display a heading

#Display the game's instructions
puts "This game pits the player against the computer. To play, you must"
puts "enter one of the following moves when prompted: Rock, Paper, or"
puts "Scissors.\n\n"
puts "The game will randomly select a move for the computer, and "
puts "the two moves will then be analyzed according to the following"
puts "rules: \n\n"
puts "* Rock crushes Scissors, Rock equals Rock, and Rock is covered by"
puts " Paper.\n\n"
puts "* Paper covers Rock, Paper equals Paper, and Paper is cut by"
puts " Scissors.\n\n"
puts "* Scissors cut Paper, Scissors equals Scissors, and Scissors are"
puts " crushed by Rock.\n\n\n"
puts "Good luck!\n\n\n"
print "Press Enter to continue. "
Console_Screen.pause #Pause the game
  
end

#Define a method to control game play
def play_game

Console_Screen.cls #Clear the display area
  
#Call on the method responsible for collecting the player's move
playerMove = get_player_move

#Call on the method responsible for generating the computer's move
computerMove = get_computer_move

#Call on the method responsible for determining the results of the game
result = analyze_results(playerMove, computerMove)
  
#Call on the method responsible for displaying the results of the game
display_results(playerMove, computerMove, result)
  
end

#Define the method responsible for collecting the player's move
def get_player_move

Console_Screen.cls #Clear the display area

loop do #Loop forever

Console_Screen.cls #Clear the display area

#Prompt the player to select a move
puts "To make a move, type one of the following and press Enter:\n\n"
print "[Rock(R)] [Paper(P)] [Scissors(S)]:""\a"

@choice = STDIN.gets #Collect the player's answer
@choice.chop! #Remove any extra characters appended to
#the string

#Terminate the loop if valid input was provided
@[email protected]
break if @choice =~ /R|P|S/i

end
  
if @choice == "R" then #if R entered then Rock chosen
  
@choice = "ROCK"
  
elsif @choice == "P" then #if P entered then Paper chosen
  
@choice = "PAPER"
elsif @choice == "S" then #if S entered Scissors chosen
  
@choice = "SCISSORS"
  
end

return@choice
  
end
  
  
#Define the method responsible for making the computer's move
def get_computer_move
  
#Define and array containing a list of three possible moves
moves = ["ROCK", "PAPER", "SCISSORS"]
  
#Generate and return a random number between 0 and 2
randomNo = rand(3)
  
#Return a randomly selected move to the calling statement
return moves[randomNo]
  
end
  
#Define the method responsible for analyzing and returning the result of
#the game (arguments are passed as upper case characters)
def analyze_results(player, computer)
  
#Analyze the results of the game when the player selects ROCK
if player == "ROCK" then
return "Player wins!" if computer == "SCISSORS"
return "Tie!" if computer == "ROCK"   
return "Computer wins!" if computer == "PAPER"
end
  
#Analyze the results of the game when the player selects PAPER
if player == "PAPER" then
return "Player wins!" if computer == "ROCK"
return "Tie!" if computer == "PAPER"   
return "Computer wins!" if computer == "SCISSORS"
end
  
#Analyze the results of the game when the player selects SCISSORS
if player == "SCISSORS" then
return "Player wins!" if computer == "PAPER"
return "Tie!" if computer == "SCISSORS"   
return "Computer wins!" if computer == "ROCK"
end

end

#Define the method responsible for displaying the result of the game
def display_results(player, computer, result)
  
#Display arguments passed to the method using the following template
Console_Screen.cls #Clear the display area
puts "\n\n\t\t\tRESULTS:"
puts "\n\n\t\t\t================================"
puts "\n\n\t\t\tPlayer's move: " + player
puts "\n\n\t\t\tComputer's move: " + computer
puts "\n\n\t\t\tResult: " + result
puts "\n\n\t\t\t================================"
puts "\n\n\n\n"
print "Press Enter to continue. ""\a"
Console_Screen.pause #Pause the game
  
end

#This method displays the information about the Rock, Paper,
#Scissors game
def display_credits
  
Console_Screen.cls #Clear the display area
  
#Thank the player and display game information
puts "\t Thank you for playing the Rock, Paper, Scissors game.\n\n\n\n"
puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"
puts "\t\t\t\t Copyright 2010\n\n"
puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"

end

end


# Main Script Logic -------------------------------------------------------

Console_Screen = Screen.new #Instantiate a new Screen object
RPS = Game.new #Instantiate a new Game object

#Execute the Game class's display_greeting method
RPS.display_greeting

$gameCount = 0
answer = "" #Initialize variable and assign it an empty string

#Loop until the player enters y or n and do not accept any other input
loop do

Console_Screen.cls #Clear the display area

#Prompt the player for permission to start the game
print "Are you ready to play Rock, Paper, Scissors? (y/n): "

answer = STDIN.gets #Collect the player's answer
answer.chop! #Remove any extra characters appended to the string

#Terminate the loop if valid input was provided
break if answer =~ /y|n/i

end

#Analyze the player's answer
if answer =~ /n/i #See if the player wants to quit

Console_Screen.cls #Clear the display area

#Invite the player to return and play the game some other time
puts "Okay, perhaps another time.\n\n"

else #The player wants to play the game

#Execute the game class's display_instructions method
RPS.display_instructions

playAgain = ""

loop do #Loop forever
  
#Execute the Game class's play_game method
RPS.play_game

loop do #Loop forever

Console_Screen.cls #Clear the display area

#Find out if the player wants to play another round
print "Would you like to play again? (y/n): "

playAgain = STDIN.gets #Collect the player's response
playAgain.chop! #Remove any extra characters appended to the string
  
#Terminate the loop if valid input was provided
break if playAgain =~ /n|y/i
  
end
  
#Terminate the loop if valid input was provided
break if playAgain =~ /n/i
  
end
  
#Call upon the Game class's determine_credits method
RPS.display_credits
  
end

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In case of any query, do comment. Please rate answer. Thanks

Please use modified code (also highlighted in bold here and with RED in screen shots) as below:

#--------------------------------------------------------------------------

#

# Script Name: RPS_ExerciseE.rb

# Version: 1.0

# Author: x

# Date: x

#

# Description: This Ruby game s a computerized version of the classic

# children’s Rock, Paper, Scissors game, in which the player goes

# head-to-head against the computer.

#

#--------------------------------------------------------------------------

# Define custom classes ---------------------------------------------------

#Define a class representing the console window

class Screen

    def cls #Define a method that clears the display area

        puts ("\n" * 25) #Scroll the screen 25 times

        puts "\a" #Make a little noise to get the player's attention

    end

    def pause #Define a method that pauses the display area

        STDIN.gets #Execute the STDIN class's gets method to pause script

        #execution until the player presses the enter key

    end

end

#Define a class representing the Rock, Paper, scissors game

class Game

    #This method displays the game's opening message

    def display_greeting

        Console_Screen.cls #Clear the display area

        #Display welcome message

        print "\t\t\tLet's Play Rock, Paper, Scissors!" +

        "\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +

        "continue. "

        Console_Screen.pause #Pause the game

    end

    #Define a method to be used to present game instructions

    def display_instructions

        Console_Screen.cls #Clear the display area

        puts "INSTRUCTIONS:\n\n" #Display a heading

       

        #Display the game's instructions

        puts "This game pits the player against the computer. To play, you must"

        puts "enter one of the following moves when prompted: Rock, Paper, or"

        puts "Scissors.\n\n"

        puts "The game will randomly select a move for the computer, and "

        puts "the two moves will then be analyzed according to the following"

        puts "rules: \n\n"

        puts "* Rock crushes Scissors, Rock equals Rock, and Rock is covered by"

        puts " Paper.\n\n"

        puts "* Paper covers Rock, Paper equals Paper, and Paper is cut by"

        puts " Scissors.\n\n"

        puts "* Scissors cut Paper, Scissors equals Scissors, and Scissors are"

        puts " crushed by Rock.\n\n\n"

        puts "Good luck!\n\n\n"

        print "Press Enter to continue. "

        Console_Screen.pause #Pause the game

    end

    #Define a method to control game play

    def play_game

        Console_Screen.cls #Clear the display area

         

        #Call on the method responsible for collecting the player's move

        playerMove = get_player_move

       

        #Call on the method responsible for generating the computer's move

        computerMove = get_computer_move

       

        #Call on the method responsible for determining the results of the game

        result = analyze_results(playerMove, computerMove)

         

        #Call on the method responsible for displaying the results of the game

        display_results(playerMove, computerMove, result)

    end

    #Define the method responsible for collecting the player's move

    def get_player_move

   

        Console_Screen.cls #Clear the display area

       

        loop do #Loop forever

       

        Console_Screen.cls #Clear the display area

       

        #Prompt the player to select a move

        puts "To make a move, type one of the following and press Enter:\n\n"

        print "[Rock(R)] [Paper(P)] [Scissors(S)]:""\a"

       

        @choice = STDIN.gets #Collect the player's answer

        @choice.chop! #Remove any extra characters appended to

        #the string

       

        #Terminate the loop if valid input was provided

        @[email protected]

        break if @choice =~ /R|P|S/i

              end

        if @choice == "R" then #if R entered then Rock chosen

         

            @choice = "ROCK"

         

        elsif @choice == "P" then #if P entered then Paper chosen

         

            @choice = "PAPER"

        elsif @choice == "S" then #if S entered Scissors chosen

         

            @choice = "SCISSORS"

  

        end

        return@choice

    end

    #Define the method responsible for making the computer's move

    def get_computer_move

        #Define and array containing a list of three possible moves

        moves = ["ROCK", "PAPER", "SCISSORS"]

         

        #Generate and return a random number between 0 and 2

        randomNo = rand(3)

         

        #Return a randomly selected move to the calling statement

        return moves[randomNo]

    end

    #Define the method responsible for analyzing and returning the result of

    #the game (arguments are passed as upper case characters)

    def analyze_results(player, computer)

        #Analyze the results of the game when the player selects ROCK

      if player == "ROCK" then

            if computer == "SCISSORS" then

                $wins = $wins +1

                return "Player wins!"

            end

            if computer == "ROCK" then

                $ties = $ties +1

                return "Tie!"

            end

            if computer == "PAPER" then

                $lost = $lost +1

                return "Computer wins!"

            end

        end

        #Analyze the results of the game when the player selects PAPER

        if player == "PAPER" then

            if computer == "ROCK" then

               $wins = $wins +1

               return "Player wins!"

            end

            if computer == "PAPER" then

                $ties = $ties +1

                return "Tie!"

            end

            if computer == "SCISSORS" then

               $lost = $lost +1

                return "Computer wins!"

            end

        end

        #Analyze the results of the game when the player selects SCISSORS

        if player == "SCISSORS" then

            if computer == "PAPER" then

               $wins = $wins +1

                return "Player wins!"

            end

            if computer == "SCISSORS" then

                $ties = $ties +1

                return "Tie!"   

            end

            if computer == "ROCK" then

                $lost = $lost +1

                return "Computer wins!"

            end

           

        end

    end

    #Define the method responsible for displaying the result of the game

    def display_results(player, computer, result)

        #Display arguments passed to the method using the following template

        Console_Screen.cls #Clear the display area

        puts "\n\n\t\t\tRESULTS:"

        puts "\n\n\t\t\t================================"

        puts "\n\n\t\t\tPlayer's move: " + player

        puts "\n\n\t\t\tComputer's move: " + computer

        puts "\n\n\t\t\tResult: " + result

        puts "\n\n\t\t\tNo of Wins: #{$wins}"

        puts "\n\n\t\t\tNo of Lost: #{$lost}"

        puts "\n\n\t\t\tNo of Ties: #{$ties}"

        puts "\n\n\t\t\t================================"

        puts "\n\n\n\n"

        print "Press Enter to continue. ""\a"

        Console_Screen.pause #Pause the game

    end

    #This method displays the information about the Rock, Paper,

    #Scissors game

    def display_credits

    Console_Screen.cls #Clear the display area

     

    #Thank the player and display game information

    puts "\t Thank you for playing the Rock, Paper, Scissors game.\n\n\n\n"

    puts "\n\t\t\t Developed by Jerry Lee Ford, Jr.\n\n"

    puts "\t\t\t\t Copyright 2010\n\n"

    puts "\t\t\tURL: http://www.tech-publishing.com\n\n\n\n\n\n\n\n\n\n"

    end

end

# Main Script Logic -------------------------------------------------------

#added variables for wins, ties and lost

$wins = 0

$lost = 0

$ties = 0

Console_Screen = Screen.new #Instantiate a new Screen object

RPS = Game.new #Instantiate a new Game object

#Execute the Game class's display_greeting method

RPS.display_greeting

$gameCount = 0

answer = "" #Initialize variable and assign it an empty string

#Loop until the player enters y or n and do not accept any other input

loop do

Console_Screen.cls #Clear the display area

#Prompt the player for permission to start the game

print "Are you ready to play Rock, Paper, Scissors? (y/n): "

answer = STDIN.gets #Collect the player's answer

answer.chop! #Remove any extra characters appended to the string

#Terminate the loop if valid input was provided

break if answer =~ /y|n/i

end

#Analyze the player's answer

if answer =~ /n/i #See if the player wants to quit

Console_Screen.cls #Clear the display area

#Invite the player to return and play the game some other time

puts "Okay, perhaps another time.\n\n"

else #The player wants to play the game

#Execute the game class's display_instructions method

RPS.display_instructions

playAgain = ""

loop do #Loop forever

#Execute the Game class's play_game method

RPS.play_game

loop do #Loop forever

Console_Screen.cls #Clear the display area

#Find out if the player wants to play another round

print "Would you like to play again? (y/n): "

playAgain = STDIN.gets #Collect the player's response

playAgain.chop! #Remove any extra characters appended to the string

#Terminate the loop if valid input was provided

break if playAgain =~ /n|y/i

end

#Terminate the loop if valid input was provided

break if playAgain =~ /n/i

end

#Call upon the Game class's determine_credits method

RPS.display_credits

End

=========screen shot of the code==============

main.ro #---------------- # 1 2 3 # Script Name: RPS_ExerciseE.rb 4 # Version: 1.0 5 # Author: x 6 # Date: x 7 # Description:

main.rb puts INSTRUCTIONS:\n\n #Display a heading #Display the games instructions puts This game pits the player against

main.rb print [Rock(R)] [Paper(P)] [Scissors(S)]:\a @choice = STDIN. gets #Collect the players answer @choice.chop! #Rem

main.rb #Define the method responsible for analyzing and returning the result of #the game (arguments are passed as upper cas

#Dejine the method responsible for displaying the result of the game def display_results(player, computer, result) main.rb 20

main.rb 240 # mal scrup Log ---- 241 $wins = 0 242 $lost = 0 243 $ties = 0 244 Console Screen = Screen.new #Instantiate a new

Output:

input Lets Play Rock, Paper, Scissors! Press Enter to continue.

ve input Are you ready to play Rock, Paper, Scissors? (y/n): y

input INSTRUCTIONS: This game pits the player against the computer. To play, you must enter one of the following moves when p

input To make a move, type one of the following and press Enter: [Rock (R)] [Paper (P)] [Scissors (S)]:R

input RESULTS: Players move: ROCK Computers move: SCISSORS Result: Player wins! No of Wins: 1 No of Lost: 0 No of Ties: 0 P

Press Enter to continue. Would you like to play again? (y/n): Y

To make a move, type one of the following and press Enter: [Rock (R)] [Paper (P)] [Scissors (S)]:P

input RESULTS: ============================ Players move: PAPER Computers move: PAPER Result: Tie! No of Wins: 1 No of Lost

Press Enter to continue. Would you like to play again? (y/n): Y

vig To make a move, type one of the following and press Enter: [Rock (R)] [Paper (P)] [Scissors (S)]:s

RESULTS: Players move: SCISSORS Computers move: SCISSORS Result: Tie! No of Wins: 1 No of Lost: 0 No of Ties: 2 Press Enter

vis input Press Enter to continue. Would you like to play again? (y/n): n

Thank you for playing the Rock, Paper, Scissors game. Developed by Jerry Lee Ford, Jr. Copyright 2010 URL: http://www.tech-pu

Add a comment
Know the answer?
Add Answer to:
The game lets players know whether they have won, lost, or tied a particular game. Modify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help finishing this excerise this is what I have:   I get this output for an error: RPS_ExerciseE1.rb:171:in `+&#...

    I need help finishing this excerise this is what I have:   I get this output for an error: RPS_ExerciseE1.rb:171:in `+': no implicit conversion of nil into String (TypeError) from RPS_ExerciseE1.rb:171:in `display_results' from RPS_ExerciseE1.rb:89:in `play_game' from RPS_ExerciseE1.rb:240:in `block in <main>' from RPS_ExerciseE1.rb:237:in `loop' from RPS_ExerciseE1.rb:237:in `<main>' Currently, the Game class’s get_player_move method uses a regular expression to validate the player’s moves of Rock, Paper, or Scissors, rejecting any input other than one of these words. Making the player enter entire words...

  • Currently, the game allows players to play as many times as they wish. It does not...

    Currently, the game allows players to play as many times as they wish. It does not provide any feedback on how the players are doing, however. Modify the game so that it keeps track of the number of games played as well as the average number of guesses made per game. To implement this change, add the following list of global variables to the beginning of the script’s Main Script Logic section. Assign each variable an initial value of zero....

  • Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method...

    Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method so that it informs the player a mistake has been made, then display the challenge sentence followed by the player’s sentence so the player can determine where the error lies. #-------------------------------------------------------------------------- #  # Script Name: TypingChallenge.rb # Version:     1.0 # Author:      Jerry Lee Ford, Jr. # Date:        March 2010 #  # Description: This Ruby script demonstrates how to apply conditional logic  #              in order...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • 5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may...

    5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may recognize Rock Paper Scissors Lizard Spock as a game of chance that expands on the standard Rock Paper Scissors game. It introduces two new hand signs and several more rules. The rules: • Scissors cuts Paper • Paper covers Rock • Rock crushes Lizard • Lizard poisons Spock • Spock smashes Scissors • Scissors decapitates Lizard • Lizard eats Paper • Paper disproves Spock...

  • Python please. I have a working one that doesn't keep track of w/l ratio, it may...

    Python please. I have a working one that doesn't keep track of w/l ratio, it may be helpful to see how others did the entire program and inserted that module. Write a modular program that let the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 thru 3 is generated but do not display the computer choice immediately. Number 1...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT