Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Modify the ruby virtual crazy 8-ball game so that, in addition to welcoming t

ID: 3700914 • Letter: 1

Question

1. Modify the ruby virtual crazy 8-ball game so that, in addition to welcoming the player at the beginning of the game, it displays instructions for how to play. Implement this challenge by modifying the text that is displayed by the Ball class's say_gretting methood

2. The ruby virtual crazy 8-ball game resopnds to player questions by randomly selecting and displaying one of six answers. Make the game more unpredictable by expanding the list of possible answers from six to 10. Implement this challenge by expanding the number of when statements from 6 to 10 in the ball class's get_fortune method. Make sure you also modify the get_fortune method so that it generates its random number within a range of one to 10.

3. The ruby virtual crazy 8-ball game allows the player to submit blank questions by simply pressing the Enter key. Disable this behavior, forcing the player to type something before pressing Enter.

4. Modify the game so that it displays your name and your school's URL in addition to a message thanking the player for playing the game. To implement this challenge, modify the Ball class's say_goodbye method by adding the specified text.

5. Modify the game so that the player sees the same closing information regardless of when the game ends. To do so, the Ball class's say_goodbye method will have to be called if the player decides not to play the game.


#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 "" #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 8-Ball
class Ball

#Define class properties for the 8-Ball
attr_accessor :randomNo, :greeting, :question, :goodbye
  
#Define a method to be used to generate random answers
def get_fortune
randomNo = 1 + rand(6)
  
#Assign an answer based on the randomly generated number
case randomNo
when 1
$prediction = "yes"
when 2
$prediction = "no"
when 3
$prediction = "maybe"
when 4
$prediction = "hard to tell. Try again"
when 5
$prediction = "unlikely"
when 6
$prediction = "unknown"
end
  
end

#This method displays the 8-Ball greeting message
def say_greeting
greeting = " Welcome to the Virtual Crazy 8-Ball game!" +
" Press Enter to " +
"continue. : "
print greeting
end
  
#This method displays the 8-Ball's primary query
def get_question
question = "Type your question and press the Enter key. : "
print question
end
  
#This method display the 8-Ball answers
def tell_fortune()
print "The answer is " + $prediction + ". : "
end

#This method displays the 8-Balls closing message
def say_goodbye
goodbye = "Thanks for playing the Virtual Crazy 8-Ball game! "
puts goodbye
end

end

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

Console_Screen = Screen.new #Initialize a new Screen object
Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game


answer = "" #Initialize variable that is used to control the game's first
#loop

#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 game
print "Would you like to have your fortune predicted? (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 has elected to play the game

#Initialize variable that is used to control the game's primary loop
gameOver = "No"

#Loop until the player decides to quit
until gameOver == "Yes"
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for prompting the player to ask a
#question
Eight_Ball.get_question
  
#Call upon the method responsible for generating an answer
Eight_Ball.get_fortune
  
Console_Screen.pause #Pause the game
  
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for telling the player the 8-Ball's
#answer
Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game
  
Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question
print "Press Enter to ask another question or type q to quit. : "

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

#Analyze the player's response
if answer == "q" #See if the player elected not to play
gameOver = "Yes" #The player wants to quit
end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player
Eight_Ball.say_goodbye
  
end

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

Console_Screen = Screen.new #Initialize a new Screen object
Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game


answer = "" #Initialize variable that is used to control the game's first
#loop

#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 game
print "Would you like to have your fortune predicted? (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 has elected to play the game

#Initialize variable that is used to control the game's primary loop
gameOver = "No"

#Loop until the player decides to quit
until gameOver == "Yes"
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for prompting the player to ask a
#question
Eight_Ball.get_question
  
#Call upon the method responsible for generating an answer
Eight_Ball.get_fortune
  
Console_Screen.pause #Pause the game
  
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for telling the player the 8-Ball's
#answer
Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game
  
Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question
print "Press Enter to ask another question or type q to quit. : "

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

#Analyze the player's response
if answer == "q" #See if the player elected not to play
gameOver = "Yes" #The player wants to quit
end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player
Eight_Ball.say_goodbye
  
end

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

Console_Screen = Screen.new #Initialize a new Screen object
Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game


answer = "" #Initialize variable that is used to control the game's first
#loop

#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 game
print "Would you like to have your fortune predicted? (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 has elected to play the game

#Initialize variable that is used to control the game's primary loop
gameOver = "No"

#Loop until the player decides to quit
until gameOver == "Yes"
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for prompting the player to ask a
#question
Eight_Ball.get_question
  
#Call upon the method responsible for generating an answer
Eight_Ball.get_fortune
  
Console_Screen.pause #Pause the game
  
  
Console_Screen.cls #Clear the display area
  
#Call upon the method responsible for telling the player the 8-Ball's
#answer
Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game
  
Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question
print "Press Enter to ask another question or type q to quit. : "

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

