Question

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.

$gameCount

• $noOfGuesses

• $totalNoOfGuesses

• $avgNoOfGuesses

Next, modify the Game class’s play game method by adding a statement that automatically increments the value assigned to $noOfGuesses. Finally, modify the programming logic (located in the loop at the end of the Main Script Logic section) that is responsible for prompting players to play again. The required modifications are outlined here:

• Increment $gameCount
• Calculate $totalNoOfGuesses
• Calculate $avgNoOfGuesses

Display two text strings showing the number of games played and the average number of guesses per game

________________________________________________________________________________________________

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

# Description: This Ruby script is a number guessing game that challenges
# 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 pause script
#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 1000 and"
puts "challenges you to identify it in 10 guesses."
puts "After each guess, the game will analyze your input and provide"
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 random number
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 and 1000
if reply < 1 or reply > 1000 then
puts "Number out of range !!!"
puts "[NOTE] Valid numbers are from 1 to 1000. Please enter the valid 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 #The player'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 to continue."
Console_Screen.pause #Pause the game
resetGuessesCount
break #Exit loop
elsif reply < number then #The player's guess was too low
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 Number Guessing 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 Screen object
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 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 the Ruby Number Guessing Game? (y/n): "

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

#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 the game

Console_Screen.cls #Clear the display area

#Invite the player to return and play the game some other time
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 of play
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

break if playAgain == "n" #Exit loop

end

#Call upon the Game class's determine_credits method in order to thank
#the player for playing the game and to display game information
SQ.display_credits

end

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

As per the given details the program should be as follows :

# 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 pause script

#execution until the player presses the enter key

end

end

#Define a class representing the Ruby Number Guessing Game

class Game

#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 100 and"

puts "challenges you to identify it in as few guesses as possible."

puts "After each guess, the game will analyze your input and provide"

puts "you with feedback. You may take as many turns as you need 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 100

return randomNo = 1 + rand(100)

end

#Define a method to be used control game play

def play_game

#Call on the generate_number method in order to get a random number

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

$noOfGuesses=$noOfGuesses.to_i+1.to_i

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

reply.chop! #Remove the end of line character

reply = reply.to_i #Convert the player's guess to an integer

#Validate the player's input only allowing guesses between 1 and 100

if reply < 1 or reply > 100 then

redo #Redo the current iteration of the loop

end

#Analyze the player's guess to determine if it is correct

if reply == number then #The player'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 reply < number then #The player's guess was too low

Console_Screen.cls #Clear the display area

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 "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 Number Guessing 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 Screen object

$gameCount = 0

$noOfGuesses = 0

$totalNoOfGuesses = 0

$avgNoOfGuesses = 0

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 other input

loop do

Console_Screen.cls #Clear the display area

#Prompt the player for permission to start the game

print "Are you rea

HOPE THIS MAY HELP YOU---

THANK YOU ……. PLEASE LIKE,IT WILL INCREASE MY SCORE,

HOPE YOU WILL ENCOURAGE ME…..

Add a comment
Know the answer?
Add Answer to:
Currently, the game allows players to play as many times as they wish. It does not...
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,...

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

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

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

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