class VcsBkTdrRps::Game

Attributes

players[R]

Public Class Methods

new(user_player) click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 7
def initialize(user_player)
  @winner = nil
  @user_player = user_player
  @comp_player = VcsBkTdrRps::Player.new("Computer", true)
  @players = [@user_player, @comp_player]
end

Public Instance Methods

find_winner() click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 32
def find_winner
  compare_hands(@user_player, @comp_player)
end
play() click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 36
def play
  throw_hands
  @winner = find_winner
  show_hands
  show_winner
end
show_hands() click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 18
def show_hands
  @players.each do |player|
    puts "#{player.name}'s hand was #{player.hand}"
  end
end
show_winner() click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 24
def show_winner
  if @winner == 'tie'
    puts 'It was a tie!'
  else
    puts "#{@winner} won!"
  end
end
throw_hands() click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 14
def throw_hands
  @players.each(&:set_hand)
end

Private Instance Methods

compare_hands(p1, p2) click to toggle source
# File lib/vcs_bk_tdr_rps/game.rb, line 45
def compare_hands(p1, p2)
  if p1.hand == p2.hand
    'tie'
  else
    case p1.hand
    when 'rock'
      p2.hand == 'paper' ? p2.name : p1.name
    when 'paper'
      p2.hand == 'scissors' ? p2.name : p1.name
    when 'scissors'
      p2.hand == 'rock' ? p2.name : p1.name
    end
  end
end