#Analyze the player's response
if answer == "q" #See if the player elected not to play
gameOver = "Yes" #The player wants to quit
end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player
Eight_Ball.say_goodbye
  
end

Explanation / Answer

#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 "" #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 8-Ball

class Ball

#Define class properties for the 8-Ball

attr_accessor :randomNo, :greeting, :question, :goodbye

#Define a method to be used to generate random answers

def get_fortune

randomNo = 1 + rand(10)

#Assign an answer based on the randomly generated number

case randomNo

when 1

$prediction = "yes"

when 2

$prediction = "no"

when 3

$prediction = "maybe"

when 4

$prediction = "hard to tell. Try again"

when 5

$prediction = "unlikely"

when 6

$prediction = "50-50"

when 7

$prediction = "40-50"

when 8

$prediction = "30-70"

when 9

$prediction = "20-80"

when 10

$prediction = "unknown"

end

end

#This method displays the 8-Ball greeting message

def say_greeting

greeting = " Welcome to the Virtual Crazy 8-Ball game!" +

"Instructions:"+

" 1. You can predict your future by typing a question about it."+

" 2. You have to press 'y' if you want to predict your future. Press 'n' otherwise"+

" Press Enter to continue. : " +

print greeting

end

#This method displays the 8-Ball's primary query

def get_question

question = "Type your question and press the Enter key. : "

print question

end

#This method display the 8-Ball answers

def tell_fortune()

print "The answer is " + $prediction + ". : "

end

#This method displays the 8-Balls closing message

def say_goodbye

goodbye = "Thanks for playing the Virtual Crazy 8-Ball game! " +

"YOUR_NAME_HERE YOUR_SCHOOL_URL_HER"

puts goodbye

end

end

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

Console_Screen = Screen.new #Initialize a new Screen object

Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game

answer = "" #Initialize variable that is used to control the game's first

#loop

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

print "Would you like to have your fortune predicted? (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

Eight_Ball.say_goodbye #Say good bye even if the user does't want to play the game

Console_Screen.cls #Clear the display area

#Invite the player to return and play again

puts "Okay, perhaps another time. "

else #The player has elected to play the game

#Initialize variable that is used to control the game's primary loop

gameOver = "No"

#Loop until the player decides to quit

until gameOver == "Yes"

Console_Screen.cls #Clear the display area

#Call upon the method responsible for prompting the player to ask a

#question

Eight_Ball.get_question

#Call upon the method responsible for generating an answer

Eight_Ball.get_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Call upon the method responsible for telling the player the 8-Ball's

#answer

Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question

print "Press Enter to ask another question or type q to quit. : "

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

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

#Analyze the player's response

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

gameOver = "Yes" #The player wants to quit

end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player

Eight_Ball.say_goodbye

end

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

Console_Screen = Screen.new #Initialize a new Screen object

Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game

answer = "" #Initialize variable that is used to control the game's first

#loop

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

print "Would you like to have your fortune predicted? (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 has elected to play the game

#Initialize variable that is used to control the game's primary loop

gameOver = "No"

#Loop until the player decides to quit

until gameOver == "Yes"

Console_Screen.cls #Clear the display area

#Call upon the method responsible for prompting the player to ask a

#question

Eight_Ball.get_question

#Call upon the method responsible for generating an answer

Eight_Ball.get_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Call upon the method responsible for telling the player the 8-Ball's

#answer

Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question

print "Press Enter to ask another question or type q to quit. : "

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

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

#Analyze the player's response

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

gameOver = "Yes" #The player wants to quit

end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player

Eight_Ball.say_goodbye

end

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

Console_Screen = Screen.new #Initialize a new Screen object

Eight_Ball = Ball.new #Initialize a new Ball object

Console_Screen.cls #Clear the display area

Eight_Ball.say_greeting #Call method responsible for greeting the player

Console_Screen.pause #Pause the game

answer = "" #Initialize variable that is used to control the game's first

#loop

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

print "Would you like to have your fortune predicted? (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 has elected to play the game

#Initialize variable that is used to control the game's primary loop

gameOver = "No"

#Loop until the player decides to quit

until gameOver == "Yes"

Console_Screen.cls #Clear the display area

#Call upon the method responsible for prompting the player to ask a

#question

Eight_Ball.get_question

#Call upon the method responsible for generating an answer

Eight_Ball.get_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Call upon the method responsible for telling the player the 8-Ball's

#answer

Eight_Ball.tell_fortune

Console_Screen.pause #Pause the game

Console_Screen.cls #Clear the display area

#Find out if the player wants to ask another question

print "Press Enter to ask another question or type q to quit. : "

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

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

#Analyze the player's response

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

gameOver = "Yes" #The player wants to quit

end

end

Console_Screen.cls #Clear the display area

#call upon the method responsible for saying goodbye to the player

Eight_Ball.say_goodbye

end