class Rockpaperscissors::Game

Attributes

player_one[R]
player_one_wins[RW]
player_quits[R]
player_two[R]

Public Class Methods

new() click to toggle source

Set up the game initially

# File lib/rockpaperscissors/game.rb, line 7
def initialize
  @player_quits = false
  @player_one_wins = false
  @player_one = Player.new(1)
  puts "Welcome to Rock Paper Scissors!"
  puts "One player or two?"
  number_of_players = gets.chomp.to_i

  if number_of_players == 1
    @player_two = AI.new
  else
    @player_two = Player.new(2)
  end

  play
end

Private Instance Methods

ask_player_to_continue() click to toggle source
# File lib/rockpaperscissors/game.rb, line 57
def ask_player_to_continue
  puts "Enter 'q' to quit, or anything else to play again: "
  quit_prompt = gets.chomp.upcase
  @player_quits = (quit_prompt == 'Q')
end
display_winner_message(player_two_move) click to toggle source
# File lib/rockpaperscissors/game.rb, line 63
def display_winner_message player_two_move
  puts "Player two chose #{player_two_move}"
  if player_one_wins
    puts "Player one wins!"
  else
    puts "Player two wins!"
  end
end
is_draw?(move1,move2) click to toggle source
# File lib/rockpaperscissors/game.rb, line 53
def is_draw?(move1,move2)
  move1 == move2
end
pick_winner(move1,move2) click to toggle source
# File lib/rockpaperscissors/game.rb, line 40
def pick_winner(move1,move2)
  case
  when move1 == "R" && move2 == "S"
    self.player_one_wins = true
  when move1 == "S" && move2 == "P"
    self.player_one_wins = true
  when move1 == "P" && move2 == "R"
    self.player_one_wins = true
  else
    self.player_one_wins = false
  end
end
play() click to toggle source
# File lib/rockpaperscissors/game.rb, line 26
def play
  until player_quits
    player_one_move = player_one.move
    player_two_move = player_two.move
    if is_draw?(player_one_move,player_two_move)
      puts "It's a draw"
    else
      pick_winner(player_one_move,player_two_move)
      display_winner_message player_two_move
    end
    ask_player_to_continue
  end
end