codebreaker_game 0.1.0 → 0.1.1
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/lib/codebreaker_game.rb +14 -8
- data/lib/codebreaker_game/random.rb +7 -6
- data/lib/codebreaker_game/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: c834fd35a0d502315d62fc4d1bf762d86c00358cd7f3c68ae625b7a205d9f21e
|
4
|
+
data.tar.gz: f55dd523970e01fe39e31caeb9c81c249242ae305c8977d257df62d5dc70f389
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25f2e5c012907e01ee90e6277576503008a69786eec71bc2305e7c87a81ac519371f0ddbf7f1fcba1266e8b0a8c17f9c32340dc79881806dd84cb3cba24bf72a
|
7
|
+
data.tar.gz: 8514c94e16c2bb9ccc6472836605de32fbc20e434acde35524fbddb95fc0d58a040eebe349900d35b3b995fc66f0b636f2b3300297a001c681746d7e0fb89d79
|
data/lib/codebreaker_game.rb
CHANGED
@@ -16,10 +16,14 @@ module CodebreakerGame
|
|
16
16
|
STATUS_IN_PROGRESS = 'in_process'.freeze
|
17
17
|
STATUS_WIN = 'win'.freeze
|
18
18
|
STATUS_LOSE = 'lose'.freeze
|
19
|
+
STRONG_INTERSECTION = '+'.freeze
|
20
|
+
NOT_STRONG_INTERSECTION = '-'.freeze
|
21
|
+
SECRET_LENGTH = 4
|
19
22
|
|
20
23
|
def initialize
|
21
|
-
@secret_number = generate_secret
|
24
|
+
@secret_number = generate_secret(SECRET_LENGTH)
|
22
25
|
@status = STATUS_IN_PROGRESS
|
26
|
+
@hints = @secret_number.to_s.chars.shuffle
|
23
27
|
@storage = yaml_store
|
24
28
|
@difficulty_factory = CodebreakerGame::DifficultyFactory.new
|
25
29
|
save_storage unless CodebreakerGame::Game.storage_exists?
|
@@ -36,9 +40,14 @@ module CodebreakerGame
|
|
36
40
|
|
37
41
|
def create_user(name:, difficulty:)
|
38
42
|
@user = User.new(name, difficulty)
|
43
|
+
sample_hints(difficulty.hints)
|
39
44
|
@user
|
40
45
|
end
|
41
46
|
|
47
|
+
def sample_hints(hints)
|
48
|
+
@hints = @hints.sample(hints)
|
49
|
+
end
|
50
|
+
|
42
51
|
def create_difficulty(difficulty:)
|
43
52
|
@difficulty_factory.difficulty_by_name(difficulty)
|
44
53
|
end
|
@@ -58,10 +67,7 @@ module CodebreakerGame
|
|
58
67
|
end
|
59
68
|
|
60
69
|
def hint
|
61
|
-
|
62
|
-
|
63
|
-
user.hint
|
64
|
-
@secret_number.to_s.chars[rand(CodebreakerGame::Random::RANDOM_LENGTH)]
|
70
|
+
@hints.pop
|
65
71
|
end
|
66
72
|
|
67
73
|
private
|
@@ -82,7 +88,7 @@ module CodebreakerGame
|
|
82
88
|
user_digits = number_two.to_s.chars
|
83
89
|
response = pluses(secret_digits, user_digits)
|
84
90
|
response += minuses(secret_digits, user_digits)
|
85
|
-
win if response ==
|
91
|
+
win if response == STRONG_INTERSECTION * SECRET_LENGTH
|
86
92
|
response
|
87
93
|
end
|
88
94
|
|
@@ -91,7 +97,7 @@ module CodebreakerGame
|
|
91
97
|
secret_digits.each_with_index do |digit, index|
|
92
98
|
next if digit != user_digits[index]
|
93
99
|
|
94
|
-
response +=
|
100
|
+
response += STRONG_INTERSECTION
|
95
101
|
secret_digits[index] = nil
|
96
102
|
user_digits[index] = nil
|
97
103
|
end
|
@@ -103,7 +109,7 @@ module CodebreakerGame
|
|
103
109
|
secret_digits.each_with_index do |digit, index|
|
104
110
|
next unless digit != user_digits[index] && user_digits.include?(digit)
|
105
111
|
|
106
|
-
response +=
|
112
|
+
response += NOT_STRONG_INTERSECTION
|
107
113
|
end
|
108
114
|
response
|
109
115
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module CodebreakerGame
|
2
2
|
module Random
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
DEFAULT_MIN = 1
|
4
|
+
DEFAULT_MAX = 6
|
5
|
+
DEFAULT_LENGTH = 4
|
6
|
+
ORDER_AMOUNT = 10
|
6
7
|
|
7
|
-
def generate_secret(length =
|
8
|
+
def generate_secret(length = DEFAULT_LENGTH)
|
8
9
|
number = 0
|
9
10
|
length.times do |degree|
|
10
11
|
number += generate_random * order(degree)
|
@@ -15,10 +16,10 @@ module CodebreakerGame
|
|
15
16
|
private
|
16
17
|
|
17
18
|
def order(degree)
|
18
|
-
|
19
|
+
ORDER_AMOUNT**degree
|
19
20
|
end
|
20
21
|
|
21
|
-
def generate_random(min =
|
22
|
+
def generate_random(min = DEFAULT_MIN, max = DEFAULT_MAX)
|
22
23
|
rand(min..max)
|
23
24
|
end
|
24
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_game
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|