class Rockpaperscissorsgame::RockPaper
Public Instance Methods
begin()
click to toggle source
# File lib/rockpaperscissorsgame.rb, line 8 def begin while true game_type @your_move = get_an_answer puts "The other player plays #{@second_player_move}" winner end end
game_type()
click to toggle source
# File lib/rockpaperscissorsgame.rb, line 19 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/rockpaperscissorsgame.rb, line 36 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/rockpaperscissorsgame.rb, line 27 def play_vs_computer @second_player_move = @@options.key(rand(3)) end
play_vs_human()
click to toggle source
# File lib/rockpaperscissorsgame.rb, line 31 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/rockpaperscissorsgame.rb, line 54 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