andrii_codebreaker 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/andrii_codebreaker/constants.rb +3 -0
- data/lib/andrii_codebreaker/difficulty.rb +25 -0
- data/lib/andrii_codebreaker/game.rb +49 -35
- data/lib/andrii_codebreaker/user.rb +7 -11
- data/lib/andrii_codebreaker/version.rb +1 -1
- data/lib/andrii_codebreaker.rb +1 -1
- metadata +3 -4
- data/andrii_codebreaker-0.1.5.gem +0 -0
- data/lib/andrii_codebreaker/validate/validate.rb +0 -37
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
@@ -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
|
@@ -2,11 +2,10 @@
|
|
2
2
|
|
3
3
|
module AndriiCodebreaker
|
4
4
|
class Game
|
5
|
-
include Validate
|
6
5
|
include Statistic
|
7
6
|
include Constant
|
8
7
|
|
9
|
-
attr_reader :player_name, :
|
8
|
+
attr_reader :player_name, :difficulties, :attempts_total, :hints_total, :secret_code
|
10
9
|
attr_accessor :hints_left, :attempts_left, :secret_code_copy
|
11
10
|
|
12
11
|
CONST_COMMAND = {
|
@@ -18,66 +17,81 @@ module AndriiCodebreaker
|
|
18
17
|
|
19
18
|
def initialize(player_name, difficulty)
|
20
19
|
@player_name = player_name
|
21
|
-
@
|
20
|
+
@difficulties = Difficulty.new(difficulty)
|
22
21
|
@attempts_left = 0
|
23
22
|
@hints_left = 0
|
24
|
-
initialize_class
|
25
23
|
end
|
26
24
|
|
27
|
-
def
|
28
|
-
difficult = DIFFICULTY[@difficulty.to_sym]
|
29
|
-
@hints_total = difficult[:hints]
|
30
|
-
@attempts_total = difficult[:attempts]
|
25
|
+
def start
|
31
26
|
@secret_code = generate_code
|
32
|
-
@secret_code_copy = @secret_code.
|
27
|
+
@secret_code_copy = @secret_code.chars
|
28
|
+
@hints_total = @difficulties.hint
|
29
|
+
@attempts_total = @difficulties.attempts
|
33
30
|
end
|
34
31
|
|
35
|
-
def
|
36
|
-
hints_left
|
32
|
+
def hints
|
33
|
+
return unless @hints_total > @hints_left
|
34
|
+
|
35
|
+
@hints_left += 1
|
36
|
+
@secret_code_copy.pop
|
37
37
|
end
|
38
38
|
|
39
39
|
def compare_codes(guess)
|
40
|
-
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)
|
49
|
+
|
50
|
+
delete_index(code_guess, index)
|
51
|
+
delete_index(secret_code, index)
|
41
52
|
|
42
|
-
(
|
53
|
+
check_minus(code_guess, result, secret_code, index)
|
43
54
|
end
|
44
55
|
|
45
56
|
private
|
46
57
|
|
47
|
-
def
|
48
|
-
|
49
|
-
end
|
58
|
+
def check_plus(*info)
|
59
|
+
code_guess, result, secret_code, index = info
|
50
60
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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]
|
55
68
|
end
|
56
69
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
arr[1].each do |number|
|
62
|
-
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)
|
63
74
|
|
64
|
-
|
75
|
+
result << MINUS
|
76
|
+
index = secret_code.index(i)
|
77
|
+
secret_code.delete_at(index)
|
65
78
|
end
|
66
|
-
|
79
|
+
result
|
67
80
|
end
|
68
81
|
|
69
|
-
def
|
70
|
-
|
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
|
71
87
|
end
|
72
88
|
|
73
|
-
def
|
74
|
-
|
75
|
-
.delete_if { |el| el[0] == el[1] }
|
76
|
-
.transpose
|
89
|
+
def delete_index(array, index)
|
90
|
+
array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
|
77
91
|
end
|
78
92
|
|
79
93
|
def generate_code
|
80
|
-
(
|
94
|
+
(CODE_START_LENGTH..CODE_LENGTH_COUNT).map { rand(CODE_START_LENGTH..CODE_LENGTH) }.join
|
81
95
|
end
|
82
96
|
end
|
83
97
|
end
|
@@ -2,26 +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 game.hints_total > game.hints_left
|
13
|
+
private
|
16
14
|
|
17
|
-
|
18
|
-
|
15
|
+
def validates_name(name)
|
16
|
+
name if valid_name?(name)
|
19
17
|
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
def validate_name
|
24
|
-
@name = validates_name(name)
|
19
|
+
def valid_name?(name)
|
20
|
+
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
25
21
|
end
|
26
22
|
end
|
27
23
|
end
|
data/lib/andrii_codebreaker.rb
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
require 'yaml'
|
4
4
|
require_relative 'andrii_codebreaker/constants'
|
5
|
-
require_relative 'andrii_codebreaker/validate/validate'
|
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'
|
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
|
@@ -127,17 +127,16 @@ files:
|
|
127
127
|
- LICENSE.txt
|
128
128
|
- README.md
|
129
129
|
- Rakefile
|
130
|
-
- andrii_codebreaker-0.1.5.gem
|
131
130
|
- andrii_codebreaker.gemspec
|
132
131
|
- bin/console
|
133
132
|
- bin/setup
|
134
133
|
- lib/andrii_codebreaker.rb
|
135
134
|
- lib/andrii_codebreaker/constants.rb
|
135
|
+
- lib/andrii_codebreaker/difficulty.rb
|
136
136
|
- lib/andrii_codebreaker/game.rb
|
137
137
|
- lib/andrii_codebreaker/model.rb
|
138
138
|
- lib/andrii_codebreaker/statistic.rb
|
139
139
|
- lib/andrii_codebreaker/user.rb
|
140
|
-
- lib/andrii_codebreaker/validate/validate.rb
|
141
140
|
- lib/andrii_codebreaker/version.rb
|
142
141
|
homepage: https://github.com/Andrii-RubyGarage/Codebreker-Gem
|
143
142
|
licenses:
|
Binary file
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module AndriiCodebreaker
|
4
|
-
module Validate
|
5
|
-
include Constant
|
6
|
-
|
7
|
-
def validates_name(name)
|
8
|
-
name if valid_name?(name)
|
9
|
-
end
|
10
|
-
|
11
|
-
def validate_guess(code)
|
12
|
-
code = string_to_integer(code)
|
13
|
-
true if check_code_length?(code) && check_numbers?(code)
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def string_to_integer(input_code)
|
19
|
-
array_int = []
|
20
|
-
array_chars = input_code.chars
|
21
|
-
array_chars.each { |i| array_int << i.to_i }
|
22
|
-
array_int
|
23
|
-
end
|
24
|
-
|
25
|
-
def valid_name?(name)
|
26
|
-
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
27
|
-
end
|
28
|
-
|
29
|
-
def check_code_length?(code)
|
30
|
-
code.length == CODE_LENGTH_COUNT
|
31
|
-
end
|
32
|
-
|
33
|
-
def check_numbers?(code)
|
34
|
-
code.all? { |value| value.between?(CODE_START_LENGTH, CODE_LENGTH) }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|