class Rpsgame::RockPaper

Public Instance Methods

begin() click to toggle source

@@options_arr = [“rock”, “paper”, “scissors”]

# File lib/rpsgame.rb, line 9
def begin
  while true
    #Choose the game type
    game_type

    #break if @@second_player_move == "quit"

    #Player 1
    @your_move = get_an_answer

    #break if @@your_move == "quit"
      
    puts "The other player plays #{@second_player_move}"

    winner     
  end
end
game_type() click to toggle source
# File lib/rpsgame.rb, line 27
def game_type
  
    puts "Do you want to play with computer or another player? (c for computer, p for human player)?"
    play_with = gets.chomp 
    play_with == "c" ? play_vs_computer : play_vs_human
    
end
get_an_answer() click to toggle source
# File lib/rpsgame.rb, line 44
def get_an_answer
    while true
      puts "Please enter your move (rock, paper or scissors)"
      puts "Enter quit if you want to end the game."
      answer = gets.chomp

      if ["rock", "paper", "scissors"].include?(answer)
        return answer
      elsif answer == "quit"
        exit
      else
        puts "Your input is not valid"
        next
      end
      
    end
end
play_vs_computer() click to toggle source
# File lib/rpsgame.rb, line 35
def play_vs_computer
  @second_player_move = @@options.key(rand(3))
end
play_vs_human() click to toggle source
# File lib/rpsgame.rb, line 39
def play_vs_human
  puts "Player two, please enter your move"
  @second_player_move = get_an_answer
end
winner() click to toggle source
# File lib/rpsgame.rb, line 62
def winner
  if @your_move == @second_player_move
      puts "Tie"
  elsif [1,-2].include?(@@options[@your_move] - @@options[@second_player_move])
    puts "Congrats! You win."
  else
    puts "You lose!"
  end
end