cb-core 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10d77f2204399e688b8fdc7682608c67a20635e2081089bb19ccb67e937ab8e5
4
- data.tar.gz: 8b1beda09989c6570219bd385d4f280772c62990471367b3d561a36357cae59d
3
+ metadata.gz: 177b156f01c5212b23c6bef720dc6962a5103bd09dd79f9df40b84dd3312c57c
4
+ data.tar.gz: eb3c47dd6140adb217d3e64d4bf0c69446a555dd63944db0c0d09f04d29743c2
5
5
  SHA512:
6
- metadata.gz: cc43320fc01a5bd588122b13ef482017b8a067efeccd37d53fa1e530477cea056e99e00b349ecfdfa9a0ba12b22b11ad4b99f0e957fbf0f05b204fb7974506b3
7
- data.tar.gz: 62b52ad66559cfbb93e49d3269ef87e20db0673cf3aff4885d88ab81aac26829ccb3876d1728b43b668f1326f3253050ba202da2cefbc0979bc359de01531aae
6
+ metadata.gz: 9bd350d5b79d20591e5bd3911a9c4f13817f1639af878ec8d2d520a7eb3116c9e8d74800389c2669c5874c9bc55c587c764e6bece85d53385111ba96954f71ca
7
+ data.tar.gz: 1a0db0f64f89eb34ce758b84080c3b588663500e3199b2644232948bb07270a972a5c6946503769bfcc8f22ea8befb51d3f9f9af0c638128546797fd3dc6009d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cb-core (0.1.8)
4
+ cb-core (0.1.9)
5
5
  i18n (~> 1.8.9)
6
6
 
7
7
  GEM
@@ -1,4 +1,7 @@
1
1
  en:
2
2
  errors:
3
- invalid_difficulty: "unknown difficulty type %{type}"
4
- validation_length: "invalid length '%{value}'. min=%{min}, max=%{max}"
3
+ validation_length_common: Invalid string length
4
+ validation_length_detailed: "Invalid length '%{value}'. min=%{min}, max=%{max}"
5
+ game_over: No attempts available or a player won the game
6
+ game_save: Only won game could be saved
7
+ no_hints: No hints available
data/lib/codebreaker.rb CHANGED
@@ -4,12 +4,8 @@ require 'i18n'
4
4
 
5
5
  require_relative '../config/i18n'
6
6
 
7
- require_relative 'codebreaker/difficulty'
8
- require_relative 'codebreaker/difficulty_factory'
9
- require_relative 'codebreaker/difficulty_type'
10
7
  require_relative 'codebreaker/errors/game_over_error'
11
8
  require_relative 'codebreaker/errors/game_save_error'
12
- require_relative 'codebreaker/errors/invalid_difficulty_error'
13
9
  require_relative 'codebreaker/errors/no_hints_error'
14
10
  require_relative 'codebreaker/errors/validation_length_error'
15
11
  require_relative 'codebreaker/create_stat_service'
@@ -13,11 +13,11 @@ module Codebreaker
13
13
  def stat_data
14
14
  {
15
15
  name: @game.player.name,
16
- difficulty: @game.difficulty.type,
17
- attempts_total: @game.difficulty.attempts,
18
- attempts_used: @game.difficulty.attempts - @game.attempts_left,
19
- hints_total: @game.difficulty.hints,
20
- hints_used: @game.difficulty.hints - @game.hints_left
16
+ difficulty: @game.difficulty_type,
17
+ attempts_total: @game.attempts_total,
18
+ attempts_used: @game.attempts_total - @game.attempts_left,
19
+ hints_total: @game.hints_total,
20
+ hints_used: @game.hints_total - @game.hints_left
21
21
  }
22
22
  end
23
23
  end
@@ -1,7 +1,7 @@
1
1
  module Codebreaker
2
2
  module Errors
3
3
  class GameOverError < StandardError
4
- def initialize(msg = 'No attempts available or a player won the game')
4
+ def initialize(msg = I18n.t('errors.game_over'))
5
5
  super
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module Codebreaker
2
2
  module Errors
3
3
  class GameSaveError < StandardError
4
- def initialize(msg = 'Only won game could be saved')
4
+ def initialize(msg = I18n.t('errors.game_save'))
5
5
  super
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module Codebreaker
2
2
  module Errors
3
3
  class NoHintsError < StandardError
4
- def initialize(msg = 'No hints available')
4
+ def initialize(msg = I18n.t('errors.no_hints'))
5
5
  super
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  module Codebreaker
2
2
  module Errors
3
3
  class ValidationLengthError < StandardError
4
- def initialize(msg = 'Invalid string length')
4
+ def initialize(msg = I18n.t('errors.validation_length_common'))
5
5
  super
6
6
  end
7
7
  end
@@ -1,11 +1,17 @@
1
1
  module Codebreaker
2
2
  class Game
