codebreaker_kub 0.2.4 → 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 -1
- data/Gemfile.lock +1 -1
- data/lib/codebreaker/game.rb +18 -11
- data/lib/codebreaker/guess_checker.rb +19 -10
- data/lib/codebreaker/version.rb +1 -1
- metadata +2 -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.lock
CHANGED
data/lib/codebreaker/game.rb
CHANGED
@@ -2,9 +2,8 @@
|
|
2
2
|
|
3
3
|
module Codebreaker
|
4
4
|
class Game
|
5
|
-
|
6
|
-
|
7
|
-
attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
|
5
|
+
attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
|
6
|
+
:all_attempts
|
8
7
|
attr_reader :hints_code
|
9
8
|
|
10
9
|
CODE_LENGTH = 4
|
@@ -14,12 +13,13 @@ module Codebreaker
|
|
14
13
|
@difficulties = Codebreaker::Loader.load('difficulties')
|
15
14
|
@code = generate_code
|
16
15
|
@hints_code = @code.chars.shuffle
|
17
|
-
symbols
|
18
16
|
end
|
19
17
|
|
20
18
|
def game_option(name, difficulty)
|
21
19
|
@name = name
|
22
20
|
@difficulty = difficulty
|
21
|
+
@all_attempts = difficulty_option[:attempts]
|
22
|
+
@all_hints = difficulty_option[:hints]
|
23
23
|
@attempts_left = difficulty_option[:attempts]
|
24
24
|
@hints_left = difficulty_option[:hints]
|
25
25
|
end
|
@@ -40,7 +40,7 @@ module Codebreaker
|
|
40
40
|
return unless attempts_left?
|
41
41
|
|
42
42
|
@attempts_left -= 1
|
43
|
-
check_input(@code, input_code)
|
43
|
+
check_input(@code, @input_code)
|
44
44
|
end
|
45
45
|
|
46
46
|
def attempts_left?
|
@@ -51,8 +51,8 @@ module Codebreaker
|
|
51
51
|
input_code == code
|
52
52
|
end
|
53
53
|
|
54
|
-
def save
|
55
|
-
Codebreaker::Loader.save(to_h,
|
54
|
+
def save(filename = 'stats')
|
55
|
+
Codebreaker::Loader.save(to_h, filename)
|
56
56
|
end
|
57
57
|
|
58
58
|
private
|
@@ -69,15 +69,22 @@ module Codebreaker
|
|
69
69
|
@difficulties[@difficulty]
|
70
70
|
end
|
71
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
|
+
|
72
78
|
def to_h
|
73
79
|
{
|
74
80
|
name: @name,
|
75
81
|
difficulty: @difficulty,
|
76
|
-
attempts:
|
77
|
-
hints:
|
82
|
+
attempts: @all_attempts,
|
83
|
+
hints: @all_hints,
|
78
84
|
code: @code,
|
79
|
-
used_attempts:
|
80
|
-
used_hints:
|
85
|
+
used_attempts: @all_attempts - @attempts_left,
|
86
|
+
used_hints: @all_hints - @hints_left,
|
87
|
+
date: Time.now
|
81
88
|
}
|
82
89
|
end
|
83
90
|
end
|
@@ -1,10 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Codebreaker
|
4
|
-
|
4
|
+
class GuessChecker
|
5
|
+
attr_accessor :inexact, :exact, :none
|
6
|
+
|
5
7
|
MINUSE = '-'
|
6
8
|
PLUS = '+'
|
7
|
-
NONE = ''
|
9
|
+
NONE = ' '
|
10
|
+
|
11
|
+
def initialize(code, code_input)
|
12
|
+
@code = code.chars
|
13
|
+
@code_input = code_input.chars
|
14
|
+
end
|
8
15
|
|
9
16
|
def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
|
10
17
|
@inexact = inexact
|
@@ -12,19 +19,21 @@ module Codebreaker
|
|
12
19
|
@none = none
|
13
20
|
end
|
14
21
|
|
15
|
-
def check_input
|
16
|
-
raw_result =
|
17
|
-
|
22
|
+
def check_input
|
23
|
+
raw_result = mismatched
|
24
|
+
matched(raw_result)
|
18
25
|
end
|
19
26
|
|
20
|
-
|
21
|
-
|
27
|
+
private
|
28
|
+
|
29
|
+
def mismatched
|
30
|
+
inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
|
22
31
|
@inexact * inexact
|
23
32
|
end
|
24
33
|
|
25
|
-
def
|
26
|
-
|
27
|
-
result.sub!(@inexact, @exact) if element == code[index]
|
34
|
+
def matched(result)
|
35
|
+
@code_input.each.with_index do |element, index|
|
36
|
+
result.sub!(@inexact, @exact) if element == @code[index]
|
28
37
|
end
|
29
38
|
return result unless result.empty?
|
30
39
|
|
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.2.
|
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
|