code_brkr_game_training 0.8.0 → 0.8.5
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/.gitignore +2 -1
- data/Gemfile.lock +1 -1
- data/lib/code_brkr_game_training/bootstrap/autoloader.rb +1 -0
- data/lib/code_brkr_game_training/entities/code.rb +1 -42
- data/lib/code_brkr_game_training/entities/code_comparator.rb +59 -0
- data/lib/code_brkr_game_training/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: cc535e51ce2bc210cbf4b79d3efc62bd1a2eb558968ba60f10417337d306e6b6
|
4
|
+
data.tar.gz: af72a567cfec97ada4cf01476a8a63c82ee233a09f424c68f2174889de454cf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55605b66b06f003685c9ed343e0649c050b873b59916583eb679ce2ba0d9c700162fe8e9712c3d52045167be10afa90fd1bd728089949e755ea2292fc40f022a
|
7
|
+
data.tar.gz: 2f30fa6028b171aaad2f8fe84b519a3978ee34f1be0dad8634eece3f87d08a30ef497b33623103fc374086cf42991e99f39bcc4ef00f85603783d51d5563fc16
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -9,6 +9,7 @@ require_relative '../modules/validator'
|
|
9
9
|
|
10
10
|
require_relative '../entities/user'
|
11
11
|
require_relative '../entities/difficulty_controller'
|
12
|
+
require_relative '../entities/code_comparator'
|
12
13
|
require_relative '../entities/code'
|
13
14
|
require_relative '../modules/statistics'
|
14
15
|
require_relative '../entities/game'
|
@@ -4,26 +4,14 @@
|
|
4
4
|
module CodeBrkrGameTraining
|
5
5
|
# Class class for storage and operations with secret code
|
6
6
|
class Code
|
7
|
-
include Validator
|
8
|
-
|
9
7
|
GAME_NUMBERS = { from: 1, to: 6, count: 4 }.freeze
|
10
8
|
|
11
9
|
def initialize
|
12
10
|
@secret_digit_arr = generate_code
|
13
11
|
end
|
14
12
|
|
15
|
-
def to_s
|
16
|
-
@secret_digit_arr.join
|
17
|
-
end
|
18
|
-
|
19
13
|
def code_check(user_digits_arr)
|
20
|
-
|
21
|
-
|
22
|
-
{
|
23
|
-
in_positions: check_in_positions(user_digits_arr),
|
24
|
-
out_of_positions: check_out_positions(user_digits_arr),
|
25
|
-
not_guessed: check_not_guessed(user_digits_arr)
|
26
|
-
}
|
14
|
+
CodeComparator.new(@secret_digit_arr, user_digits_arr).compare_codes
|
27
15
|
end
|
28
16
|
|
29
17
|
def random_digit(exclude_indexes)
|
@@ -36,34 +24,5 @@ module CodeBrkrGameTraining
|
|
36
24
|
def generate_code
|
37
25
|
Array.new(GAME_NUMBERS[:count]) { rand(GAME_NUMBERS[:from]..GAME_NUMBERS[:to]) }
|
38
26
|
end
|
39
|
-
|
40
|
-
def check_array_code_format(code_arr)
|
41
|
-
check_type(code_arr, Array)
|
42
|
-
raise GameError, 'incorrect_code_format' unless code_arr.size == GAME_NUMBERS[:count]
|
43
|
-
|
44
|
-
digits_range_arr = (GAME_NUMBERS[:from]..GAME_NUMBERS[:to]).to_a
|
45
|
-
code_arr.each { |digit| raise GameError, 'incorrect_code_format' unless digits_range_arr.member? digit }
|
46
|
-
end
|
47
|
-
|
48
|
-
def check_miss_indexes(user_arr)
|
49
|
-
GAME_NUMBERS[:count].times.reject { |user_index| user_arr[user_index] == @secret_digit_arr[user_index] }
|
50
|
-
end
|
51
|
-
|
52
|
-
def check_in_positions(user_arr)
|
53
|
-
GAME_NUMBERS[:count] - check_miss_indexes(user_arr).length
|
54
|
-
end
|
55
|
-
|
56
|
-
def check_out_positions(user_arr)
|
57
|
-
miss_indexes = check_miss_indexes(user_arr)
|
58
|
-
system_digits = @secret_digit_arr.select.with_index { |_, system_index| miss_indexes.include? system_index }
|
59
|
-
miss_indexes.collect do |miss_index|
|
60
|
-
found = system_digits.find_index { |system_num| user_arr[miss_index] == system_num }
|
61
|
-
found.nil? ? nil : system_digits.delete_at(found)
|
62
|
-
end.compact.length
|
63
|
-
end
|
64
|
-
|
65
|
-
def check_not_guessed(user_arr)
|
66
|
-
GAME_NUMBERS[:count] - check_in_positions(user_arr) - check_out_positions(user_arr)
|
67
|
-
end
|
68
27
|
end
|
69
28
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Main Gem module
|
4
|
+
module CodeBrkrGameTraining
|
5
|
+
# Class for comparing the hidden code and the code entered by the user
|
6
|
+
class CodeComparator
|
7
|
+
include Validator
|
8
|
+
|
9
|
+
def initialize(secret_code, user_code)
|
10
|
+
check_code_format(user_code)
|
11
|
+
|
12
|
+
@secret_code = secret_code
|
13
|
+
@user_code = user_code
|
14
|
+
end
|
15
|
+
|
16
|
+
def compare_codes
|
17
|
+
{
|
18
|
+
in_positions: check_in_positions,
|
19
|
+
out_of_positions: check_out_positions,
|
20
|
+
not_guessed: check_not_guessed
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def check_code_format(code)
|
27
|
+
check_type(code, Array)
|
28
|
+
raise GameError, 'incorrect_code_format' unless code.size == Code::GAME_NUMBERS[:count]
|
29
|
+
|
30
|
+
digits_range_array = (Code::GAME_NUMBERS[:from]..Code::GAME_NUMBERS[:to]).to_a
|
31
|
+
code.each { |digit| raise GameError, 'incorrect_code_format' unless digits_range_array.member? digit }
|
32
|
+
end
|
33
|
+
|
34
|
+
def check_miss_indexes
|
35
|
+
@check_miss_indexes ||= Code::GAME_NUMBERS[:count].times.reject do |user_index|
|
36
|
+
@user_code[user_index] == @secret_code[user_index]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def check_in_positions
|
41
|
+
@check_in_positions ||= Code::GAME_NUMBERS[:count] - check_miss_indexes.length
|
42
|
+
end
|
43
|
+
|
44
|
+
def check_out_positions
|
45
|
+
return @check_out_positions if @check_out_positions
|
46
|
+
|
47
|
+
miss_indexes = check_miss_indexes
|
48
|
+
system_digits = @secret_code.select.with_index { |_, system_index| miss_indexes.include? system_index }
|
49
|
+
@check_out_positions = miss_indexes.collect do |miss_index|
|
50
|
+
found = system_digits.find_index { |system_num| @user_code[miss_index] == system_num }
|
51
|
+
found.nil? ? nil : system_digits.delete_at(found)
|
52
|
+
end.compact.length
|
53
|
+
end
|
54
|
+
|
55
|
+
def check_not_guessed
|
56
|
+
Code::GAME_NUMBERS[:count] - check_in_positions - check_out_positions
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_brkr_game_training
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- lib/code_brkr_game_training.rb
|
133
133
|
- lib/code_brkr_game_training/bootstrap/autoloader.rb
|
134
134
|
- lib/code_brkr_game_training/entities/code.rb
|
135
|
+
- lib/code_brkr_game_training/entities/code_comparator.rb
|
135
136
|
- lib/code_brkr_game_training/entities/difficulty_controller.rb
|
136
137
|
- lib/code_brkr_game_training/entities/game.rb
|
137
138
|
- lib/code_brkr_game_training/entities/user.rb
|