The game’s welcome screen and credits screen are also stored astext strings within the program file. Externalize the content ofthese two screens in separate text files named BJWelcome.txt andBJCredits.txt. Once these files have been created, remove thedisplay_greeting and display_credits methods from the program file.Next, change the name of the get_help_file to get_file and modifythe method so that it accepts an argu- ment named filename,representing the name of an external text file. Modify the methodso that it retrieves and displays the con- tents of the text filepassed to it as an argument. Next, modify the Main Script Logicsection by replacing the statement that called on the get_help_filewith a statement that calls on the get_file method.
Make sure this new statement passes the full name and path of thetext file to be displayed. Finally, replace the two statements thatcall on the display_greeting and display_credits methods withstatements that call on the get_file method and pass it the name ofthe appropriate external text fil
# Define custom classes—————————————————
#Define a class representing the console window
class Screen
def cls #Define a method that clears thedisplay area
puts (“n” * 25) #Scroll the screen 25 times
puts “a” #Make a little noise to get the player’sattention
end
def pause #Define a method that pauses thedisplay 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 Blackjackgame
class Game
#This method displays the game’s opening message
def display_greeting
Console_Screen.cls #Clear the display area
#Display a welcome message
print “tttWelcome to the Ruby Blackjack Game!” +
“nnnnnnnnnnnnnnPress Enter to ” +
“continue. ”
Console_Screen.pause #Pause the game
end
#Define a method to be used to display gameinstructions
def display_instructions
Console_Screen.cls #Clear the displayarea
puts “INSTRUCTIONS:nn” #Display a heading
#Display the game’s instructions
puts “This game is based on the Blackjack card game, wherethe”
puts “objective is to beat the dealer by acquiring cards thattotal”
puts “higher than the dealer’s cards without going over 21. Inthis”
puts “version, the player and dealer are each dealt an initialcard. The”
puts “player is then prompted to draw additional cards. Theplayer”
puts “may draw as many additional cards as desired, as long asthe”
puts “player’s hand adds up to less than 21. If the player’shand goes”
puts “over 21, the player busts and the dealerautomatically”
puts “wins. Once the player decides to hold, it is thedealer’s”
puts “turn. The dealer continues to add new cards to hishand”
puts “until it adds up to 17 or more or the dealer busts. Oncethe”
puts “dealer’s hand is complete, the game analyzes the player’shand”
puts “and the dealer’s hand to determine the results of thegame.”
puts “nnnnnnn”
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
#Assist the player and dealer an initialstarting card
playerHand = get_new_card
dealerHand = get_new_card
#Call the method responsible for dealingnew cards to the player
playerHand = complete_player_hand(playerHand, dealerHand)
#If the player has not gone bust, call themethod responsible for managing
#dealer’s hand
if playerHand <= 21then
dealerHand = play_dealer_hand(dealerHand)
end
#call the method responsible for determining the results ofthe game
determine_winner(playerHand, dealerHand)
end
#Define a method responsible for dealing a new card
def get_new_card
#Assign a random number between 1 and 13as the value of the card being
#created
card = 1 + rand(13)
#A value of 1 is an ace, so reassign thecard a value of 11
return 11 if card == 1
#A value of 10 or more equals a face card so reassign thecard a value
#of 10
return 10 if card >= 10
return card #Return the value assigned tothe new card
end
#Define a method responsible for dealing the rest of theplayer’s hand
def complete_player_hand(playerHand,dealerHand)
loop do #Loop forever
Console_Screen.cls #Clear the displayarea
#Show the current state of the player anddealer’s hands
puts “Player’s hand: ” + playerHand.to_s + “nn”
puts “Dealer’s hand: ” + dealerHand.to_s + “nnnnnn”
print “Would you like another card? (Y/N) ”
reply = STDIN.gets #Collect the player’sanswer
reply.chop! #Remove any extra characters appended to thestring
#See if the player decided to ask for another card
if reply =~ /y/i then
#Call method responsible for getting a new card and add itto the
#player’s hand
playerHand = playerHand + get_new_card
end
#See if the player has decided to stick with the currenthand
if reply =~ /n/i then
break #Terminate the execution of theloop
end
if playerHand > 21 then
break #Terminate the execution of theloop
end
end
#Return the value of the player’shand
return playerHand
end
#Define a method responsible for managing the dealer’shand
def play_dealer_hand(dealerHand)
loop do #Loopforever
#If the value of the dealer’s hand is lessthan 17 then give the
#dealer another card
if dealerHand < 17 then
#Call method responsible for getting a new card and add itto the
#dealer’s hand
dealerHand = dealerHand + get_new_card
else
break #Terminate the execution of theloop
end
end
#Return the value of the dealer’shand
return dealerHand
end
#Define a method responsible for analyzing the player anddealer’s
#hands and determining who won
def determine_winner(playerHand,dealerHand)
Console_Screen.cls #Clear the displayarea
#Show the value of the player and dealer’shands
puts “Player’s hand: ” + playerHand.to_s + “nn”
puts “Dealer’s hand: ” + dealerHand.to_s + “nnnnnn”
if playerHand > 21 then#See if the player has gone bust
puts “You have gone bust!nn”
print “Press Enter to continue.”
else #See if the player and dealer havetied
if playerHand == dealerHandthen
puts “Tie!nn”
print “Press Enter to continue.”
end
#Dee if the dealer has gone bust
if dealerHand > 21 then
puts “The Dealer has gone bust!nn”
print “Press Enter to continue.”
else
#See if the player’s hand beats the dealer’s hand
if playerHand > dealerHandthen
puts “You have won!nn”
print “Press Enter to continue.”
end
#See if the dealer’s hand beats the player’s hand
if playerHand < dealerHandthen
puts “The Dealer has won!nn”
print “Press Enter to continue.”
end
end
end
Console_Screen.pause #Pause the game
end
#This method displays information about the Ruby Blackjackgame
def display_credits
Console_Screen.cls #Clear the display area
#Thank the player and display game information
puts “tt Thank you for playing the Ruby Blackjackgame.nnnn”
puts “nttt Developed by Jerry Lee Ford, Jr.nn”
puts “tttt Copyright 2010nn”
puts “tttURL:http://www.tech-publishing.comnnnnnnnnnn”
end
end
# Main Script Logic——————————————————-
Console_Screen = Screen.new #Instantiate a new Screenobject
BJ = Game.new #Instantiate a new Game object
#Execute the Game class’s display_greeting method
BJ.display_greeting
answer = “” #Initialize variable and assign it an emptystring
#Loop until the player enters y or n and do not accept anyother 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 Ruby Blackjack? (y/n): ”
answer = STDIN.gets #Collect the player’s answer
answer.chop! #Remove any extra characters appended to thestring
#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 wantsto quit
Console_Screen.cls #Clear the display area
#Invite the player to return and play the game some othertime
puts “Okay, perhaps another time.nn”
else #The player wants to play thegame
#Execute the game class’s display_instructionsmethod
BJ.display_instructions
playAgain = “” #Initialize variable and assign it an emptystring
loop do #Loop forever
#Execute the Game class’s play_game method
BJ.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 another hand? (y/n): ”
playAgain = STDIN.gets #Collect the player’sresponse
playAgain.chop! #Remove any extra characters appended to thestring
#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 display_credits method
BJ.display_credits
end
Expert Answer
Answer to The game’s welcome screen and credits screen are also stored as text strings within the program file. Externalize the…