Class: RoShamBo
- Inherits:
-
Object
- Object
- RoShamBo
- Defined in:
- lib/ro_sham_bo.rb,
lib/ro_sham_bo/version.rb
Defined Under Namespace
Modules: Version
Constant Summary collapse
- RULES =
{ rock: {rock: :draw, paper: :lose, scissors: :win}.freeze, paper: {rock: :win, paper: :draw, scissors: :lose}.freeze, scissors: {rock: :lose, paper: :win, scissors: :draw}.freeze }.freeze
- VERSION =
The version, as a string.
Version.to_s
Instance Attribute Summary collapse
-
#cheater ⇒ Object
readonly
Returns the value of attribute cheater.
-
#computer_choice ⇒ Object
readonly
Returns the value of attribute computer_choice.
-
#draws ⇒ Object
readonly
Returns the value of attribute draws.
-
#points_to_win ⇒ Object
readonly
Returns the value of attribute points_to_win.
-
#round_winner ⇒ Object
readonly
Returns the value of attribute round_winner.
-
#rounds ⇒ Object
readonly
Returns the value of attribute rounds.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#user_choice ⇒ Object
readonly
Returns the value of attribute user_choice.
-
#winner ⇒ Object
readonly
Returns the value of attribute winner.
Instance Method Summary collapse
-
#initialize(cheater: nil, rounds: 3) ⇒ RoShamBo
constructor
A new instance of RoShamBo.
- #over? ⇒ Boolean
- #play(input) ⇒ Object
Constructor Details
#initialize(cheater: nil, rounds: 3) ⇒ RoShamBo
Returns a new instance of RoShamBo.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ro_sham_bo.rb', line 20 def initialize(cheater: nil, rounds: 3) if rounds.even? raise ArgumentError, "rounds must be odd" elsif ![nil, :user, :computer].include?(cheater) raise ArgumentError, "cheater must be :computer or :user" end @cheater = cheater @rounds = rounds @score = {user: 0, computer: 0} @draws = 0 @points_to_win = (rounds.to_f / 2).ceil end |
Instance Attribute Details
#cheater ⇒ Object (readonly)
Returns the value of attribute cheater.
10 11 12 |
# File 'lib/ro_sham_bo.rb', line 10 def cheater @cheater end |
#computer_choice ⇒ Object (readonly)
Returns the value of attribute computer_choice.
16 17 18 |
# File 'lib/ro_sham_bo.rb', line 16 def computer_choice @computer_choice end |
#draws ⇒ Object (readonly)
Returns the value of attribute draws.
13 14 15 |
# File 'lib/ro_sham_bo.rb', line 13 def draws @draws end |
#points_to_win ⇒ Object (readonly)
Returns the value of attribute points_to_win.
14 15 16 |
# File 'lib/ro_sham_bo.rb', line 14 def points_to_win @points_to_win end |
#round_winner ⇒ Object (readonly)
Returns the value of attribute round_winner.
17 18 19 |
# File 'lib/ro_sham_bo.rb', line 17 def round_winner @round_winner end |
#rounds ⇒ Object (readonly)
Returns the value of attribute rounds.
11 12 13 |
# File 'lib/ro_sham_bo.rb', line 11 def rounds @rounds end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
12 13 14 |
# File 'lib/ro_sham_bo.rb', line 12 def score @score end |
#user_choice ⇒ Object (readonly)
Returns the value of attribute user_choice.
15 16 17 |
# File 'lib/ro_sham_bo.rb', line 15 def user_choice @user_choice end |
#winner ⇒ Object (readonly)
Returns the value of attribute winner.
18 19 20 |
# File 'lib/ro_sham_bo.rb', line 18 def winner @winner end |
Instance Method Details
#over? ⇒ Boolean
45 46 47 |
# File 'lib/ro_sham_bo.rb', line 45 def over? !winner.nil? end |
#play(input) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ro_sham_bo.rb', line 34 def play(input) raise "Game Over!" if over? @user_choice = sanitized_choice(input) @computer_choice = computer_turn(user_choice) @round_winner = determine_round_winner(@user_choice, @computer_choice) @winner = score.key(points_to_win) if score.value?(points_to_win) self end |