Assignment #8: Black Jack Summary: Create the Black Jack program. The solution f
ID: 3855518 • Letter: A
Question
Assignment #8: Black Jack
Summary: Create the Black Jack program.
The solution file should be named BlackJack_ExerciseE.rb.
Use the instructions on page 290 of the textbook.
Run your program until it works and the output looks nice.
Add the necessary documentation as described in Course Documents, and then attach your .rb file to this assignment.
Object riented Programming CHAPTER 8 Object-Oriented Programming 17. A(n) variable's scope is accessible to all instances of the same class. a. Local b. Global c. Instance 290 Class 18. The dehed. eadmethod determines whether a variable exists, returning a value of true if the variable is found. 19. Using themethod, you can reverse the cnd in which arrays are retrieved. 20. is a keyword that can be used as a shorthand way of referring to the current object. Reinforcement Exercises The following exercises are designed to further your understanding af Ruby programming by challenging you to make improvements to the chapter's game project, the Rock, Paper, Scissors game. 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 takes time and can lead to typos, however. Simplify game play by modifying the get player_move method to also allow single character input in the form of R, P, and S. Make sure you accommodate both uppercase and lowercase input. 1. The game allows players to play an unlimited number of times. To help them keep track of how many times they have played, modify the game by adding a global variable named SgameCount at the beginning of the program's Main Script Logic area, assigning it an initial value of 0. Next, add a method named game count and set it to increment the value of SgameCount each time it is executed. Lastly add a statement to the play game method that executes the 2.Explanation / Answer
# 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 "" #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 Rock, Paper, scissors game
class Game
#This method displays the game's opening message
def display_greeting
Console_Screen.cls #Clear the display area
#Display welcome message
print " Let's Play Rock, Paper, Scissors!" +
" 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 pits the player against the computer. To play, you must"
puts "enter one of the following moves when prompted: Rock, Paper, or"
puts "Scissors. "
puts "The game will randomly select a move for the computer, and "
puts "the two moves will then be analyzed according to the following"
puts "rules: "
puts "* Rock crushes Scissors, Rock equals Rock, and Rock is covered by"
puts " Paper. "
puts "* Paper covers Rock, Paper equals Paper, and Paper is cut by"
puts " Scissors. "
puts "* Scissors cut Paper, Scissors equals Scissors, and Scissors are"
puts " crushed by Rock. "
puts "Good luck! "
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
#tracking players games played
gamesPlayed = game_counter
#Call on the method responsible for collecting the player's move
playerMove = get_player_move
#Call on the method responsible for generating the computer's move
computerMove = get_computer_move
#Call on the method responsible for determining the results of the game
result = analyze_results(playerMove, computerMove)
#Call on the method responsible for displaying the results of the game
display_results(gamesPlayed, playerMove, computerMove, result)
end
#Define the method responsible for collecting the player's move
def get_player_move
Console_Screen.cls #Clear the display area
loop do #Loop forever
Console_Screen.cls #Clear the display area
#Prompt the player to select a move
puts ""
puts "To make a move, type one of the following and press Enter: "
print "[Rock] [Paper] [Scissors]: "
@choice = STDIN.gets #Collect the player's answer
@choice.chop! #Remove any extra characters appended to
#the string
#check input for single letters R, P, S
@choice = @choice.downcase
if @choice == 'r'
@choice = 'rock'
elsif @choice == 'p'
@choice = 'paper'
elsif @choice == 's'
@choice = 'scissors'
end
#Terminate the loop if valid input was provided
break if @choice =~ /Rock|Paper|Scissors/i
end
#Convert the player move to upper case and return it to the calling
#statement
return @choice.upcase
end
#Define the method responsible for making the computer's move
def get_computer_move
#Define and array containing a list of three possible moves
moves = ["ROCK", "PAPER", "SCISSORS"]
#Generate and return a random number between 0 and 2
randomNo = rand(3)
#Return a randomly selected move to the calling statement
return moves[randomNo]
end
#Define the method responsible for analyzing and returning the result of
#the game (arguments are passed as upper case characters)
def analyze_results(player, computer)
#Analyze the results of the game when the player selects ROCK
if player == "ROCK" then
if computer == "SCISSORS"
$wins += 1
return "Rock crushes Scissors.. Player wins!"
elsif computer == "ROCK"
$ties += 1
return "Two Rocks don't make a right. Tie!"
elsif computer == "PAPER"
$lost += 1
return "Rock coverd by Paper.. Computer wins!"
end
#return "Player wins! (ROCK)" if computer == "SCISSORS"
#return "Tie!" if computer == "ROCK"
#return "Computer wins!" if computer == "PAPER"
end
#Analyze the results of the game when the player selects PAPER
if player == "PAPER" then
if computer == "ROCK"
$wins += 1
return "Paper covers Rock.. Player wins!"
elsif computer == "PAPER"
$ties += 1
return "Papers fold. Tie!"
elsif computer == "SCISSORS"
$lost += 1
return "Paper is cut by Scissors.. Computer wins!"
end
#return "Player wins! (PAPER)" if computer == "ROCK"
#return "Tie!" if computer == "PAPER"
#return "Computer wins!" if computer == "SCISSORS"
end
#Analyze the results of the game when the player selects SCISSORS
if player == "SCISSORS" then
if computer == "PAPER"
$wins += 1
return "Scissors slash Paper.. Player wins!"
elsif computer == "SCISSORS"
$ties += 1
return "Sword fight. Tie!"
elsif computer == "ROCK"
$lost += 1
return "Scissors crushed by Rock.. Computer wins!"
end
#return "Player wins! (SCISSORS)" if computer == "PAPER"
#return "Tie!" if computer == "SCISSORS"
#return "Computer wins!" if computer == "ROCK"
end
end
#Define the method responsible for displaying the result of the game
def display_results(played, player, computer, result)
#Display arguments passed to the method using the following template
Console_Screen.cls #Clear the display area
puts ""
puts " RESULTS:"
puts " ================================"
puts " Games played: " + played.to_s
puts " Player's move: " + player
puts " Computer's move: " + computer
puts " Result: " + result
puts " Wins Lost Ties "
puts " ----- ----- -----"
puts " " + $wins.to_s + " " + $lost.to_s + " " + $ties.to_s
puts " ================================"
puts " "
print "Press Enter to continue. "
Console_Screen.pause #Pause the game
end
#This method displays the information about the Rock, Paper,
#Scissors game
def display_credits
Console_Screen.cls #Clear the display area
#Thank the player and display game information
puts " Thank you for playing the Rock, Paper, Scissors game. "
puts " Developed by Jerry Lee Ford, Jr. "
puts " Copyright 2010 "
puts " URL: http://www.tech-publishing.com "
end
#Define game count method
def game_counter
$gameCount += 1 #increment counter by 1
@g = $gameCount #assign counter
return @g #return value
end
end
# Main Script Logic -------------------------------------------------------
Console_Screen = Screen.new #Instantiate a new Screen object
RPS = Game.new #Instantiate a new Game object
#Execute the Game class's display_greeting method
RPS.display_greeting
answer = "" #Initialize variable and assign it an empty string
$gameCount = 0 #global variable - game counter for # of plays
$wins = 0 #global variable - wins
$lost = 0 #global variable - lost
$ties = 0 #global variable - ties
#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 Rock, Paper, Scissors? (y/n): "
answer = STDIN.gets #Collect the player's answer
answer.chop! #Remove any extra characters appended to the string
#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 wants to quit
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
RPS.display_instructions
playAgain = ""
loop do #Loop forever
#Execute the Game class's play_game method
RPS.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 again? (y/n): "
playAgain = STDIN.gets #Collect the player's response
playAgain.chop! #Remove any extra characters appended to the string
#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 determine_credits method
RPS.display_credits
end