You should thoroughly test all your Ruby scripts to make surethey are running as expected. With the
You should thoroughly test all your Ruby scripts to make surethey are running as expected. With the | savvyessaywriters.org
You should thoroughly test all your Ruby scripts to make surethey are running as expected. With the Number Guessing game, thatmeans playing the game repeatedly. To make this easier on you, adda hidden “cheat” to the game that allows you to display the game’snumber. To implement this change, modify the game so that, inaddition to accepting a reply of y or n when prompting the playerfor permission to begin the game, it accepts a reply of c (forcheat). Next, modify the Game class’s play_game method so that itdisplays the game’s number when it’s run in cheat mode. ______________________________________________________________ #————————————————————————– # Description: This Ruby script is a number guessing game thatchallenges
# the player to guess a randomly generated number in 10
# guesses.
#
#————————————————————————– # Define custom classes————————————————— #Define a class representing the console window
class Screen def cls #Define a method that clears the display area
puts (” ” * 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 pausescript
#execution until the player presses the enter key
end end #Define a class representing the Ruby Number Guessing Game
class Game @@guesses = 0 def incrementGuesses
@@guesses += 1
end def getGuessesCount
return @@guesses
end def resetGuessesCount
@@guesses=0
end
#This method displays the game’s opening screen
def display_greeting Console_Screen.cls #Clear the display area #Display welcome message
print ” Welcome to the Ruby Number Guessing Game!” +
” Press 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: ” #Display a heading #Display the game’s instructions
puts “This game randomly generates a number from 1 to 1000and”
puts “challenges you to identify it in 10 guesses.”
puts “After each guess, the game will analyze your input andprovide”
puts “you with feedback. You may take 10 turns in”
puts “order to guess the game’s secret number. “
puts “Good luck! “
print “Press Enter to continue.” Console_Screen.pause #Pause the game end #Define a method that generates the game’s secret number
def generate_number #Generate and return a random number between 1 and 1000
return randomNo = 1 + rand(1000) end #Define a method to be used control game play
def play_game #Call on the generate_number method in order to get a randomnumber
number = generate_number #Loop until the player inputs a valid answer
loop do Console_Screen.cls #Clear the display area #Prompt the player to make a guess
print ” Enter your guess and press the Enter key: “
incrementGuesses
reply = STDIN.gets #Collect the player’s answer
reply.chop! #Remove the end of line character if reply != reply.to_i.to_s then
puts “Invalid input !!!”
puts “[NOTE] Please enter a number between 1 to 1000.”
Console_Screen.pause #Pause the game
redo #Redo the current iteration of the loop
end reply = reply.to_i #Convert the player’s guess to an integer #Validate the player’s input only allowing guesses between 1 and1000
if reply < 1 or reply > 1000 then
puts “Number out of range !!!”
puts “[NOTE] Valid numbers are from 1 to 1000. Please enter thevalid number between the range provided.”
Console_Screen.pause #Pause the game
redo #Redo the current iteration of the loop
end #Analyze the player’s guess to determine if it is correct
if reply == number && getGuessesCount == 10 then #Theplayer’s guess was correct
Console_Screen.cls #Clear the display area
print “You have guessed the number! Press enter to continue.”
Console_Screen.pause #Pause the game
break #Exit loop
elsif getGuessesCount == 10 then #If user reached 10 guesses
Console_Screen.cls #Clear the display area
print “You have reached maximum number of guesses! Press enter tocontinue.”
Console_Screen.pause #Pause the game
resetGuessesCount
break #Exit loop
elsif reply
Console_Screen.cls #Clear the display area
print “#{getGuessesCount} Guesses made out of 10 “
print “Your guess is too low! Press Enter to continue.”
Console_Screen.pause #Pause the game
elsif reply > number then #The player’s guess was too high
Console_Screen.cls #Clear the display area
print “#{getGuessesCount} Guesses made out of 10 “
print “Your guess is too high! Press Enter to continue.”
Console_Screen.pause #Pause the game
end end end #This method displays the information about the Ruby NumberGuessing Game
def display_credits Console_Screen.cls #Clear the display area #Thank the player and display game information
puts ” Thank you playing the Ruby Number Guessing Game. “
puts ” Developed by Jerry Lee Ford, Jr. “
puts ” Copyright 2010 “
puts ” URL: http://www.tech-publishing.com “ end end # Main Script Logic——————————————————- Console_Screen = Screen.new #Instantiate a new Screenobject
SQ = Game.new #Instantiate a new Game object #Execute the Game class’s display_greeting method
SQ.display_greeting answer = “” #Loop until the player enters y or n and do not accept any otherinput
loop do Console_Screen.cls #Clear the display area #Prompt the player for permission to start the game
print “Are you ready to play the Ruby Number Guessing Game? (y/n):” answer = STDIN.gets #Collect the player’s response
answer.chop! #Remove any extra characters appended to thestring #Terminate the loop if valid input was provided
break if answer == “y” || answer == “n” #Exit loop end #Analyze the player’s input
if answer == “n” #See if the player elected not to take thegame Console_Screen.cls #Clear the display area #Invite the player to return and play the game some othertime
puts “Okay, perhaps another time. “ else #The player wants to play the game #Execute the Game class’s display_instructions method
SQ.display_instructions loop do #Execute the Game class’s play_game method
SQ.play_game Console_Screen.cls #Clear the display area #Prompt the player for permission start a new round ofplay
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 thestring break if playAgain == “n” #Exit loop end #Call upon the Game class’s determine_credits method in order tothank
#the player for playing the game and to display gameinformation
SQ.display_credits end Attached
Do you need a similar assignment done for you from scratch? We have qualified writers to help you. We assure you an A+ quality paper that is free from plagiarism. Order now for an Amazing Discount!
Use Discount Code “Newclient” for a 15% Discount!
NB: We do not resell papers. Upon ordering, we do an original paper exclusively for you.
The post You should thoroughly test all your Ruby scripts to make surethey are running as expected. With the appeared first on Affordable Nursing Writers.