Class: VikingRoom7::RPS
- Inherits:
-
Object
- Object
- VikingRoom7::RPS
- Defined in:
- lib/viking_room7/RPS.rb
Instance Method Summary collapse
- #ask_player_num ⇒ Object
- #compare ⇒ Object
- #display_winner ⇒ Object
- #get_name ⇒ Object
-
#initialize ⇒ RPS
constructor
A new instance of RPS.
- #play ⇒ Object
- #who_won ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize ⇒ RPS
Returns a new instance of RPS.
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/viking_room7/RPS.rb', line 3 def initialize @player1 = Human.new(get_name) num = ask_player_num if num == 1 @player2 = Computer.new elsif num == 2 @player2 = Human.new(get_name) else puts "pick 1 or 2" end play end |
Instance Method Details
#ask_player_num ⇒ Object
59 60 61 62 |
# File 'lib/viking_room7/RPS.rb', line 59 def ask_player_num puts "how many players?" num = gets.chomp.to_i end |
#compare ⇒ Object
30 31 32 33 |
# File 'lib/viking_room7/RPS.rb', line 30 def compare @player1.get_hand @player2.get_hand end |
#display_winner ⇒ Object
50 51 52 |
# File 'lib/viking_room7/RPS.rb', line 50 def display_winner puts "#{who_won} wins!" end |
#get_name ⇒ Object
16 17 18 19 |
# File 'lib/viking_room7/RPS.rb', line 16 def get_name puts "What is your name?" gets.chomp end |
#play ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/viking_room7/RPS.rb', line 21 def play loop do compare who_won break if win? end display_winner end |
#who_won ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/viking_room7/RPS.rb', line 35 def who_won moves = [@player1.hand, @player2.hand] players = [@player1.name, @player2.name] if moves.include?("r") && moves.include?("p") i = moves.index("p") return players[i] elsif moves.include?("r") && moves.include?("s") i = moves.index("r") return players[i] elsif moves.include?("s") && moves.include?("p") i = moves.index("s") return players[i] end end |
#win? ⇒ Boolean
54 55 56 |
# File 'lib/viking_room7/RPS.rb', line 54 def win? @player1.hand != @player2.hand end |