Class: Rpsshoot::RockPaperScissors
- Inherits:
-
Object
- Object
- Rpsshoot::RockPaperScissors
- Defined in:
- lib/rpsshoot.rb
Overview
Controls main game function
Instance Method Summary collapse
-
#determine_winner(array) ⇒ Object
Determines who wins after five rounds.
-
#initialize ⇒ RockPaperScissors
constructor
A new instance of RockPaperScissors.
-
#instructions ⇒ Object
Instructions on how to play.
- #paper(selection) ⇒ Object
-
#pick_players ⇒ Object
Pick the number of players you would like to play with.
-
#play ⇒ Object
Starts game loop.
-
#play_again? ⇒ Boolean
Once game has finished, asks if they would like to play another round.
-
#results(round, first, second) ⇒ Object
Display results after each round.
-
#rock(selection) ⇒ Object
Rock beats Scissors, Scissors beats Paper, Paper beats Rock.
-
#round_winner(rps1, rps2) ⇒ Object
Determine the winner of each round.
- #scissors(selection) ⇒ Object
-
#score ⇒ Object
Displays final score.
Constructor Details
#initialize ⇒ RockPaperScissors
Returns a new instance of RockPaperScissors.
10 11 12 13 14 15 |
# File 'lib/rpsshoot.rb', line 10 def initialize @player1 = Player.new(1, "Player 1") @player2 = Computer.new(2, "Computer") @score = [] @instructions = instructions end |
Instance Method Details
#determine_winner(array) ⇒ Object
Determines who wins after five rounds
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rpsshoot.rb', line 116 def determine_winner(array) p1 = array.count(@player1.symbol) p2 = array.count(@player2.symbol) if p1 > p2 puts "Congrats! #{@player1.name} wins!" score elsif p1 < p2 puts "Congrats! #{@player2.name} wins!" score else puts "You both win, it's a tie!" score end end |
#instructions ⇒ Object
Instructions on how to play
32 33 34 35 36 37 38 39 40 |
# File 'lib/rpsshoot.rb', line 32 def instructions puts "Welcome to Rock, Paper, Scissors" puts "This is a two player game where:" puts "* Rock smashes Scissors" puts "* Scissors cut Paper" puts "* Paper covers Rock" puts "* Best out of five wins" pick_players end |
#paper(selection) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rpsshoot.rb', line 103 def paper(selection) if selection == "rock" puts ">> #{@player1.name} takes this round!" @player1.symbol elsif selection == "scissors" puts ">> #{@player2.name} takes this round!" @player2.symbol elsif selection == "paper" 0 end end |
#pick_players ⇒ Object
Pick the number of players you would like to play with
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rpsshoot.rb', line 43 def pick_players puts "Would you like to play with 1 or 2 players?" puts "Choose 1 player to play against the computer" number_of_players = gets.chomp.to_i if number_of_players == 1 play elsif number_of_players == 2 @player2 = Player.new(2, "Player 2") play else puts "That was not a valid selection, please try again..." pick_players end end |
#play ⇒ Object
Starts game loop
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rpsshoot.rb', line 18 def play round = 0 5.times do round += 1 p1_selection = @player1.shoot p2_selection = @player2.shoot results(round, p1_selection, p2_selection) @score << round_winner(p1_selection, p2_selection) end determine_winner(@score) play_again? ? @score.clear && play : exit end |
#play_again? ⇒ Boolean
Once game has finished, asks if they would like to play another round
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rpsshoot.rb', line 139 def play_again? puts "Would you like to play again? y/n?" answer = gets.chomp case answer when "n" false when "y" true end end |
#results(round, first, second) ⇒ Object
Display results after each round
71 72 73 74 75 |
# File 'lib/rpsshoot.rb', line 71 def results(round, first, second) puts ">> Round #{round} results:" puts ">> #{@player1.name}: #{first}" puts ">> #{@player2.name}: #{second}" end |
#rock(selection) ⇒ Object
Rock beats Scissors, Scissors beats Paper, Paper beats Rock
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rpsshoot.rb', line 78 def rock(selection) if selection == "rock" puts ">> It's a tie!" 0 elsif selection == "scissors" puts ">> #{@player1.name} takes this round!" @player1.symbol elsif selection == "paper" puts ">> #{@player2.name} takes this round!" @player2.symbol end end |
#round_winner(rps1, rps2) ⇒ Object
Determine the winner of each round
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rpsshoot.rb', line 59 def round_winner(rps1, rps2) case rps1 when "rock" rock(rps2) when "scissors" scissors(rps2) when "paper" paper(rps2) end end |
#scissors(selection) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rpsshoot.rb', line 91 def scissors(selection) if selection == "rock" puts ">> #{@player2.name} takes this round!" @player2.symbol elsif selection == "scissors" 0 elsif selection == "paper" puts ">> #{@player1.name} takes this round!" @player1.symbol end end |
#score ⇒ Object
Displays final score
132 133 134 135 136 |
# File 'lib/rpsshoot.rb', line 132 def score puts ">> #{@player1.name}: #{@score.count(@player1.symbol)} wins" puts ">> #{@player2.name}: #{@score.count(@player2.symbol)} wins" puts ">> Ties: #{@score.count(0)}" end |