Class: PaperRockScissors::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_rock_scissors/game.rb

Constant Summary collapse

SYMBOLS =
{"R" => "S", "S" => "P", "P" => "R"}

Instance Method Summary collapse

Constructor Details

#initialize(players = 1) ⇒ Game

Returns a new instance of Game.



7
8
9
10
11
12
13
14
# File 'lib/paper_rock_scissors/game.rb', line 7

def initialize(players=1)
  @player1 = Human.new
  if players == 1
    @player2 = Computer.new
  else
    @player2 = Human.new
  end
end

Instance Method Details

#check_winner(move1, move2) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/paper_rock_scissors/game.rb', line 33

def check_winner(move1, move2)
  if SYMBOLS[move1] == move2
    puts "Player 1 is the winner!"
  else
    puts "Player 2 is the winner!"
  end
end

#playObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/paper_rock_scissors/game.rb', line 16

def play
  loop do
    move1 = @player1.get_move

    move2 = @player2.get_move

    if move1 == move2
      puts "Tie! Try again!"
      next
    else
      check_winner(move1, move2)
    end

    break
  end
end