Class: Codebreaker::Game

Inherits:
BaseClass show all
Includes:
InitDifficulties
Defined in:
lib/codebreaker/game.rb

Constant Summary collapse

CODE_LENGTH =
4
CODE_NUMBERS =
('1'..'6').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InitDifficulties

#init_difficulties

Methods inherited from BaseClass

#valid?

Methods included from Validator

#validate_length?, #validate_number_range?

Constructor Details

#initializeGame

Returns a new instance of Game.



11
12
13
14
15
16
# File 'lib/codebreaker/game.rb', line 11

def initialize
  @errors = {}
  @statistic = Statistic.new
  @difficulties = init_difficulties
  @difficulty = nil
end

Instance Attribute Details

#difficultiesObject (readonly)

Returns the value of attribute difficulties.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def difficulties
  @difficulties
end

#difficultyObject

Returns the value of attribute difficulty.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def difficulty
  @difficulty
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def errors
  @errors
end

#game_stageObject (readonly)

Returns the value of attribute game_stage.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def game_stage
  @game_stage
end

#hint_codeObject

Returns the value of attribute hint_code.



9
10
11
# File 'lib/codebreaker/game.rb', line 9

def hint_code
  @hint_code
end

#statisticObject (readonly)

Returns the value of attribute statistic.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def statistic
  @statistic
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/codebreaker/game.rb', line 9

def user
  @user
end

Instance Method Details

#game_startObject



34
35
36
37
38
# File 'lib/codebreaker/game.rb', line 34

def game_start
  generate_secret_code
  generate_hints
  @game_stage = GameStage.new(match_code_length: CODE_LENGTH, attempts: @difficulty.attempts)
end

#game_step(match_codes) ⇒ Object



40
41
42
43
44
45
# File 'lib/codebreaker/game.rb', line 40

def game_step(match_codes)
  return unless match_code_valid?(match_codes)

  @game_stage.step(@compare_codes.compare(match_codes))
  @game_stage.compare_result
end

#hint_showObject



52
53
54
55
56
57
# File 'lib/codebreaker/game.rb', line 52

def hint_show
  return if @hint_code.empty?

  @game_stage.hint_used += 1
  @hint_code.shift
end

#match_code_valid_length?(match_code) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/codebreaker/game.rb', line 18

def match_code_valid_length?(match_code)
  return true if validate_length?(match_code, CODE_LENGTH..CODE_LENGTH)

  (@errors[:match_code] = 'error_match_code_length') && false
end

#registration(username) ⇒ Object



47
48
49
50
# File 'lib/codebreaker/game.rb', line 47

def registration(username)
  @user = User.new(username)
  { status: @user.valid?, value: @user.errors.empty? ? @user.username : @user.errors[:user] }
end

#statistic_saveObject



59
60
61
62
# File 'lib/codebreaker/game.rb', line 59

def statistic_save
  @statistic.statistic_add_item(name: user.username, difficulty: @difficulty, game_stage: @game_stage)
  @statistic.statistic_save
end

#validate_match_code_number_range?(match_code) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/codebreaker/game.rb', line 24

def validate_match_code_number_range?(match_code)
  return true if validate_number_range?(match_code, CODE_NUMBERS)

  (@errors[:match_code] = 'error_match_code_number') && false
end