codebreaker_kub 0.2.1 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +1 -1
- data/lib/codebreaker.rb +1 -0
- data/lib/codebreaker/game.rb +26 -33
- data/lib/codebreaker/guess_checker.rb +43 -0
- data/lib/codebreaker/loader.rb +6 -3
- data/lib/codebreaker/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34ece6de532a41fce0ebb9bdc5c43b78967db6d7685030efbfecddc76189d846
|
4
|
+
data.tar.gz: 64c3f7249d16a91c5244f431d20ddd8d60de2dafdc82a3997c79168b10afa5c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1820cb27ce2f71ecf10738033bcb81b64a0c9f7e914e4af57d4a2fc565af045a33ed9d14d24a681ccbf94935bcfeb01cd1e9536255806f7f6e7da7e3d1aea772
|
7
|
+
data.tar.gz: 2c55801f6013e94165389014838eec3e94afdbad7d1fe2589f83e44000c58aaaceefe062d7e3c101ffbf6f70a73863989f879bc2309394825bd056c8d22a1718
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/lib/codebreaker.rb
CHANGED
data/lib/codebreaker/game.rb
CHANGED
@@ -2,25 +2,24 @@
|
|
2
2
|
|
3
3
|
module Codebreaker
|
4
4
|
class Game
|
5
|
+
attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
|
6
|
+
:all_attempts
|
7
|
+
attr_reader :hints_code
|
8
|
+
|
5
9
|
CODE_LENGTH = 4
|
6
10
|
CODE_RANGE = (1..6).freeze
|
7
|
-
|
8
|
-
attr_reader :minuse, :plus, :none
|
11
|
+
|
9
12
|
def initialize
|
10
13
|
@difficulties = Codebreaker::Loader.load('difficulties')
|
11
14
|
@code = generate_code
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def symbols(minuse = '-', plus = '+', none = '')
|
16
|
-
@minuse = minuse
|
17
|
-
@plus = plus
|
18
|
-
@none = none
|
15
|
+
@hints_code = @code.chars.shuffle
|
19
16
|
end
|
20
17
|
|
21
18
|
def game_option(name, difficulty)
|
22
19
|
@name = name
|
23
20
|
@difficulty = difficulty
|
21
|
+
@all_attempts = difficulty_option[:attempts]
|
22
|
+
@all_hints = difficulty_option[:hints]
|
24
23
|
@attempts_left = difficulty_option[:attempts]
|
25
24
|
@hints_left = difficulty_option[:hints]
|
26
25
|
end
|
@@ -33,7 +32,7 @@ module Codebreaker
|
|
33
32
|
end
|
34
33
|
|
35
34
|
def hints_left?
|
36
|
-
|
35
|
+
hints_left.positive?
|
37
36
|
end
|
38
37
|
|
39
38
|
def input_operation(input_code)
|
@@ -41,57 +40,51 @@ module Codebreaker
|
|
41
40
|
return unless attempts_left?
|
42
41
|
|
43
42
|
@attempts_left -= 1
|
44
|
-
check_input
|
43
|
+
check_input(@code, @input_code)
|
45
44
|
end
|
46
45
|
|
47
46
|
def attempts_left?
|
48
|
-
|
47
|
+
attempts_left.positive?
|
49
48
|
end
|
50
49
|
|
51
50
|
def win?
|
52
|
-
|
51
|
+
input_code == code
|
53
52
|
end
|
54
53
|
|
55
|
-
def save
|
56
|
-
Codebreaker::Loader.save(to_h,
|
54
|
+
def save(filename = 'stats')
|
55
|
+
Codebreaker::Loader.save(to_h, filename)
|
57
56
|
end
|
58
57
|
|
59
58
|
private
|
60
59
|
|
61
|
-
def check_input(code = @code.chars)
|
62
|
-
input = @input_code.chars
|
63
|
-
minuses = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
|
64
|
-
result = @minuse * minuses
|
65
|
-
input.each.with_index do |element, index|
|
66
|
-
result.sub!(@minuse, @plus) if element == code[index]
|
67
|
-
end
|
68
|
-
return result unless result.empty?
|
69
|
-
|
70
|
-
@none
|
71
|
-
end
|
72
|
-
|
73
60
|
def generate_code
|
74
61
|
Array.new(CODE_LENGTH) { rand(CODE_RANGE) }.join
|
75
62
|
end
|
76
63
|
|
77
64
|
def generate_hint
|
78
|
-
|
79
|
-
hint_code.pop
|
65
|
+
@hints_code.pop
|
80
66
|
end
|
81
67
|
|
82
68
|
def difficulty_option
|
83
69
|
@difficulties[@difficulty]
|
84
70
|
end
|
85
71
|
|
72
|
+
def check_input(code, input)
|
73
|
+
guess_checker = Codebreaker::GuessChecker.new(code, input)
|
74
|
+
guess_checker.symbols
|
75
|
+
guess_checker.check_input
|
76
|
+
end
|
77
|
+
|
86
78
|
def to_h
|
87
79
|
{
|
88
80
|
name: @name,
|
89
81
|
difficulty: @difficulty,
|
90
|
-
attempts:
|
91
|
-
hints:
|
82
|
+
attempts: @all_attempts,
|
83
|
+
hints: @all_hints,
|
92
84
|
code: @code,
|
93
|
-
used_attempts:
|
94
|
-
used_hints:
|
85
|
+
used_attempts: @all_attempts - @attempts_left,
|
86
|
+
used_hints: @all_hints - @hints_left,
|
87
|
+
date: Time.now
|
95
88
|
}
|
96
89
|
end
|
97
90
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class GuessChecker
|
5
|
+
attr_accessor :inexact, :exact, :none
|
6
|
+
|
7
|
+
MINUSE = '-'
|
8
|
+
PLUS = '+'
|
9
|
+
NONE = ' '
|
10
|
+
|
11
|
+
def initialize(code, code_input)
|
12
|
+
@code = code.chars
|
13
|
+
@code_input = code_input.chars
|
14
|
+
end
|
15
|
+
|
16
|
+
def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
|
17
|
+
@inexact = inexact
|
18
|
+
@exact = exact
|
19
|
+
@none = none
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_input
|
23
|
+
raw_result = mismatched
|
24
|
+
matched(raw_result)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def mismatched
|
30
|
+
inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
|
31
|
+
@inexact * inexact
|
32
|
+
end
|
33
|
+
|
34
|
+
def matched(result)
|
35
|
+
@code_input.each.with_index do |element, index|
|
36
|
+
result.sub!(@inexact, @exact) if element == @code[index]
|
37
|
+
end
|
38
|
+
return result unless result.empty?
|
39
|
+
|
40
|
+
@none
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/codebreaker/loader.rb
CHANGED
@@ -2,10 +2,12 @@
|
|
2
2
|
|
3
3
|
module Codebreaker
|
4
4
|
module Loader
|
5
|
-
PATH = File.
|
5
|
+
# PATH = File.expand_path("../lib/data/#{file_name}", __FILE__)
|
6
|
+
# PATH = File.join(File.dirname(__FILE__), 'data/').freeze
|
6
7
|
EXTENCTION = '.yml'
|
7
8
|
def self.load(file_name)
|
8
|
-
|
9
|
+
path = File.expand_path("./data/#{file_name}", __dir__)
|
10
|
+
file_name = path + EXTENCTION.to_s
|
9
11
|
if File.exist?(file_name)
|
10
12
|
YAML.load_file(file_name)
|
11
13
|
else
|
@@ -15,7 +17,8 @@ module Codebreaker
|
|
15
17
|
end
|
16
18
|
|
17
19
|
def self.save(obj, file_name)
|
18
|
-
|
20
|
+
path = File.expand_path("./data/#{file_name}", __dir__)
|
21
|
+
file_name = path + EXTENCTION.to_s
|
19
22
|
stats = File.file?(file_name) && !File.zero?(file_name) ? YAML.load_file(file_name) : []
|
20
23
|
stats << obj
|
21
24
|
file = File.open(file_name, 'w')
|
data/lib/codebreaker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_kub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- katia kub
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/codebreaker/data/difficulties.yml
|
121
121
|
- lib/codebreaker/data/locales/en.yml
|
122
122
|
- lib/codebreaker/game.rb
|
123
|
+
- lib/codebreaker/guess_checker.rb
|
123
124
|
- lib/codebreaker/loader.rb
|
124
125
|
- lib/codebreaker/output.rb
|
125
126
|
- lib/codebreaker/validation.rb
|