Class: RPSGame::Game

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

Instance Method Summary collapse

Instance Method Details

#playObject



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

def play
	puts "Enter your choice Rock(r), Paper(p), Scissors(s): "
	choice = gets.chomp
	unless validate?(choice)
		self.play
	end
	winner(choice)
end

#validate?(choice) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rps/game.rb', line 17

def validate?(choice)
	["r", "p", "s"].include?(choice[0].downcase)
end

#winner(choice) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rps/game.rb', line 21

def winner(choice)
	player2_choice = ["r", "p", "s"].sample
	puts "Player 2 chooses #{player2_choice}"

	if choice == player2_choice
		puts "It's a tie!"
	elsif choice == "r" && player2_choice == "p"
		puts "Player 2 wins"
	elsif choice == "r" && player2_choice == "s"
		puts "Player 1 wins"
	elsif choice == "p" && player2_choice == "s"
		puts "Player 2 wins"
	elsif choice == "p" && player2_choice == "r"
		puts "Player 1 wins"
	elsif choice == "s" && player2_choice == "p"
		puts "Player 1 wins"
	elsif choice == "s" && player2_choice == "r"
		puts "Player 2 wins"
	end
end