Question

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 takes time and can lead to typos, however. Simplify game play by modifying the get_player_move method to also allow single- character input in the form of R, P, and S. Make sure you accommodate both uppercase and lowercase input.

__________________________________________________________________________________________________

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

#

# Script Name: RPS.rb

# Version: 1.0

# Author: Jerry Lee Ford, Jr.

# Date: March 2010

#

# 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] [Paper] [Scissors]: "

@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

break if @choice =~ /Rock|Paper|Scissors/i

end

#Convert the player move to upper case and return it to the calling

#statement

return @choice.upcase

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. "

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

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

#please modify the get play function as below to get required result.

#comments provided for details

#Sample output provided for the particular case please go through it . Thank You

#Thank you#######

def get_player_move

Console_Screen.cls #Clears the screen display area

loop do #Loop begin forever

Console_Screen.cls #Clears the screen display area

#Provide instruction and prompt the user to make a choice.

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

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

#variable created and initialized with with empty.

@choice=""

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

#Remove any extra characters appended to

@choice.chop!


#Terminate the loop if valid input was provided

#converted to upper case before comparing
@[email protected]

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

end

#based on the entered value it converts into to string accordingly


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


#statement

return @choice


end

To make a move, type one of the following and press Enter: [Rock (R) [Paper (P) [scissors (S)1: s RESULTS: Players move: sCI

Add a comment
Know the answer?
Add Answer to:
I need help finishing this excerise this is what I have:   I get this output for an error: RPS_ExerciseE1.rb:171:in `+&#...
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
  • 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,...

  • 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...

  • 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...

  • 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...

  • 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...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

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