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 +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +4 -0
- data/Gemfile.lock +1 -1
- data/lib/codebreaker.rb +6 -0
- data/lib/codebreaker/difficulties/base.rb +4 -0
- data/lib/codebreaker/difficulties/easy.rb +4 -0
- data/lib/codebreaker/difficulties/hell.rb +4 -0
- data/lib/codebreaker/difficulties/medium.rb +4 -0
- data/lib/codebreaker/errors/game_save_error.rb +9 -0
- data/lib/codebreaker/file_storage.rb +16 -0
- data/lib/codebreaker/game.rb +20 -6
- data/lib/codebreaker/hint.rb +5 -1
- data/lib/codebreaker/secret_code_generator.rb +5 -0
- data/lib/codebreaker/stats.rb +37 -0
- data/lib/codebreaker/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaa6a62438cc6336ddc9329d0c56f9b070f52d66bdee469d0bd2ec5f16ed6a72
|
4
|
+
data.tar.gz: 3ef241b273c84403f66ff74521e3b6f234f7af9758f3ad96e40473f40815d561
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: acaed6517f2d08384eac662e39ae0ca174822572ca9efe6d19861b71c42403b402f9674605ddde7a0fde82cc6516632867fabc0936966f4f39808624d7b44435
|
7
|
+
data.tar.gz: e4da06e1b72430b70a7b3a669da965a65795dc6a0f620030e7aeb5dfd819b8423a44b9ed52ab89a30431ef298db370e48ad277a0db1112a3c28c2357e78a2d55
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
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
|
@@ -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
|
data/lib/codebreaker/game.rb
CHANGED
@@ -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.
|
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
|
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
|
-
|
35
|
+
def over?
|
36
|
+
win? || lose?
|
37
|
+
end
|
38
|
+
|
39
|
+
def hints_left
|
40
|
+
@hint.count
|
41
|
+
end
|
34
42
|
|
35
|
-
def
|
36
|
-
|
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
|
58
|
+
raise Errors::GameOverError if over?
|
45
59
|
end
|
46
60
|
|
47
61
|
def check_win(combination)
|
data/lib/codebreaker/hint.rb
CHANGED
@@ -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
|
data/lib/codebreaker/version.rb
CHANGED
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.
|
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-
|
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:
|