codebreaker_kub 0.1.9 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -4
- data/Gemfile +2 -0
- data/Gemfile.lock +1 -1
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/codebreaker.gemspec +2 -0
- data/lib/codebreaker.rb +3 -0
- data/lib/codebreaker/game.rb +31 -34
- data/lib/codebreaker/guess_checker.rb +43 -0
- data/lib/codebreaker/loader.rb +3 -1
- data/lib/codebreaker/output.rb +2 -0
- data/lib/codebreaker/validation.rb +5 -1
- data/lib/codebreaker/version.rb +3 -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: d3999defb3a7f8d534184fbe210075d9a2e7db373c9f3f03ee274eac2386b61c
|
4
|
+
data.tar.gz: f35e7d26ae5e52db50ed64164d1226cb10c2aabcb1f153ae7e73d8c35f959c30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e07b1939bd46e0a0244dcc92f14ff9de0113b8becf263e5b337f613a7c4c6104d99282723baca44266d5da04764399e1375106561deca721e8531eb9d0f156f
|
7
|
+
data.tar.gz: b6c04918739abc7d95e8a8426df36eb858d9e8befac9dce84fff4fc84aba703d7dfb0c463aebd7688e0d5d0f4f66173ba5bc57e0ab1b45c90954507884e35949
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/codebreaker.gemspec
CHANGED
data/lib/codebreaker.rb
CHANGED
data/lib/codebreaker/game.rb
CHANGED
@@ -1,22 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Codebreaker
|
2
4
|
class Game
|
3
|
-
attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
|
4
|
-
|
5
|
+
attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
|
6
|
+
:all_attempts
|
7
|
+
attr_reader :hints_code
|
8
|
+
|
9
|
+
CODE_LENGTH = 4
|
10
|
+
CODE_RANGE = (1..6).freeze
|
11
|
+
|
5
12
|
def initialize
|
6
13
|
@difficulties = Codebreaker::Loader.load('difficulties')
|
7
14
|
@code = generate_code
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def symbols(minuse = '-', plus = '+', none = '')
|
12
|
-
@minuse = minuse
|
13
|
-
@plus = plus
|
14
|
-
@none = none
|
15
|
+
@hints_code = @code.chars.shuffle
|
15
16
|
end
|
16
17
|
|
17
18
|
def game_option(name, difficulty)
|
18
19
|
@name = name
|
19
20
|
@difficulty = difficulty
|
21
|
+
@all_attempts = difficulty_option[:attempts]
|
22
|
+
@all_hints = difficulty_option[:hints]
|
20
23
|
@attempts_left = difficulty_option[:attempts]
|
21
24
|
@hints_left = difficulty_option[:hints]
|
22
25
|
end
|
@@ -29,7 +32,7 @@ module Codebreaker
|
|
29
32
|
end
|
30
33
|
|
31
34
|
def hints_left?
|
32
|
-
|
35
|
+
hints_left.positive?
|
33
36
|
end
|
34
37
|
|
35
38
|
def input_operation(input_code)
|
@@ -37,57 +40,51 @@ module Codebreaker
|
|
37
40
|
return unless attempts_left?
|
38
41
|
|
39
42
|
@attempts_left -= 1
|
40
|
-
check_input
|
43
|
+
check_input(@code, @input_code)
|
41
44
|
end
|
42
45
|
|
43
46
|
def attempts_left?
|
44
|
-
|
47
|
+
attempts_left.positive?
|
45
48
|
end
|
46
49
|
|
47
50
|
def win?
|
48
|
-
|
51
|
+
input_code == code
|
49
52
|
end
|
50
53
|
|
51
|
-
def save
|
52
|
-
Codebreaker::Loader.save(to_h,
|
54
|
+
def save(filename = 'stats')
|
55
|
+
Codebreaker::Loader.save(to_h, filename)
|
53
56
|
end
|
54
57
|
|
55
58
|
private
|
56
59
|
|
57
|
-
def check_input(code = @code.chars)
|
58
|
-
input = @input_code.chars
|
59
|
-
minuses = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
|
60
|
-
result = @minuse * minuses
|
61
|
-
input.each.with_index do |element, index|
|
62
|
-
result.sub!(@minuse, @plus) if element == code[index]
|
63
|
-
end
|
64
|
-
return result unless result.empty?
|
65
|
-
|
66
|
-
@none
|
67
|
-
end
|
68
|
-
|
69
60
|
def generate_code
|
70
|
-
Array.new(
|
61
|
+
Array.new(CODE_LENGTH) { rand(CODE_RANGE) }.join
|
71
62
|
end
|
72
63
|
|
73
64
|
def generate_hint
|
74
|
-
@
|
65
|
+
@hints_code.pop
|
75
66
|
end
|
76
67
|
|
77
68
|
def difficulty_option
|
78
69
|
@difficulties[@difficulty]
|
79
70
|
end
|
80
71
|
|
72
|
+
def check_input(code, input)
|
73
|
+
guess_checker = Codebreaker::GuessChecker.new(code, input)
|
74
|
+
guess_checker.symbols
|
75
|
+
guess_checker.check_input
|
76
|
+
end
|
77
|
+
|
81
78
|
def to_h
|
82
79
|
{
|
83
80
|
name: @name,
|
84
81
|
difficulty: @difficulty,
|
85
|
-
attempts:
|
86
|
-
hints:
|
82
|
+
attempts: @all_attempts,
|
83
|
+
hints: @all_hints,
|
87
84
|
code: @code,
|
88
|
-
used_attempts:
|
89
|
-
used_hints:
|
90
|
-
|
85
|
+
used_attempts: @all_attempts - @attempts_left,
|
86
|
+
used_hints: @all_hints - @hints_left,
|
87
|
+
date: Time.now
|
91
88
|
}
|
92
89
|
end
|
93
90
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class GuessChecker
|
5
|
+
attr_accessor :inexact, :exact, :none
|
6
|
+
|
7
|
+
MINUSE = '-'
|
8
|
+
PLUS = '+'
|
9
|
+
NONE = ' '
|
10
|
+
|
11
|
+
def initialize(code, code_input)
|
12
|
+
@code = code.chars
|
13
|
+
@code_input = code_input.chars
|
14
|
+
end
|
15
|
+
|
16
|
+
def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
|
17
|
+
@inexact = inexact
|
18
|
+
@exact = exact
|
19
|
+
@none = none
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_input
|
23
|
+
raw_result = mismatched
|
24
|
+
matched(raw_result)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def mismatched
|
30
|
+
inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
|
31
|
+
@inexact * inexact
|
32
|
+
end
|
33
|
+
|
34
|
+
def matched(result)
|
35
|
+
@code_input.each.with_index do |element, index|
|
36
|
+
result.sub!(@inexact, @exact) if element == @code[index]
|
37
|
+
end
|
38
|
+
return result unless result.empty?
|
39
|
+
|
40
|
+
@none
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/codebreaker/loader.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Codebreaker
|
2
4
|
module Loader
|
3
5
|
PATH = File.join(File.dirname(__FILE__), 'data/').freeze
|
4
|
-
EXTENCTION = '.yml'
|
6
|
+
EXTENCTION = '.yml'
|
5
7
|
def self.load(file_name)
|
6
8
|
file_name = PATH + file_name + EXTENCTION.to_s
|
7
9
|
if File.exist?(file_name)
|
data/lib/codebreaker/output.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Codebreaker
|
2
4
|
module Validation
|
5
|
+
MIN_NAME_LENGTH = 3
|
6
|
+
MAX_NAME_LENGTH = 20
|
3
7
|
def name_is_valid?(name)
|
4
|
-
name.instance_of?(String) && name.length.between?(
|
8
|
+
name.instance_of?(String) && name.length.between?(MIN_NAME_LENGTH, MAX_NAME_LENGTH)
|
5
9
|
end
|
6
10
|
|
7
11
|
def input_is_valid?(input)
|
data/lib/codebreaker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_kub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- katia kub
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/codebreaker/data/difficulties.yml
|
121
121
|
- lib/codebreaker/data/locales/en.yml
|
122
122
|
- lib/codebreaker/game.rb
|
123
|
+
- lib/codebreaker/guess_checker.rb
|
123
124
|
- lib/codebreaker/loader.rb
|
124
125
|
- lib/codebreaker/output.rb
|
125
126
|
- lib/codebreaker/validation.rb
|