Class: MastermindGenerator::Game
- Inherits:
-
Object
- Object
- MastermindGenerator::Game
- Extended by:
- Forwardable
- Defined in:
- lib/mastermind_generator/game.rb
Overview
Mastermind game class
Instance Attribute Summary collapse
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
Instance Method Summary collapse
- #add_player(player_name) ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(difficulty) ⇒ Game
constructor
A new instance of Game.
- #next_turn ⇒ Object
- #player ⇒ Object
- #take_a_guess(value) ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(difficulty) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 |
# File 'lib/mastermind_generator/game.rb', line 10 def initialize(difficulty) @difficulty = Difficulty.new(difficulty) @sequence = SequenceGenerator.new(@difficulty).generate @players = [] @turn_counter = 1 end |
Instance Attribute Details
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
6 7 8 |
# File 'lib/mastermind_generator/game.rb', line 6 def difficulty @difficulty end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
6 7 8 |
# File 'lib/mastermind_generator/game.rb', line 6 def players @players end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
6 7 8 |
# File 'lib/mastermind_generator/game.rb', line 6 def sequence @sequence end |
Instance Method Details
#add_player(player_name) ⇒ Object
25 26 27 28 29 |
# File 'lib/mastermind_generator/game.rb', line 25 def add_player(player_name) return if players.length > 1 players << Player.new(player_name) end |
#finished? ⇒ Boolean
31 32 33 34 35 36 |
# File 'lib/mastermind_generator/game.rb', line 31 def finished? return false unless player.guess.succeed? player.timer_stop true end |
#next_turn ⇒ Object
42 43 44 45 |
# File 'lib/mastermind_generator/game.rb', line 42 def next_turn player.timer_pause @turn_counter += 1 end |
#player ⇒ Object
47 48 49 50 51 |
# File 'lib/mastermind_generator/game.rb', line 47 def player return players.first if players_count == 1 @turn_counter.odd? ? players.first : players.last end |
#take_a_guess(value) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/mastermind_generator/game.rb', line 17 def take_a_guess(value) seq = Sequence.new(difficulty, value) guess = Guess.new(seq) player.timer_start player.take_a_guess(guess) player.guess.assign_target(sequence) end |
#winner ⇒ Object
38 39 40 |
# File 'lib/mastermind_generator/game.rb', line 38 def winner player if finished? end |