3
- attr_reader :player, :attempts_left
3
+ attr_reader :player, :difficulty_type, :attempts_left
4
+
5
+ DIFFICULTIES = {
6
+ easy: { attempts: 15, hints: 2, hardness: 1 },
7
+ medium: { attempts: 10, hints: 1, hardness: 2 },
8
+ hell: { attempts: 5, hints: 1, hardness: 3 }
9
+ }.freeze
4
10
 
5
11
  def initialize(player, difficulty_type)
6
12
  @player = player
7
13
  @difficulty_type = difficulty_type
8
- @attempts_left = difficulty.attempts
14
+ @attempts_left = difficulty[:attempts]
9
15
  @is_win = false
10
16
  end
11
17
 
@@ -13,10 +19,6 @@ module Codebreaker
13
19
  @secret_code ||= SecretCodeGenerator.new.call
14
20
  end
15
21
 
16
- def difficulty
17
- @difficulty ||= DifficultyFactory.build(@difficulty_type)
18
- end
19
-
20
22
  def take_hint
21
23
  ensure_game_is_not_over
22
24
  raise Errors::NoHintsError if hints.empty?
@@ -33,7 +35,7 @@ module Codebreaker
33
35
  end
34
36
 
35
37
  def lose?
36
- no_attempts? && !win?
38
+ attempts_left.zero? && !win?
37
39
  end
38
40
 
39
41
  def win?
@@ -44,6 +46,14 @@ module Codebreaker
44
46
  win? || lose?
45
47
  end
46
48
 
49
+ def attempts_total
50
+ difficulty[:attempts]
51
+ end
52
+
53
+ def hints_total
54
+ difficulty[:hints]
55
+ end
56
+
47
57
  def hints_left
48
58
  hints.count
49
59
  end
@@ -56,12 +66,12 @@ module Codebreaker
56
66
 
57
67
  private
58
68
 
59
- def hints
60
- @hints ||= secret_code.sample(difficulty.hints)
69
+ def difficulty
70
+ DIFFICULTIES[@difficulty_type]
61
71
  end
62
72
 
63
- def no_attempts?
64
- attempts_left.zero?
73
+ def hints
74
+ @hints ||= secret_code.sample(difficulty[:hints])
65
75
  end
66
76
 
67
77
  def ensure_game_is_not_over
@@ -5,7 +5,7 @@ module Codebreaker
5
5
  return if value.instance_of?(String) && value.size.between?(min, max)
6
6
 
7
7
  raise Codebreaker::Errors::ValidationLengthError,
8
- I18n.t('errors.validation_length', value: value, min: min, max: max)
8
+ I18n.t('errors.validation_length_detailed', value: value, min: min, max: max)
9
9
  end
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module Codebreaker
2
- VERSION = '0.1.8'.freeze
2
+ VERSION = '0.1.9'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cb-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmytro Yakupov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-23 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -147,12 +147,8 @@ files:
147
147
  - config/locales/en.yml
148
148
  - lib/codebreaker.rb
149
149
  - lib/codebreaker/create_stat_service.rb
150
- - lib/codebreaker/difficulty.rb
151
- - lib/codebreaker/difficulty_factory.rb
152
- - lib/codebreaker/difficulty_type.rb
153
150
  - lib/codebreaker/errors/game_over_error.rb
154
151
  - lib/codebreaker/errors/game_save_error.rb
155
- - lib/codebreaker/errors/invalid_difficulty_error.rb
156
152
  - lib/codebreaker/errors/no_hints_error.rb
157
153
  - lib/codebreaker/errors/validation_length_error.rb
158
154
  - lib/codebreaker/game.rb
@@ -1,11 +0,0 @@
1
- module Codebreaker
2
- class Difficulty
3
- attr_reader :type, :attempts, :hints
4
-
5
- def initialize(type, attempts, hints)
6
- @type = type
7
- @attempts = attempts
8
- @hints = hints
9
- end
10
- end
11
- end
@@ -1,16 +0,0 @@
1
- module Codebreaker
2
- class DifficultyFactory
3
- def self.build(type)
4
- case type
5
- when DifficultyType::EASY
6
- Difficulty.new(DifficultyType::EASY, 15, 2)
7
- when DifficultyType::MEDIUM
8
- Difficulty.new(DifficultyType::MEDIUM, 10, 1)
9
- when DifficultyType::HELL
10
- Difficulty.new(DifficultyType::HELL, 5, 1)
11
- else
12
- raise Errors::InvalidDifficultyError, I18n.t('errors.invalid_difficulty', type: type)
13
- end
14
- end
15
- end
16
- end
@@ -1,7 +0,0 @@
1
- module Codebreaker
2
- module DifficultyType
3
- EASY = :easy
4
- MEDIUM = :medium
5
- HELL = :hell
6
- end
7
- end
@@ -1,9 +0,0 @@
1
- module Codebreaker
2
- module Errors
3
- class InvalidDifficultyError < StandardError
4
- def initialize(msg = 'Invalid difficulty type')
5
- super
6
- end
7
- end
8
- end
9
- end