cb-core 0.1.0 → 0.1.6

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: 9356fd9f962bb5aa804f0958aa570a2911389c1581dd0444f36f47348b7283ea
4
- data.tar.gz: 166ae692e5475313779e355471a303311b7aab05903fba10789074f26dd0596b
3
+ metadata.gz: eaa6a62438cc6336ddc9329d0c56f9b070f52d66bdee469d0bd2ec5f16ed6a72
4
+ data.tar.gz: 3ef241b273c84403f66ff74521e3b6f234f7af9758f3ad96e40473f40815d561
5
5
  SHA512:
6
- metadata.gz: 42a5c4f64ba179ec63ccab33aac5ecae75756c7db11a3def7453aa9c936d33c5617c7891fac1f80f47d42bd3967a88314e25f0098f876d56e705af517c60a474
7
- data.tar.gz: 4273d181bf0a867764099f0851fa20a1ec35d06b53a59243eed88ed1ca15aab1e29b953f9406abbba5dbae32d29a91a018d2e5051dc373fdc03319ea963f267e
6
+ metadata.gz: acaed6517f2d08384eac662e39ae0ca174822572ca9efe6d19861b71c42403b402f9674605ddde7a0fde82cc6516632867fabc0936966f4f39808624d7b44435
7
+ data.tar.gz: e4da06e1b72430b70a7b3a669da965a65795dc6a0f620030e7aeb5dfd819b8423a44b9ed52ab89a30431ef298db370e48ad277a0db1112a3c28c2357e78a2d55
data/.gitignore CHANGED
@@ -10,4 +10,7 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
 
13
+
14
+ *.gem
15
+
13
16
  .idea
data/.rubocop.yml CHANGED
@@ -12,6 +12,10 @@ Metrics/BlockLength:
12
12
  Exclude:
13
13
  - 'spec/**/*.rb'
14
14
 
15
+ Metrics/ModuleLength:
16
+ Exclude:
17
+ - 'spec/**/*.rb'
18
+
15
19
  Style/Documentation:
16
20
  Enabled: false
