Class: RpsGameDavidxin::RockPaperScissors
- Inherits:
-
Object
- Object
- RpsGameDavidxin::RockPaperScissors
- Defined in:
- lib/rps_game_davidxin.rb
Instance Method Summary collapse
- #compare_move(move1, move2) ⇒ Object
-
#initialize ⇒ RockPaperScissors
constructor
A new instance of RockPaperScissors.
- #is_1_or_2?(input) ⇒ Boolean
- #is_true_or_false?(input) ⇒ Boolean
- #one_player? ⇒ Boolean
- #play_again? ⇒ Boolean
- #player1_win?(move1, move2) ⇒ Boolean
- #take_turn ⇒ Object
Constructor Details
#initialize ⇒ RockPaperScissors
Returns a new instance of RockPaperScissors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rps_game_davidxin.rb', line 6 def initialize puts "Welcome to Rock Paper Scissors game" @player_1 = Player.new("Player 1") if one_player? @player_2 = AIGameplay.new else @player_2 = Player.new("Player 2") end = true while take_turn = play_again? end puts "Play again later! Bye." end |
Instance Method Details
#compare_move(move1, move2) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/rps_game_davidxin.rb', line 68 def compare_move(move1, move2) if move1 == move2 puts "You tied against player 2!" elsif player1_win?(move1, move2) puts "Player 1 wins!" else puts "Player 2 wins!" end end |
#is_1_or_2?(input) ⇒ Boolean
46 47 48 |
# File 'lib/rps_game_davidxin.rb', line 46 def is_1_or_2?(input) ["1","2"].include?(input) end |
#is_true_or_false?(input) ⇒ Boolean
63 64 65 |
# File 'lib/rps_game_davidxin.rb', line 63 def is_true_or_false?(input) ["y", "n"].include?(input) end |
#one_player? ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rps_game_davidxin.rb', line 34 def one_player? print "Would you like to play (1) against the AI, or (2) multiplayer? " is_valid = false until is_valid input = gets.chomp is_valid = is_1_or_2?(input) print "Please enter 1 for solo and 2 for multiplayer: " unless is_valid end input == "1" end |
#play_again? ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rps_game_davidxin.rb', line 51 def play_again? is_valid = false until is_valid print "Do you want to play again? (Y/N): " input = gets.chomp.downcase is_valid = is_true_or_false?(input) puts "Try 'y' or 'n'" unless is_valid end return input == "y" end |
#player1_win?(move1, move2) ⇒ Boolean
79 80 81 82 83 |
# File 'lib/rps_game_davidxin.rb', line 79 def player1_win?(move1, move2) (move1 == "r" && move2 == "s") || (move1 == "p" && move2 == "r") || (move1 == "s" && move2 == "p") end |
#take_turn ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rps_game_davidxin.rb', line 26 def take_turn move_1 = @player_1.turn move_2 = @player_2.turn puts "Player 1 chose #{move_1} and player 2 chose #{move_2}" compare_move(move_1, move_2) end |