Class: Game
- Inherits:
-
Object
- Object
- Game
- Defined in:
- lib/game.rb
Constant Summary collapse
- WINNING_COMBINATIONS =
{ "rock" => "scissors", "paper" => "rock", "scissors" => "paper" }
Instance Method Summary collapse
- #check_outcome(opponent_choice) ⇒ Object
- #game_loop ⇒ Object
- #get_player_choice(player) ⇒ Object
-
#initialize(num_of_players) ⇒ Game
constructor
A new instance of Game.
- #play ⇒ Object
- #print_instructions ⇒ Object
- #print_outcome ⇒ Object
- #validate_player_choice(player) ⇒ Object
- #welcome_message ⇒ Object
Constructor Details
#initialize(num_of_players) ⇒ Game
Returns a new instance of Game.
8 9 10 11 12 13 14 15 16 17 18 |
# 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 |
Instance Method Details
#check_outcome(opponent_choice) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# 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 ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/game.rb', line 60 def game_loop 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) ⇒ Object
47 48 49 50 |
# File 'lib/game.rb', line 47 def get_player_choice(player) print "Please enter your choice now: " player.make_choice end |
#play ⇒ Object
28 29 30 31 |
# File 'lib/game.rb', line 28 def play puts "Welcome to Rock Paper Scissors\n You must choose: " @player1.make_choice end |
#print_instructions ⇒ Object
24 25 26 |
# 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 ⇒ Object
43 44 45 |
# File 'lib/game.rb', line 43 def print_outcome puts @outcome end |
#validate_player_choice(player) ⇒ Object
52 53 54 55 56 57 |
# 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 ⇒ Object
20 21 22 |
# File 'lib/game.rb', line 20 def puts "Rock Paper Scissors." end |