andrii_codebreaker 0.1.7 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -3
- data/Gemfile.lock +3 -5
- data/andrii_codebreaker.gemspec +1 -1
- data/lib/andrii_codebreaker/constants.rb +2 -0
- data/lib/andrii_codebreaker/difficulty.rb +9 -12
- data/lib/andrii_codebreaker/game.rb +39 -59
- data/lib/andrii_codebreaker/statistic.rb +1 -1
- data/lib/andrii_codebreaker/user.rb +3 -9
- data/lib/andrii_codebreaker/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32857bb09a13937194578bcc74d883ab93b151300e6948a0b3b0b1a2121c50f7
|
4
|
+
data.tar.gz: 6b01d5ac0c18f89b6006bb48aa0ff21ad98a5ced20c0209d728f95f986dae566
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eed8485cfdbe585ab513a4b87f00baa661546e62351f53fa565861a28aad46ddcacc4bbdf94066b256c956f98bf5927e6033536c09cdd03e6445146d32c61eac
|
7
|
+
data.tar.gz: 20d8163febd298a40a69034893b372a27af6ca6b36de323d17359c67df0855685ff4566514527b808598c4d904f338e2e7f7e62917bb326be050e4e5066d96e1
|
data/.rubocop.yml
CHANGED
@@ -10,12 +10,12 @@ Style/Documentation:
|
|
10
10
|
Metrics/LineLength:
|
11
11
|
Max: 120
|
12
12
|
|
13
|
-
RSpec/AnyInstance:
|
14
|
-
Enabled: false
|
15
|
-
|
16
13
|
Metrics/BlockLength:
|
17
14
|
Exclude:
|
18
15
|
- 'Rakefile'
|
19
16
|
- '**/*.rake'
|
20
17
|
- 'spec/**/*.rb'
|
21
18
|
- '*.gemspec'
|
19
|
+
|
20
|
+
RSpec/NamedSubject:
|
21
|
+
Enabled: false
|
data/Gemfile.lock
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
andrii_codebreaker (0.1.
|
4
|
+
andrii_codebreaker (0.1.11)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ast (2.4.2)
|
10
10
|
colorize (0.8.1)
|
11
|
-
concurrent-ruby (1.1.9)
|
12
11
|
diff-lcs (1.4.4)
|
13
12
|
docile (1.4.0)
|
14
13
|
fasterer (0.9.0)
|
15
14
|
colorize (~> 0.7)
|
16
15
|
ruby_parser (>= 3.14.1)
|
17
|
-
|
18
|
-
concurrent-ruby (~> 1.0)
|
16
|
+
ffaker (2.20.0)
|
19
17
|
parallel (1.21.0)
|
20
18
|
parser (3.0.2.0)
|
21
19
|
ast (~> 2.4.1)
|
@@ -67,7 +65,7 @@ PLATFORMS
|
|
67
65
|
DEPENDENCIES
|
68
66
|
andrii_codebreaker!
|
69
67
|
fasterer
|
70
|
-
|
68
|
+
ffaker
|
71
69
|
rake
|
72
70
|
rspec
|
73
71
|
rubocop
|
data/andrii_codebreaker.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.metadata['source_code_uri'] = spec.homepage
|
21
21
|
|
22
22
|
spec.add_development_dependency 'fasterer'
|
23
|
-
spec.add_development_dependency '
|
23
|
+
spec.add_development_dependency 'ffaker'
|
24
24
|
spec.add_development_dependency 'rake'
|
25
25
|
spec.add_development_dependency 'rspec'
|
26
26
|
spec.add_development_dependency 'rubocop'
|
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
module AndriiCodebreaker
|
4
4
|
module Constant
|
5
|
+
LALA = 2
|
5
6
|
NAME_MIN_LENGTH = 3
|
6
7
|
NAME_MAX_LENGTH = 20
|
7
8
|
CODE_START_LENGTH = 1
|
8
9
|
CODE_LENGTH = 6
|
9
10
|
CODE_LENGTH_COUNT = 4
|
11
|
+
RANGE_SECRET_CODE = (CODE_START_LENGTH..CODE_LENGTH).freeze
|
10
12
|
DIFFICULTY = {
|
11
13
|
easy: { attempts: 15, hints: 2 },
|
12
14
|
medium: { attempts: 10, hints: 1 },
|
@@ -4,22 +4,19 @@ module AndriiCodebreaker
|
|
4
4
|
class Difficulty
|
5
5
|
include Constant
|
6
6
|
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :name, :hints, :attempts
|
8
8
|
|
9
|
-
def initialize(
|
10
|
-
|
9
|
+
def initialize(name)
|
10
|
+
validate_difficulty(name)
|
11
|
+
@name = name
|
12
|
+
@hints = DIFFICULTY[name.to_sym][:hints]
|
13
|
+
@attempts = DIFFICULTY[name.to_sym][:attempts]
|
11
14
|
end
|
12
15
|
|
13
|
-
def
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def attempts
|
18
|
-
difficult[:attempts]
|
19
|
-
end
|
16
|
+
def validate_difficulty(name)
|
17
|
+
return DIFFICULTY[name.to_sym] if DIFFICULTY.keys.include? name.to_sym
|
20
18
|
|
21
|
-
|
22
|
-
DIFFICULTY[@difficulty.to_sym]
|
19
|
+
raise ArgumentError
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -5,93 +5,73 @@ module AndriiCodebreaker
|
|
5
5
|
include Statistic
|
6
6
|
include Constant
|
7
7
|
|
8
|
-
attr_reader :player_name, :difficulties, :
|
9
|
-
attr_accessor :
|
10
|
-
|
11
|
-
CONST_COMMAND = {
|
12
|
-
start: 'start',
|
13
|
-
rules: 'rules',
|
14
|
-
stats: 'stats',
|
15
|
-
exit: 'exit'
|
16
|
-
}.freeze
|
8
|
+
attr_reader :player_name, :difficulties, :secret_code
|
9
|
+
attr_accessor :used_hints, :used_attempts, :available_hints
|
17
10
|
|
18
11
|
def initialize(player_name, difficulty)
|
19
|
-
@player_name = player_name
|
12
|
+
@player_name = User.new(player_name)
|
20
13
|
@difficulties = Difficulty.new(difficulty)
|
21
|
-
@
|
22
|
-
@
|
14
|
+
@used_attempts = 0
|
15
|
+
@used_hints = 0
|
23
16
|
end
|
24
17
|
|
25
18
|
def start
|
26
19
|
@secret_code = generate_code
|
27
|
-
@
|
28
|
-
@hints_total = @difficulties.hint
|
29
|
-
@attempts_total = @difficulties.attempts
|
20
|
+
@available_hints = @secret_code.join.dup
|
30
21
|
end
|
31
22
|
|
32
|
-
def
|
33
|
-
return unless
|
23
|
+
def hint
|
24
|
+
return unless left_hint?
|
34
25
|
|
35
|
-
|
36
|
-
@
|
26
|
+
hint = @available_hints.chars.sample
|
27
|
+
@available_hints.sub!(hint, '')
|
28
|
+
@used_hints += 1
|
29
|
+
hint
|
37
30
|
end
|
38
31
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
32
|
+
def left_hint?
|
33
|
+
@used_hints < @difficulties.hints
|
34
|
+
end
|
42
35
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
secret_code = string_to_integer(@secret_code)
|
36
|
+
def left_attempts?
|
37
|
+
@used_attempts < @difficulties.attempts
|
38
|
+
end
|
47
39
|
|
48
|
-
|
40
|
+
def win?(guees)
|
41
|
+
return true if @secret_code.join == guees
|
49
42
|
|
50
|
-
|
51
|
-
|
43
|
+
false
|
44
|
+
end
|
45
|
+
|
46
|
+
def lose
|
47
|
+
return true if left_attempts?
|
52
48
|
|
53
|
-
|
49
|
+
false
|
54
50
|
end
|
55
51
|
|
56
|
-
|
52
|
+
def compare_codes(guess)
|
53
|
+
@used_attempts += 1
|
57
54
|
|
58
|
-
|
59
|
-
|
55
|
+
guess = guess.chars.map(&:to_i)
|
56
|
+
minuses = (@secret_code & guess).map { |element| [@secret_code.count(element), guess.count(element)].min }.sum
|
57
|
+
result = MINUS * minuses
|
60
58
|
|
61
|
-
|
62
|
-
if code_guess[i] == secret_code[i]
|
63
|
-
result += '+'
|
64
|
-
index << i
|
65
|
-
end
|
66
|
-
end
|
67
|
-
[result, index]
|
59
|
+
check_result(result, guess)
|
68
60
|
end
|
69
61
|
|
70
|
-
def
|
71
|
-
|
72
|
-
|
73
|
-
next unless secret_code.include?(i)
|
74
|
-
|
75
|
-
result << MINUS
|
76
|
-
index = secret_code.index(i)
|
77
|
-
secret_code.delete_at(index)
|
62
|
+
def check_result(result, guess)
|
63
|
+
guess.each.with_index do |code, index|
|
64
|
+
result.sub!(MINUS, PLUS) if code == @secret_code[index]
|
78
65
|
end
|
79
66
|
result
|
80
67
|
end
|
81
68
|
|
82
|
-
def
|
83
|
-
|
84
|
-
array_chars = input_code.chars
|
85
|
-
array_chars.each { |i| array_int << i.to_i }
|
86
|
-
array_int
|
87
|
-
end
|
88
|
-
|
89
|
-
def delete_index(array, index)
|
90
|
-
array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
|
69
|
+
def generate_code
|
70
|
+
Array.new(CODE_LENGTH_COUNT) { rand(RANGE_SECRET_CODE) }
|
91
71
|
end
|
92
72
|
|
93
|
-
def
|
94
|
-
(
|
73
|
+
def code_valid?(code)
|
74
|
+
code.to_s.match(/^[1-6]{4}$/)
|
95
75
|
end
|
96
76
|
end
|
97
77
|
end
|
@@ -6,7 +6,7 @@ module AndriiCodebreaker
|
|
6
6
|
|
7
7
|
def statistics
|
8
8
|
data = DbYaml.load_file_stats('result.yml')
|
9
|
-
data.sort_by { |item| [-DIFFICULTY_SORT[item.
|
9
|
+
data.sort_by { |item| [-DIFFICULTY_SORT[item.difficulties.name.to_sym], item.used_attempts, item.used_hints] }
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -7,17 +7,11 @@ module AndriiCodebreaker
|
|
7
7
|
attr_reader :name
|
8
8
|
|
9
9
|
def initialize(name)
|
10
|
-
@name =
|
10
|
+
@name = name
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
def validates_name(name)
|
16
|
-
name if valid_name?(name)
|
17
|
-
end
|
18
|
-
|
19
|
-
def valid_name?(name)
|
20
|
-
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
13
|
+
def self.validate_name(name)
|
14
|
+
name if name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
21
15
|
end
|
22
16
|
end
|
23
17
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: andrii_codebreaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrii
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: ffaker
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|