class LJRPS::Game

Constants

COMPUTER_MOVES
MOVES

Public Class Methods

new() click to toggle source
# File lib/LJ_RPS.rb, line 9
def initialize()
  puts "Welcome to Rock, Paper, Scissors"
  set_up_players

end

Public Instance Methods

evaluate_winner(move_1,move_2) click to toggle source
# File lib/LJ_RPS.rb, line 53
def evaluate_winner(move_1,move_2)
  return 1 if MOVES[move_1] == move_2
  return -1 if MOVES[move_2] == move_1
  return 0
end
play() click to toggle source
# File lib/LJ_RPS.rb, line 26
def play
  loop do
    take_turn
  end
end
quit?(player_move) click to toggle source
# File lib/LJ_RPS.rb, line 49
def quit?(player_move)
  exit if player_move == 'quit'
end
results(winner) click to toggle source
# File lib/LJ_RPS.rb, line 42
def results(winner)
  puts "You win!" if winner == 1
  puts "Tie" if winner == 0
  puts "You lose" if winner == -1
  puts "Invalid move" if winner == -2
end
set_up_players() click to toggle source
# File lib/LJ_RPS.rb, line 15
def set_up_players
  @player_1 = Human.new
  print "How many players are playing today? ('1' or '2')"
  number_of_players = gets.chomp
  if number_of_players == 1
    @player_2 = Computer.new
  else
    @player_2 = Human.new
  end
end
take_turn() click to toggle source
# File lib/LJ_RPS.rb, line 32
def take_turn
  print "Enter the first letter of your move: [r]ock, [p]aper, [s]cissors or type 'quit' \n >> "
  player_move = gets.chomp
  quit?(player_move)
  computer_move = COMPUTER_MOVES[rand(0..2)]
  puts "Computer move: #{computer_move}"
  winner = evaluate_winner(player_move,computer_move)
  results(winner)
end