17
21
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cb-core (0.1.0)
4
+ cb-core (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/codebreaker.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'fileutils'
2
+ require 'yaml'
3
+
1
4
  require_relative 'codebreaker/difficulty_factory'
2
5
  require_relative 'codebreaker/difficulty_type'
3
6
  require_relative 'codebreaker/difficulties/base'
@@ -5,15 +8,18 @@ require_relative 'codebreaker/difficulties/easy'
5
8
  require_relative 'codebreaker/difficulties/medium'
6
9
  require_relative 'codebreaker/difficulties/hell'
7
10
  require_relative 'codebreaker/errors/game_over_error'
11
+ require_relative 'codebreaker/errors/game_save_error'
8
12
  require_relative 'codebreaker/errors/invalid_difficulty_error'
9
13
  require_relative 'codebreaker/errors/no_hints_error'
10
14
  require_relative 'codebreaker/errors/validation_length_error'
15
+ require_relative 'codebreaker/file_storage'
11
16
  require_relative 'codebreaker/game'
12
17
  require_relative 'codebreaker/hint'
13
18
  require_relative 'codebreaker/mixins/validatable'
14
19
  require_relative 'codebreaker/player'
15
20
  require_relative 'codebreaker/secret_code_generator'
16
21
  require_relative 'codebreaker/secret_code_matcher'
22
+ require_relative 'codebreaker/stats'
17
23
  require_relative 'codebreaker/version'
18
24
 
19
25
  module Codebreaker
@@ -1,6 +1,10 @@
1
1
  module Codebreaker
2
2
  module Difficulties
3
3
  class Base
4
+ def type
5
+ raise NotImplementedError
6
+ end
7
+
4
8
  def attempts
5
9
  raise NotImplementedError
6
10
  end
@@ -1,6 +1,10 @@
1
1
  module Codebreaker
2
2
  module Difficulties
3
3
  class Easy < Base
4
+ def type
5
+ DifficultyType::EASY
6
+ end
7
+
4
8
  def attempts
5
9
  15
6
10
  end
@@ -1,6 +1,10 @@
1
1
  module Codebreaker
2
2
  module Difficulties
3
3
  class Hell < Base
4
+ def type
5
+ DifficultyType::HELL
6
+ end
7
+
4
8
  def attempts
5
9
  5
6
10
  end
@@ -1,6 +1,10 @@
1
1
  module Codebreaker
2
2
  module Difficulties
3
3
  class Medium < Base
4
+ def type
5
+ DifficultyType::MEDIUM
6
+ end
7
+
4
8
  def attempts
5
9
  10
6
10
  end
@@ -0,0 +1,9 @@
1
+ module Codebreaker
2
+ module Errors
3
+ class GameSaveError < StandardError
4
+ def initialize(msg = 'Only won game could be saved')
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Codebreaker
2
+ class FileStorage
3
+ def initialize(path)
4
+ @path = path
5
+ end
6
+
7
+ def load
8
+ File.file?(@path) ? YAML.load_file(@path) : nil
9
+ end
10
+
11
+ def save(data)
12
+ Pathname(@path).dirname.mkpath
13
+ File.write(@path, data.to_yaml)
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,7 @@
1
1
  module Codebreaker
2
2
  class Game
3
+ attr_reader :secret_code, :player, :difficulty
4
+
3
5
  def initialize(player, difficulty_type)
4
6
  @player = player
5
7
  @secret_code = SecretCodeGenerator.new.call
@@ -11,7 +13,7 @@ module Codebreaker
11
13
 
12
14
  def take_hint
13
15
  ensure_game_is_not_over
14
- @hint.call
16
+ @hint.take
15
17
  end
16
18
 
17
19
  def guess_secret_code(guess)
@@ -22,7 +24,7 @@ module Codebreaker
22
24
  matching
23
25
  end
24
26
 
25
- def loose?
27
+ def lose?
26
28
  no_attempts? && !win?
27
29
  end
28
30
 
@@ -30,18 +32,30 @@ module Codebreaker
30
32
  @is_win
31
33
  end
32
34
 
33
- private
35
+ def over?
36
+ win? || lose?
37
+ end
38
+
39
+ def hints_left
40
+ @hint.count
41
+ end
34
42
 
35
- def game_over?
36
- win? || loose?
43
+ def attempts_left
44
+ @difficulty.attempts - @attempts_used
37
45
  end
38
46
 
47
+ def save_result
48
+ Stats.new.save_game(self)
49
+ end
50
+
51
+ private
52
+
39
53
  def no_attempts?
40
54
  @difficulty.attempts == @attempts_used
41
55
  end
42
56
 
43
57
  def ensure_game_is_not_over
44
- raise Errors::GameOverError if game_over?
58
+ raise Errors::GameOverError if over?
45
59
  end
46
60
 
47
61
  def check_win(combination)
@@ -5,10 +5,14 @@ module Codebreaker
5
5
  @hints = secret_code.sample(difficulty.hints)
6
6
  end
7
7
 
8
- def call
8
+ def take
9
9
  raise Errors::NoHintsError if @hints.empty?
10
10
 
11
11
  @hints.pop
12
12
  end
13
+
14
+ def count
15
+ @hints.count
16
+ end
13
17
  end
14
18
  end
@@ -2,6 +2,11 @@ module Codebreaker
2
2
  class SecretCodeGenerator
3
3
  LENGTH = 4
4
4
  RANGE = (1..6).freeze
5
+ VALID_REGEXP = /^[#{RANGE.min}-#{RANGE.max}]{#{LENGTH}}$/.freeze
6
+
7
+ def self.string_valid?(secret_code)
8
+ VALID_REGEXP.match?(secret_code)
9
+ end
5
10
 
6
11
  def call
7
12
  Array.new(LENGTH) { rand(RANGE) }
@@ -0,0 +1,37 @@
1
+ module Codebreaker
2
+ class Stats
3
+ PATH = './db/data.yml'.freeze
4
+
5
+ def initialize
6
+ @storage = FileStorage.new(PATH)
7
+ end
8
+
9
+ def load
10
+ @storage.load || []
11
+ end
12
+
13
+ def save_game(game)
14
+ validate_game_is_win(game)
15
+ data = load
16
+ data << game_data(game)
17
+ @storage.save(data)
18
+ end
19
+
20
+ private
21
+
22
+ def game_data(game)
23
+ {
24
+ name: game.player.name,
25
+ difficulty: game.difficulty.type,
26
+ attempts_total: game.difficulty.attempts,
27
+ attempts_used: game.difficulty.attempts - game.attempts_left,
28
+ hints_total: game.difficulty.hints,
29
+ hints_used: game.difficulty.hints - game.hints_left
30
+ }
31
+ end
32
+
33
+ def validate_game_is_win(game)
34
+ raise Errors::GameSaveError unless game.win?
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Codebreaker
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.6'.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.0
4
+ version: 0.1.6
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-03-28 00:00:00.000000000 Z
11
+ date: 2021-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -137,15 +137,18 @@ files:
137
137
  - lib/codebreaker/difficulty_factory.rb
138
138
  - lib/codebreaker/difficulty_type.rb
139
139
  - lib/codebreaker/errors/game_over_error.rb
140
+ - lib/codebreaker/errors/game_save_error.rb
140
141
  - lib/codebreaker/errors/invalid_difficulty_error.rb
141
142
  - lib/codebreaker/errors/no_hints_error.rb
142
143
  - lib/codebreaker/errors/validation_length_error.rb
144
+ - lib/codebreaker/file_storage.rb
143
145
  - lib/codebreaker/game.rb
144
146
  - lib/codebreaker/hint.rb
145
147
  - lib/codebreaker/mixins/validatable.rb
146
148
  - lib/codebreaker/player.rb
147
149
  - lib/codebreaker/secret_code_generator.rb
148
150
  - lib/codebreaker/secret_code_matcher.rb
151
+ - lib/codebreaker/stats.rb
149
152
  - lib/codebreaker/version.rb
150
153
  homepage: https://github.com/YakupovDima/rubygarage-codebreaker-core
151
154
  licenses: