Class: Codebreaker::GameStage

Inherits:
BaseClass show all
Defined in:
lib/codebreaker/game_stage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseClass

#valid?

Methods included from Validator

#validate_length?, #validate_number_range?

Constructor Details

#initialize(attempts:) ⇒ GameStage

Returns a new instance of GameStage.



6
7
8
9
10
11
12
# File 'lib/codebreaker/game_stage.rb', line 6

def initialize(attempts:)
  @step_number = 1
  @endgame = false
  @attempts = attempts
  @compare_result = []
  @hint_used = 0
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



3
4
5
# File 'lib/codebreaker/game_stage.rb', line 3

def attempts
  @attempts
end

#compare_resultObject (readonly)

Returns the value of attribute compare_result.



3
4
5
# File 'lib/codebreaker/game_stage.rb', line 3

def compare_result
  @compare_result
end

#endgameObject (readonly)

Returns the value of attribute endgame.



3
4
5
# File 'lib/codebreaker/game_stage.rb', line 3

def endgame
  @endgame
end

#hint_usedObject

Returns the value of attribute hint_used.



4
5
6
# File 'lib/codebreaker/game_stage.rb', line 4

def hint_used
  @hint_used
end

#step_numberObject (readonly)

Returns the value of attribute step_number.



3
4
5
# File 'lib/codebreaker/game_stage.rb', line 3

def step_number
  @step_number
end

#winObject (readonly)

Returns the value of attribute win.



3
4
5
# File 'lib/codebreaker/game_stage.rb', line 3

def win
  @win
end

Instance Method Details

#step(compare_result) ⇒ Object



14
15
16
17
18
19
# File 'lib/codebreaker/game_stage.rb', line 14

def step(compare_result)
  @compare_result = compare_result
  @step_number += 1
  @win = @compare_result.length == Game::CODE_LENGTH && @compare_result.all?
  @endgame = true if !valide_allow_step? || @win
end

#valide_allow_step?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/codebreaker/game_stage.rb', line 21

def valide_allow_step?
  return true if @attempts >= @step_number
  
  @errors[:game_stage] = 'allow_step_error'
  false
end