Question: Typing Challenge Summary: Create the Typing Challenge program. The solution file should be named TypingChallenge_ExerciseE.rb. Use the instructions on page 144 of the textbook. Run

Typing Challenge Summary: Create the Typing Challenge program. The solution file should be named TypingChallenge_ExerciseE.rb. Use the instructions on page 144 of the textbook. Run your program until it works and the output looks nice.

Reinforcement Exercises The following exercises are designed to further your understanding of Ruby programming by challenging you to make improvements to the chapters game project, the Ruby Typing Challenge game.

Make the game more difficult by increasing the number of typing challenges from five to 10. Modify the program so that it takes six correctly typed sentences to pass. To implement this challenge, copy the last of the five state- ments that generate the games typing challenges and use it to add five additional challenges. You also need to modify the display_instructions method so that it tells the player there are 10 sentences instead of five. And you need to modify the Test classs determine_grade method so that it requires the player to type at least six sentences correctly in order to pass the challenge. Rather than using a pass/fail grading system, modify the game to assign grades of A, B, C, D, and F to the player using a 10-point grading scale. Make sure you use the case statement when implementing your solution. To implement this challenge, modify the Test classs determine_grade method using a case code block. Assign an A for nine or 10 correctly typed sentences, a B for eight, a C for seven, a D for six, and an F for five or fewer. 3. Add an explanation of how the test is graded. To implement this challenge, modify the test classs display_instructions method and rewrite the text accordingly. Provide immediate feedback for each mistyped sentence. To do so, modify the Test classs present_test method so that it informs the player a mistake has been made, then display the challenge sentence followed by the players sentence so the player can determine where the error lies.

# 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 typing test

class Test

#This method displays the 8-ball greeting message

def display_greeting

Console_Screen.cls #Clear the display area

#Display a welcome screen

print "\t\t Welcome to the Ruby Typing Challenge game!" +

" Press Enter to " +

"continue. : "

Console_Screen.pause #Pause the game

end

#Define a method to be used to present test instructions

def display_instructions

Console_Screen.cls #Clear the display area

puts "\t\t\tInstructions: " #Display a heading

#Display the game's instructions

puts %Q{ This test consists of a series of 5 typing challenges.

The challenge sentences are presented one at a time. To respond

correctly, you must retype each sentence exactly as shown and press

the Enter key. Your grade will be displayed at the end of the test.

Press Enter to continue. }

Console_Screen.pause #Pause the game

end

#Define a method to be used to present typing challenges

def present_test(challenge)

Console_Screen.cls #Clear the display area

print challenge + " : " #Display the challenge sentence

result = STDIN.gets #Collect the player's input

result.chop! #Remove the end of line marker

#Analyze the player input and see if it is correct

if challenge == result then

#Keep track of the number of correctly retyped challenge sentences

$noRight += 1

Console_Screen.cls #Clear the display area

#Keep the player informed

print "Correct! Press Enter to continue."

Console_Screen.pause #Pause the game

else

Console_Screen.cls #Clear the display area

#Keep the player informed

print "Incorrect! Press Enter to continue."

Console_Screen.pause #Clear the game

end

end

#Define a method to be used to display test results

def determine_grade

Console_Screen.cls #Clear the display area

#To pass the test the player must correctly retype 3 sentences

if $noRight >= 3 then

#Inform the player of the good news

print "You retyped " + $noRight.to_s + " sentence(s) correctly. "

puts "You have passed the typing test! Press Enter to continue."

else #The player has failed the test

#Inform the player of the bad news

print "You retyped " + $noRight.to_s + " sentence(s) correctly. "

puts "You have failed the typing test! Press Enter to continue."

end

end

end

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

#Initialize global variable that will be used to keep track of the number

#of correctly retyped sentences

$noRight = 0

Console_Screen = Screen.new #Instantiate a new Screen object

Typing_Test = Test.new #Instantiate a new Test object

#Execute the Test object's display_greeting method

Typing_Test.display_greeting

#Execute the Screen object's cls method in order to clear the screen

Console_Screen.cls

#Prompt the player for permission to begin the test

print "Would you like to test your typing skills? (y/n) : "

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

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

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

until answer == "y" || answer == "n"

Console_Screen.cls #Clear the display area

#Prompt the player for permission to begin the test

print "Would you like to test your typing skills? (y/n) : "

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

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

end

#Analyze the player's response

if answer == "n" #See if the player elected not to play

Console_Screen.cls #Clear the display area

#Invite the player to return and play again

puts "Okay, perhaps another time. "

else #The player wants to take the test

#Execute the Test object's display_instructions method

Typing_Test.display_instructions

#Display typing challenges and grade each answer by calling on the

#Test object's present_test method

Typing_Test.present_test "In the end there can be only one."

Typing_Test.present_test "Once a great plague swept across the land."

Typing_Test.present_test "Welcome to Ruby Principles of Ruby Programming"

Typing_Test.present_test "There are very few problems in the world " +

"that enough M&Ms cannot fix."

Typing_Test.present_test "Perhaps today is a good day to die. Fight " +

"beside me and let us die together."

#Notify the player of the results by executing the Test object's

#display_instructions method

Typing_Test.determine_grade

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Thank the player for taking the typing test

puts "Thank you for taking the Ruby Typing Challenge. "

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!