andrii_codebreaker 0.1.2 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -7
- data/.rubocop.yml +0 -3
- data/Gemfile +2 -0
- data/Gemfile.lock +5 -1
- data/Rakefile +2 -0
- data/andrii_codebreaker-0.1.5.gem +0 -0
- data/andrii_codebreaker.gemspec +3 -0
- data/bin/console +0 -10
- data/lib/andrii_codebreaker/constants.rb +17 -0
- data/lib/andrii_codebreaker/game.rb +45 -49
- data/lib/andrii_codebreaker/model.rb +14 -12
- data/lib/andrii_codebreaker/statistic.rb +6 -3
- data/lib/andrii_codebreaker/user.rb +15 -16
- data/lib/andrii_codebreaker/validate/validate.rb +8 -4
- data/lib/andrii_codebreaker/version.rb +3 -1
- data/lib/andrii_codebreaker.rb +4 -2
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 850117beac1f8b9441630a8a0f97ef5a6c374e671dce56bedc85d9f43b74d828
|
4
|
+
data.tar.gz: c52fce0dcceb07cb99bb0ce693b7e38469b380e4310c990e9783cf53acb4fbb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 614863a6b0e5d83c2e3752d5244146fc8aaebf1452dcdb367172da66bb8c6c9c6a838fc347e4af237d57792b76676b30dc55224acf1d7cc963cc0a746aa5e00a
|
7
|
+
data.tar.gz: 32267d352720c6009cd3b4bff6f3c130e1dae175f6b90b423c1b4ff8ac42459f9b252b0934d55c2fc0e4a22118d2c89f72637083c8ee4a9def75b16ae6331dc0
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,18 +1,21 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
andrii_codebreaker (0.1.
|
4
|
+
andrii_codebreaker (0.1.6)
|
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)
|
11
12
|
diff-lcs (1.4.4)
|
12
13
|
docile (1.4.0)
|
13
14
|
fasterer (0.9.0)
|
14
15
|
colorize (~> 0.7)
|
15
16
|
ruby_parser (>= 3.14.1)
|
17
|
+
i18n (1.8.11)
|
18
|
+
concurrent-ruby (~> 1.0)
|
16
19
|
parallel (1.21.0)
|
17
20
|
parser (3.0.2.0)
|
18
21
|
ast (~> 2.4.1)
|
@@ -64,6 +67,7 @@ PLATFORMS
|
|
64
67
|
DEPENDENCIES
|
65
68
|
andrii_codebreaker!
|
66
69
|
fasterer
|
70
|
+
i18n
|
67
71
|
rake
|
68
72
|
rspec
|
69
73
|
rubocop
|
data/Rakefile
CHANGED
Binary file
|
data/andrii_codebreaker.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require './lib/andrii_codebreaker/version'
|
2
4
|
lib = File.expand_path('lib', __dir__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
@@ -18,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
18
20
|
spec.metadata['source_code_uri'] = spec.homepage
|
19
21
|
|
20
22
|
spec.add_development_dependency 'fasterer'
|
23
|
+
spec.add_development_dependency 'i18n'
|
21
24
|
spec.add_development_dependency 'rake'
|
22
25
|
spec.add_development_dependency 'rspec'
|
23
26
|
spec.add_development_dependency 'rubocop'
|
data/bin/console
CHANGED
@@ -1,15 +1,5 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
1
|
require 'bundler/setup'
|
5
2
|
require 'andrii_codebreaker'
|
6
3
|
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
9
|
-
|
10
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
-
# require "pry"
|
12
|
-
# Pry.start
|
13
|
-
|
14
4
|
require 'irb'
|
15
5
|
IRB.start(__FILE__)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AndriiCodebreaker
|
4
|
+
module Constant
|
5
|
+
NAME_MIN_LENGTH = 3
|
6
|
+
NAME_MAX_LENGTH = 20
|
7
|
+
CODE_START_LENGTH = 1
|
8
|
+
CODE_LENGTH = 6
|
9
|
+
CODE_LENGTH_COUNT = 4
|
10
|
+
DIFFICULTY = {
|
11
|
+
easy: { attempts: 15, hints: 2 },
|
12
|
+
medium: { attempts: 10, hints: 1 },
|
13
|
+
hell: { attempts: 5, hints: 1 }
|
14
|
+
}.freeze
|
15
|
+
DIFFICULTY_SORT = { easy: 1, medium: 2, hell: 3 }.freeze
|
16
|
+
end
|
17
|
+
end
|
@@ -1,10 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AndriiCodebreaker
|
2
4
|
class Game
|
3
5
|
include Validate
|
4
|
-
include Model
|
5
6
|
include Statistic
|
6
|
-
|
7
|
-
|
7
|
+
include Constant
|
8
|
+
|
9
|
+
attr_reader :player_name, :difficulty, :attempts_total, :hints_total, :secret_code
|
10
|
+
attr_accessor :hints_left, :attempts_left, :secret_code_copy
|
8
11
|
|
9
12
|
CONST_COMMAND = {
|
10
13
|
start: 'start',
|
@@ -13,75 +16,68 @@ module AndriiCodebreaker
|
|
13
16
|
exit: 'exit'
|
14
17
|
}.freeze
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@
|
20
|
-
@
|
19
|
+
def initialize(player_name, difficulty)
|
20
|
+
@player_name = player_name
|
21
|
+
@difficulty = difficulty
|
22
|
+
@attempts_left = 0
|
23
|
+
@hints_left = 0
|
24
|
+
initialize_class
|
21
25
|
end
|
22
26
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@
|
27
|
+
def initialize_class
|
28
|
+
difficult = DIFFICULTY[@difficulty.to_sym]
|
29
|
+
@hints_total = difficult[:hints]
|
30
|
+
@attempts_total = difficult[:attempts]
|
31
|
+
@secret_code = generate_code
|
32
|
+
@secret_code_copy = @secret_code.dup.chars
|
28
33
|
end
|
29
34
|
|
30
|
-
def
|
31
|
-
|
32
|
-
number_secret_code(user_code, matches, code_copy)
|
35
|
+
def show_hints
|
36
|
+
hints_left
|
33
37
|
end
|
34
38
|
|
35
|
-
def
|
36
|
-
|
37
|
-
array_chars = input_code.chars
|
38
|
-
array_chars.each { |i| array_int << i.to_i }
|
39
|
-
array_int
|
40
|
-
end
|
39
|
+
def compare_codes(guess)
|
40
|
+
return '++++' if exact_match(guess)
|
41
41
|
|
42
|
-
|
43
|
-
save(user, file)
|
42
|
+
('+' * pluses(guess)) + ('-' * minuses(guess))
|
44
43
|
end
|
45
44
|
|
46
45
|
private
|
47
46
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
user_code.each_index do |i|
|
52
|
-
next unless @secret_code[i] == user_code[i]
|
47
|
+
def exact_match(guess)
|
48
|
+
@secret_code == guess
|
49
|
+
end
|
53
50
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
[matches, user_code, code_copy]
|
51
|
+
def pluses(guess)
|
52
|
+
zipped(guess)
|
53
|
+
.select { |el| el[0] == el[1] }
|
54
|
+
.count
|
59
55
|
end
|
60
56
|
|
61
|
-
def
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
next unless
|
67
|
-
amount_of_element?(amount_numbers_now, amount_numbers_secret, element)
|
57
|
+
def minuses(guess)
|
58
|
+
return 0 if exact_match(guess)
|
59
|
+
|
60
|
+
arr = delete_pairs(guess)
|
61
|
+
arr[1].each do |number|
|
62
|
+
next unless arr[0].include?(number)
|
68
63
|
|
69
|
-
|
70
|
-
amount_numbers_now[element.to_s] += 1
|
64
|
+
arr[0].delete_at(arr[0].index(number))
|
71
65
|
end
|
72
|
-
|
66
|
+
arr[1].size - arr[0].size
|
73
67
|
end
|
74
68
|
|
75
|
-
def
|
76
|
-
|
69
|
+
def zipped(guess)
|
70
|
+
@secret_code.chars.zip(guess.chars)
|
77
71
|
end
|
78
72
|
|
79
|
-
def
|
80
|
-
|
73
|
+
def delete_pairs(guess)
|
74
|
+
zipped(guess)
|
75
|
+
.delete_if { |el| el[0] == el[1] }
|
76
|
+
.transpose
|
81
77
|
end
|
82
78
|
|
83
79
|
def generate_code
|
84
|
-
Array.new(
|
80
|
+
(Array.new(CODE_LENGTH_COUNT) { rand(CODE_START_LENGTH..CODE_LENGTH) }).join.to_s
|
85
81
|
end
|
86
82
|
end
|
87
83
|
end
|
@@ -1,18 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AndriiCodebreaker
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
class DbYaml
|
5
|
+
class << self
|
6
|
+
def load_file(path)
|
7
|
+
File.open(path, 'r') do |filename|
|
8
|
+
YAML.safe_load(filename)
|
9
|
+
end
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
+
def save(user, path)
|
13
|
+
File.open(path, 'a') { |f| YAML.dump(user, f) }
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
YAML.safe_load(filename)
|
16
|
+
def load_file_stats(path)
|
17
|
+
YAML.load_stream(File.open(path))
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -1,9 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AndriiCodebreaker
|
2
4
|
module Statistic
|
5
|
+
include Constant
|
6
|
+
|
3
7
|
def statistics
|
4
|
-
data =
|
5
|
-
|
6
|
-
data.sort_by { |item| [-hash[item.difficulty.to_sym], item.attempts_now, item.hints_now] }
|
8
|
+
data = DbYaml.load_file_stats('result.yml')
|
9
|
+
data.sort_by { |item| [-DIFFICULTY_SORT[item.difficulty.to_sym], item.attempts_left, item.hints_left] }
|
7
10
|
end
|
8
11
|
end
|
9
12
|
end
|
@@ -1,28 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AndriiCodebreaker
|
2
4
|
class User
|
3
|
-
|
4
|
-
easy: { attempts: 15, hints: 2 },
|
5
|
-
medium: { attempts: 10, hints: 1 },
|
6
|
-
hell: { attempts: 5, hints: 1 }
|
7
|
-
}.freeze
|
5
|
+
include AndriiCodebreaker::Validate
|
8
6
|
|
9
|
-
attr_reader :name
|
10
|
-
attr_accessor :hints_now, :attempts_now
|
7
|
+
attr_reader :name
|
11
8
|
|
12
|
-
def initialize(name
|
9
|
+
def initialize(name)
|
13
10
|
@name = name
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
validate_name
|
12
|
+
end
|
13
|
+
|
14
|
+
def hints(game)
|
15
|
+
return unless game.hints_total > game.hints_left
|
16
|
+
|
17
|
+
game.hints_left += 1
|
18
|
+
game.secret_code_copy.pop
|
18
19
|
end
|
19
20
|
|
20
21
|
private
|
21
22
|
|
22
|
-
def
|
23
|
-
|
24
|
-
@hints_total = difficult[:hints]
|
25
|
-
@attempts_total = difficult[:attempts]
|
23
|
+
def validate_name
|
24
|
+
@name = validates_name(name)
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
@@ -1,12 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AndriiCodebreaker
|
2
4
|
module Validate
|
5
|
+
include Constant
|
6
|
+
|
3
7
|
def validates_name(name)
|
4
8
|
name if valid_name?(name)
|
5
9
|
end
|
6
10
|
|
7
11
|
def validate_guess(code)
|
8
12
|
code = string_to_integer(code)
|
9
|
-
|
13
|
+
true if check_code_length?(code) && check_numbers?(code)
|
10
14
|
end
|
11
15
|
|
12
16
|
private
|
@@ -19,15 +23,15 @@ module AndriiCodebreaker
|
|
19
23
|
end
|
20
24
|
|
21
25
|
def valid_name?(name)
|
22
|
-
name.length >=
|
26
|
+
name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
|
23
27
|
end
|
24
28
|
|
25
29
|
def check_code_length?(code)
|
26
|
-
code.length ==
|
30
|
+
code.length == CODE_LENGTH_COUNT
|
27
31
|
end
|
28
32
|
|
29
33
|
def check_numbers?(code)
|
30
|
-
code.all? { |value| value.between?(
|
34
|
+
code.all? { |value| value.between?(CODE_START_LENGTH, CODE_LENGTH) }
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
data/lib/andrii_codebreaker.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'yaml'
|
4
|
+
require_relative 'andrii_codebreaker/constants'
|
5
|
+
require_relative 'andrii_codebreaker/validate/validate'
|
3
6
|
require_relative 'andrii_codebreaker/model'
|
4
7
|
require_relative 'andrii_codebreaker/user'
|
5
|
-
require_relative 'andrii_codebreaker/validate/validate'
|
6
8
|
require_relative 'andrii_codebreaker/statistic'
|
7
9
|
require_relative 'andrii_codebreaker/version'
|
8
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.6
|
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-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,10 +127,12 @@ files:
|
|
113
127
|
- LICENSE.txt
|
114
128
|
- README.md
|
115
129
|
- Rakefile
|
130
|
+
- andrii_codebreaker-0.1.5.gem
|
116
131
|
- andrii_codebreaker.gemspec
|
117
132
|
- bin/console
|
118
133
|
- bin/setup
|
119
134
|
- lib/andrii_codebreaker.rb
|
135
|
+
- lib/andrii_codebreaker/constants.rb
|
120
136
|
- lib/andrii_codebreaker/game.rb
|
121
137
|
- lib/andrii_codebreaker/model.rb
|
122
138
|
- lib/andrii_codebreaker/statistic.rb
|