Class: Rpsgame::RockPaper
- Inherits:
-
Object
- Object
- Rpsgame::RockPaper
- Defined in:
- lib/rpsgame.rb
Constant Summary collapse
- @@options =
{"rock" => 0, "paper"=> 1, "scissors"=> 2}
Instance Method Summary collapse
-
#begin ⇒ Object
@@options_arr = [“rock”, “paper”, “scissors”].
- #game_type ⇒ Object
- #get_an_answer ⇒ Object
- #play_vs_computer ⇒ Object
- #play_vs_human ⇒ Object
- #winner ⇒ Object
Instance Method Details
#begin ⇒ Object
@@options_arr = [“rock”, “paper”, “scissors”]
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 ⇒ Object
27 28 29 30 31 32 33 |
# 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 ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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 ⇒ Object
35 36 37 |
# File 'lib/rpsgame.rb', line 35 def play_vs_computer @second_player_move = @@options.key(rand(3)) end |
#play_vs_human ⇒ Object
39 40 41 42 |
# 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 ⇒ Object
62 63 64 65 66 67 68 69 70 |
# 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 |