Class: Codebreaker::CodebreakerGem
- Includes:
- InitDifficulties
- Defined in:
- lib/codebreaker/game.rb
Constant Summary collapse
- CODE_LENGTH =
4- CODE_NUMBERS =
('1'..'6').freeze
Instance Attribute Summary collapse
-
#difficulty ⇒ Object
readonly
Returns the value of attribute difficulty.
-
#difficulty_change ⇒ Object
Returns the value of attribute difficulty_change.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#game_stage ⇒ Object
readonly
Returns the value of attribute game_stage.
-
#hint_code ⇒ Object
Returns the value of attribute hint_code.
-
#statistic ⇒ Object
readonly
Returns the value of attribute statistic.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #game_start ⇒ Object
- #game_step(user_code) ⇒ Object
- #hint_show ⇒ Object
-
#initialize ⇒ CodebreakerGem
constructor
A new instance of CodebreakerGem.
- #registration(username) ⇒ Object
- #statistic_save ⇒ Object
- #user_code_valid_length?(user_code) ⇒ Boolean
- #validate_user_code_number_range?(user_code) ⇒ Boolean
Methods included from InitDifficulties
Methods inherited from BaseClass
Methods included from Validator
#validate_length?, #validate_number_range?
Constructor Details
#initialize ⇒ CodebreakerGem
Returns a new instance of CodebreakerGem.
11 12 13 14 15 |
# File 'lib/codebreaker/game.rb', line 11 def initialize @errors = {} @statistic = Statistic.new @difficulty = init_difficulties end |
Instance Attribute Details
#difficulty ⇒ Object (readonly)
Returns the value of attribute difficulty.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def difficulty @difficulty end |
#difficulty_change ⇒ Object
Returns the value of attribute difficulty_change.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def difficulty_change @difficulty_change end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def errors @errors end |
#game_stage ⇒ Object (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_code ⇒ Object
Returns the value of attribute hint_code.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def hint_code @hint_code end |
#statistic ⇒ Object (readonly)
Returns the value of attribute statistic.
8 9 10 |
# File 'lib/codebreaker/game.rb', line 8 def statistic @statistic end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/codebreaker/game.rb', line 9 def user @user end |
Instance Method Details
#game_start ⇒ Object
33 34 35 36 37 |
# File 'lib/codebreaker/game.rb', line 33 def game_start generate_secret_code generate_hints unless @difficulty_change.nil? @game_stage = GameStage.new(user_code_length: CODE_LENGTH, attempts: @difficulty_change.attempts) end |
#game_step(user_code) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/codebreaker/game.rb', line 39 def game_step(user_code) return unless user_code_valid?(user_code) @user_code_positions = get_code_positions(user_code) @game_stage.step(compare_codes(user_code)) @game_stage.compare_result end |
#hint_show ⇒ Object
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 |
#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.first } end |
#statistic_save ⇒ Object
59 60 61 62 |
# File 'lib/codebreaker/game.rb', line 59 def statistic_save @statistic.statistic_add_item(name: user.username, difficulty: difficulty_change, game_stage: game_stage) @statistic.statistic_save end |
#user_code_valid_length?(user_code) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/codebreaker/game.rb', line 17 def user_code_valid_length?(user_code) return true if validate_length?(user_code, CODE_LENGTH..CODE_LENGTH) false || @errors[:user_code] = 'error_user_code_length' end |
#validate_user_code_number_range?(user_code) ⇒ Boolean
23 24 25 26 27 |
# File 'lib/codebreaker/game.rb', line 23 def validate_user_code_number_range?(user_code) return true if validate_number_range?(user_code, CODE_NUMBERS) false || @errors[:user_code] = 'error_user_code_number' end |