Class: Codebreaker::Game

Inherits:
BaseClass show all
Includes:
CompareCodes, 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 CompareCodes

#compare, #crossing_code_position, #get_code_positions, #get_cross_value, #guess_position, #guess_value

Methods included from InitDifficulties

#init_difficulties

Methods inherited from BaseClass

#valid?

Methods included from Validator

#validate_length?, #validate_number_range?

Constructor Details

#initializeGame



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

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

Instance Attribute Details

#difficultiesObject (readonly)

Returns the value of attribute difficulties.



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

def difficulties
  @difficulties
end

#difficultyObject

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#game_stageObject (readonly)

Returns the value of attribute game_stage.



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

def game_stage
  @game_stage
end

#hint_codeObject

Returns the value of attribute hint_code.



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

def hint_code
  @hint_code
end

#statisticObject (readonly)

Returns the value of attribute statistic.



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

def statistic
  @statistic
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#game_startObject



38
39
40
41
42
# File 'lib/codebreaker/game.rb', line 38

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

#game_step(match_codes) ⇒ Object



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

def game_step(match_codes)
  return unless match_code_valid?(match_codes)
  return unless @game_stage.valide_allow_step?

  @game_stage.step(compare(@secret_code, match_codes))
  @game_stage.compare_result
end

#hint_showObject



61
62
63
64
65
66
# File 'lib/codebreaker/game.rb', line 61

def hint_show
  return if @hint_code.empty?

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

#match_code_valid_length?(match_code) ⇒ Boolean



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

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



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

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

#statistic_saveObject



68
69
70
71
# File 'lib/codebreaker/game.rb', line 68

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



26
27
28
29
30
31
# File 'lib/codebreaker/game.rb', line 26

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