andrii_codebreaker 0.1.3 → 0.1.7
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/Gemfile.lock +1 -7
- data/lib/andrii_codebreaker/constants.rb +3 -0
- data/lib/andrii_codebreaker/difficulty.rb +25 -0
- data/lib/andrii_codebreaker/game.rb +54 -42
- data/lib/andrii_codebreaker/user.rb +7 -10
- data/lib/andrii_codebreaker/version.rb +1 -1
- data/lib/andrii_codebreaker.rb +2 -3
- metadata +3 -5
- data/config/i18n.rb +0 -3
- data/config/locales/en.yml +0 -5
- data/lib/andrii_codebreaker/validate/validate.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 614726c17f68e0208588be0daa78170ff2dca52f3cbb48017faa565b96673a0c
|
4
|
+
data.tar.gz: fd3c636ff49aaace2fda7055aa2ce90155709e0c56edb889cad671d6e4f43105
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c5603c20de947e67d699355cec83a2f4160da98d133c8516b10bd45cff5ccfc76600092f9ef74ab040d05631ae8032ec08147908dcee0775e4a2f202e618cc3
|
7
|
+
data.tar.gz: b6ae17bb6609a3ea2b6dbe9dd78e024d425c31b1428dce613660ab004389bd93d17dd2235b222fe591db4fa6fb2aea3c2b08717f39009a4ecb9ba13f61d96ebc
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
andrii_codebreaker (0.1.
|
4
|
+
andrii_codebreaker (0.1.7)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ast (2.4.2)
|
10
|
-
coderay (1.1.3)
|
11
10
|
colorize (0.8.1)
|
12
11
|
concurrent-ruby (1.1.9)
|
13
12
|
diff-lcs (1.4.4)
|
@@ -17,13 +16,9 @@ GEM
|
|
17
16
|
ruby_parser (>= 3.14.1)
|
18
17
|
i18n (1.8.11)
|
19
18
|
concurrent-ruby (~> 1.0)
|
20
|
-
method_source (1.0.0)
|
21
19
|
parallel (1.21.0)
|
22
20
|
parser (3.0.2.0)
|
23
21
|
ast (~> 2.4.1)
|
24
|
-
pry (0.14.1)
|
25
|
-
coderay (~> 1.1)
|
26
|
-
method_source (~> 1.0)
|
27
22
|
rainbow (3.0.0)
|
28
23
|
rake (13.0.6)
|
29
24
|
regexp_parser (2.1.1)
|
@@ -73,7 +68,6 @@ DEPENDENCIES
|
|
73
68
|
andrii_codebreaker!
|
74
69
|
fasterer
|
75
70
|
i18n
|
76
|
-
pry
|
77
71
|
rake
|
78
72
|
rspec
|
79
73
|
rubocop
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AndriiCodebreaker
|
4
|
+
class Difficulty
|
5
|
+
include Constant
|
6
|
+
|
7
|
+
attr_reader :difficulty
|
8
|
+
|
9
|
+
def initialize(difficulty)
|
10
|
+
@difficulty = difficulty
|
11
|
+
end
|
12
|
+
|
13
|
+
def hint
|
14
|
+
difficult[:hints]
|
15
|
+
end
|
16
|
+
|
17
|
+
def attempts
|
18
|
+
difficult[:attempts]
|
19
|
+
end
|
20
|
+
|
21
|
+
def difficult
|
22
|
+
DIFFICULTY[@difficulty.to_sym]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,85 +1,97 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '../andrii_codebreaker'
|
4
|
-
require_relative '../../config/i18n'
|
5
|
-
|
6
3
|
module AndriiCodebreaker
|
7
4
|
class Game
|
8
|
-
include Validate
|
9
5
|
include Statistic
|
10
6
|
include Constant
|
11
7
|
|
12
|
-
attr_reader :player_name, :
|
13
|
-
attr_accessor :hints_left, :attempts_left
|
8
|
+
attr_reader :player_name, :difficulties, :attempts_total, :hints_total, :secret_code
|
9
|
+
attr_accessor :hints_left, :attempts_left, :secret_code_copy
|
14
10
|
|
15
11
|
CONST_COMMAND = {
|
16
|
-
start:
|
17
|
-
rules:
|
18
|
-
stats:
|
19
|
-
exit:
|
12
|
+
start: 'start',
|
13
|
+
rules: 'rules',
|
14
|
+
stats: 'stats',
|
15
|
+
exit: 'exit'
|
20
16
|
}.freeze
|
21
17
|
|
22
18
|
def initialize(player_name, difficulty)
|
23
19
|
@player_name = player_name
|
24
|
-
@
|
20
|
+
@difficulties = Difficulty.new(difficulty)
|
25
21
|
@attempts_left = 0
|
26
22
|
@hints_left = 0
|
27
|
-
initialize_class
|
28
23
|
end
|
29
24
|
|
30
|
-
def
|
31
|
-
difficult = DIFFICULTY[@difficulty.to_sym]
|
32
|
-
@hints_total = difficult[:hints]
|
33
|
-
@attempts_total = difficult[:attempts]
|
25
|
+
def start
|
34
26
|
@secret_code = generate_code
|
27
|
+
@secret_code_copy = @secret_code.chars
|
28
|
+
@hints_total = @difficulties.hint
|
29
|
+
@attempts_total = @difficulties.attempts
|
35
30
|
end
|
36
31
|
|
37
|
-
def
|
38
|
-
hints_left
|
32
|
+
def hints
|
33
|
+
return unless @hints_total > @hints_left
|
34
|
+
|
35
|
+
@hints_left += 1
|
36
|
+
@secret_code_copy.pop
|
39
37
|
end
|
40
38
|
|
41
39
|
def compare_codes(guess)
|
42
|
-
return '
|
40
|
+
return '' if !guess.match(/^[1-6]{4}$/) && !guess.match(/^\?$/)
|
41
|
+
return WIN if guess == @secret_code
|
42
|
+
|
43
|
+
result = ''
|
44
|
+
index = []
|
45
|
+
code_guess = string_to_integer(guess)
|
46
|
+
secret_code = string_to_integer(@secret_code)
|
47
|
+
|
48
|
+
result, index = check_plus(code_guess, result, secret_code, index)
|
43
49
|
|
44
|
-
(
|
50
|
+
delete_index(code_guess, index)
|
51
|
+
delete_index(secret_code, index)
|
52
|
+
|
53
|
+
check_minus(code_guess, result, secret_code, index)
|
45
54
|
end
|
46
55
|
|
47
56
|
private
|
48
57
|
|
49
|
-
def
|
50
|
-
|
51
|
-
end
|
58
|
+
def check_plus(*info)
|
59
|
+
code_guess, result, secret_code, index = info
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
61
|
+
code_guess.each_index do |i|
|
62
|
+
if code_guess[i] == secret_code[i]
|
63
|
+
result += '+'
|
64
|
+
index << i
|
65
|
+
end
|
66
|
+
end
|
67
|
+
[result, index]
|
57
68
|
end
|
58
69
|
|
59
|
-
def
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
arr[1].each do |number|
|
64
|
-
next unless arr[0].include?(number)
|
70
|
+
def check_minus(*info)
|
71
|
+
code_guess, result, secret_code, index = info
|
72
|
+
code_guess.each do |i|
|
73
|
+
next unless secret_code.include?(i)
|
65
74
|
|
66
|
-
|
75
|
+
result << MINUS
|
76
|
+
index = secret_code.index(i)
|
77
|
+
secret_code.delete_at(index)
|
67
78
|
end
|
68
|
-
|
79
|
+
result
|
69
80
|
end
|
70
81
|
|
71
|
-
def
|
72
|
-
|
82
|
+
def string_to_integer(input_code)
|
83
|
+
array_int = []
|
84
|
+
array_chars = input_code.chars
|
85
|
+
array_chars.each { |i| array_int << i.to_i }
|
86
|
+
array_int
|
73
87
|
end
|
74
88
|
|
75
|
-
def
|
76
|
-
|
77
|
-
.delete_if { |el| el[0] == el[1] }
|
78
|
-
.transpose
|
89
|
+
def delete_index(array, index)
|
90
|
+
array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
|
79
91
|
end
|
80
92
|
|
81
93
|
def generate_code
|
82
|
-
(
|
94
|
+
(CODE_START_LENGTH..CODE_LENGTH_COUNT).map { rand(CODE_START_LENGTH..CODE_LENGTH) }.join
|
83
95
|
end
|
84
96
|
end
|
85
97
|
end
|
@@ -2,25 +2,22 @@
|
|
2
2
|
|
3
3
|
module AndriiCodebreaker
|
4
4
|
class User
|
5
|
-
include
|
5
|
+
include Constant
|
6
6
|
|
7
7
|
attr_reader :name
|
8
8
|
|
9
9
|
def initialize(name)
|
10
|
-
@name = name
|
11
|
-
validate_name
|
10
|
+
@name = validates_name(name)
|
12
11
|
end
|
13
12
|
|
14
|
-
|
15
|
-
return unless user.hints_total > user.hints_left
|
13
|
+
private
|
16
14
|
|
17
|
-
|
15
|
+
def validates_name(name)
|
16
|
+
name if valid_name?(name)
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
def validate_name
|
23
|
-
@name = validates_name(name)
|
19
|
+
def valid_name?(name)
|
20
|
+
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
24
21
|
end
|
25
22
|
end
|
26
23
|
end
|
data/lib/andrii_codebreaker.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'yaml'
|
4
|
-
|
5
|
-
require_relative 'andrii_codebreaker/validate/validate'
|
4
|
+
require_relative 'andrii_codebreaker/constants'
|
6
5
|
require_relative 'andrii_codebreaker/model'
|
7
6
|
require_relative 'andrii_codebreaker/user'
|
7
|
+
require_relative 'andrii_codebreaker/difficulty'
|
8
8
|
require_relative 'andrii_codebreaker/statistic'
|
9
9
|
require_relative 'andrii_codebreaker/version'
|
10
10
|
require_relative 'andrii_codebreaker/game'
|
11
|
-
require_relative 'andrii_codebreaker/constants'
|
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.7
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -130,15 +130,13 @@ files:
|
|
130
130
|
- andrii_codebreaker.gemspec
|
131
131
|
- bin/console
|
132
132
|
- bin/setup
|
133
|
-
- config/i18n.rb
|
134
|
-
- config/locales/en.yml
|
135
133
|
- lib/andrii_codebreaker.rb
|
136
134
|
- lib/andrii_codebreaker/constants.rb
|
135
|
+
- lib/andrii_codebreaker/difficulty.rb
|
137
136
|
- lib/andrii_codebreaker/game.rb
|
138
137
|
- lib/andrii_codebreaker/model.rb
|
139
138
|
- lib/andrii_codebreaker/statistic.rb
|
140
139
|
- lib/andrii_codebreaker/user.rb
|
141
|
-
- lib/andrii_codebreaker/validate/validate.rb
|
142
140
|
- lib/andrii_codebreaker/version.rb
|
143
141
|
homepage: https://github.com/Andrii-RubyGarage/Codebreker-Gem
|
144
142
|
licenses:
|
data/config/i18n.rb
DELETED
data/config/locales/en.yml
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../constants'
|
4
|
-
|
5
|
-
module AndriiCodebreaker
|
6
|
-
module Validate
|
7
|
-
include Constant
|
8
|
-
|
9
|
-
def validates_name(name)
|
10
|
-
name if valid_name?(name)
|
11
|
-
end
|
12
|
-
|
13
|
-
def validate_guess(code)
|
14
|
-
code = string_to_integer(code)
|
15
|
-
true if check_code_length?(code) && check_numbers?(code)
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def string_to_integer(input_code)
|
21
|
-
array_int = []
|
22
|
-
array_chars = input_code.chars
|
23
|
-
array_chars.each { |i| array_int << i.to_i }
|
24
|
-
array_int
|
25
|
-
end
|
26
|
-
|
27
|
-
def valid_name?(name)
|
28
|
-
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
29
|
-
end
|
30
|
-
|
31
|
-
def check_code_length?(code)
|
32
|
-
code.length == CODE_LENGTH_COUNT
|
33
|
-
end
|
34
|
-
|
35
|
-
def check_numbers?(code)
|
36
|
-
code.all? { |value| value.between?(CODE_START_LENGTH, CODE_LENGTH) }
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|