class VikingRoom7::RPS

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

ask_player_num() click to toggle source
# File lib/viking_room7/RPS.rb, line 59
def ask_player_num
  puts "how many players?"
  num = gets.chomp.to_i
end
compare() click to toggle source
# File lib/viking_room7/RPS.rb, line 30
def compare
  @player1.get_hand
  @player2.get_hand
end
display_winner() click to toggle source
# File lib/viking_room7/RPS.rb, line 50
def display_winner
  puts "#{who_won} wins!"
end
get_name() click to toggle source
# File lib/viking_room7/RPS.rb, line 16
def get_name
  puts "What is your name?"
  gets.chomp
end
play() click to toggle source
# File lib/viking_room7/RPS.rb, line 21
def play
  loop do
    compare
    who_won
    break if win?
  end
  display_winner
end
who_won() click to toggle source
# 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?() click to toggle source
# File lib/viking_room7/RPS.rb, line 54
def win?
  @player1.hand != @player2.hand
end