class Game
Constants
- WINNING_COMBINATIONS
Public Class Methods
new(num_of_players)
click to toggle source
# File lib/game.rb, line 8 def initialize(num_of_players) @num_of_players = num_of_players if num_of_players == 1 @player1 = Human.new @player2 = Computer.new elsif num_of_players == 2 @player1 = Human.new @player2 = Human.new @outcome = nil end end
Public Instance Methods
check_outcome(opponent_choice)
click to toggle source
# File lib/game.rb, line 33 def check_outcome(opponent_choice) if @player1.choice == opponent_choice @outcome = "The game is a tie." elsif WINNING_COMBINATIONS[@player1.choice] == opponent_choice @outcome = "Player 1 is the winner." else @outcome = "Player 2 is the winner." end end
game_loop()
click to toggle source
# File lib/game.rb, line 60 def game_loop welcome_message print_instructions until @outcome @player1.make_choice validate_player_choice(@player1) @player2.make_choice validate_player_choice(@player2) check_outcome(@player2.choice) print_outcome end end
get_player_choice(player)
click to toggle source
# File lib/game.rb, line 47 def get_player_choice(player) print "Please enter your choice now: " player.make_choice end
play()
click to toggle source
# File lib/game.rb, line 28 def play puts "Welcome to Rock Paper Scissors\n You must choose: " @player1.make_choice end
print_instructions()
click to toggle source
# File lib/game.rb, line 24 def print_instructions puts "To make a move, type the hand sign you would like to use\n e.g. \"rock\", \"paper\", \"scissors\"" end
print_outcome()
click to toggle source
# File lib/game.rb, line 43 def print_outcome puts @outcome end
validate_player_choice(player)
click to toggle source
# File lib/game.rb, line 52 def validate_player_choice(player) unless ["rock", "paper", "scissors"].include?(player.choice) puts "Please enter either \"rock\", \"paper\", or \"scissors\"" get_player_choice(player) end end
welcome_message()
click to toggle source
# File lib/game.rb, line 20 def welcome_message puts "Rock Paper Scissors." end