codebreaker_paratskiy 0.1.3 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/codebreaker_paratskiy/version.rb +1 -1
- data/lib/codebreaker_paratskiy.rb +0 -1
- data/lib/config/constants.rb +8 -0
- data/lib/console.rb +9 -9
- data/lib/game.rb +10 -7
- data/lib/services/matching_service.rb +34 -27
- data/lib/services/statistic_service.rb +3 -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: 4c7f0de90040b9ec274970f552b12d959b39648b2c38d26fe21514a9b18000cb
|
4
|
+
data.tar.gz: 0e0070f5f7e6f6472076344851b1f649821a8beb8a875bd4e89ffa91cc16f24f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ea31579120ac5943764fac800d60416382f1edf30b23d91b8baf18bf0ed6f33db63faf34ed1bd3ea260c7cb3601f5ba20ddf6237cbaeed3da33cd1edaeb1dc6
|
7
|
+
data.tar.gz: 148bc5ff6fb9570d0e58925298479450df85d2c6e87160d5b2e56e22e4670ef7869cecaf1819461a54cd12d7fcc4bbc3caaa126730dd6487c4f5685469e6551e
|
data/lib/config/constants.rb
CHANGED
data/lib/console.rb
CHANGED
@@ -32,7 +32,7 @@ class Console
|
|
32
32
|
|
33
33
|
def game_scenario
|
34
34
|
loop do
|
35
|
-
return lost if
|
35
|
+
return lost if game.lost?
|
36
36
|
|
37
37
|
show_msg(:AccompanyingMsg)
|
38
38
|
process_answer_game(user_enter)
|
@@ -42,7 +42,7 @@ class Console
|
|
42
42
|
def process_answer_game(answer)
|
43
43
|
case answer
|
44
44
|
when /^[1-6]{4}$/
|
45
|
-
return won if
|
45
|
+
return won if game.won?(check_code(answer))
|
46
46
|
when 'hint' then request_of_hint
|
47
47
|
else show_msg(:InvalidCommand)
|
48
48
|
end
|
@@ -50,15 +50,15 @@ class Console
|
|
50
50
|
|
51
51
|
def process_answer_menu(answer)
|
52
52
|
send(MAIN_MENU_COMMANDS[answer])
|
53
|
-
main_menu if answer !=
|
53
|
+
main_menu if answer != START_COMMAND
|
54
54
|
end
|
55
55
|
|
56
56
|
def request_of_hint
|
57
|
-
|
57
|
+
game.hints.zero? ? show_msg(:HintsEnded) : (puts game.use_hint)
|
58
58
|
end
|
59
59
|
|
60
60
|
def check_code(answer)
|
61
|
-
result =
|
61
|
+
result = game.result(answer)
|
62
62
|
puts result
|
63
63
|
result
|
64
64
|
end
|
@@ -88,18 +88,18 @@ class Console
|
|
88
88
|
|
89
89
|
def save_result?
|
90
90
|
show_msg(:SaveResult)
|
91
|
-
user_enter ==
|
91
|
+
user_enter == CONFIRM_COMMAND
|
92
92
|
end
|
93
93
|
|
94
94
|
def won
|
95
95
|
show_msg(:Won)
|
96
|
-
|
96
|
+
game.save_result if save_result?
|
97
97
|
main_menu
|
98
98
|
end
|
99
99
|
|
100
100
|
def lost
|
101
101
|
show_msg(:Loss)
|
102
|
-
puts
|
102
|
+
puts game.secret_code.join
|
103
103
|
main_menu
|
104
104
|
end
|
105
105
|
|
@@ -113,6 +113,6 @@ class Console
|
|
113
113
|
end
|
114
114
|
|
115
115
|
def exit?(answer)
|
116
|
-
answer ==
|
116
|
+
answer == EXIT_COMMAND
|
117
117
|
end
|
118
118
|
end
|
data/lib/game.rb
CHANGED
@@ -2,7 +2,8 @@ require_relative 'services/matching_service'
|
|
2
2
|
require_relative 'services/statistic_service'
|
3
3
|
class Game
|
4
4
|
attr_accessor :player_name, :secret_code, :user_code, :attempts, :hints, :difficulty_name
|
5
|
-
attr_reader :stats
|
5
|
+
attr_reader :stats, :secret_code_for_hint
|
6
|
+
|
6
7
|
def initialize(player_name, difficulty)
|
7
8
|
@stats = Statistic.stats
|
8
9
|
@player_name = player_name
|
@@ -10,15 +11,17 @@ class Game
|
|
10
11
|
@attempts = difficulty[:attempts]
|
11
12
|
@hints = difficulty[:hints]
|
12
13
|
@db = DB
|
14
|
+
@secret_code_for_hint = []
|
13
15
|
end
|
14
16
|
|
15
|
-
def
|
17
|
+
def use_hint
|
16
18
|
@hints -= 1
|
17
|
-
|
19
|
+
secret_code_for_hint.sort_by { rand }.pop
|
18
20
|
end
|
19
21
|
|
20
22
|
def run
|
21
23
|
@secret_code = generate_code
|
24
|
+
@secret_code_for_hint = @secret_code.clone
|
22
25
|
end
|
23
26
|
|
24
27
|
def generate_code
|
@@ -28,18 +31,18 @@ class Game
|
|
28
31
|
def result(response)
|
29
32
|
@user_code = response.each_char.map(&:to_i)
|
30
33
|
@attempts -= 1
|
31
|
-
return
|
34
|
+
return WINNING_RESULT if @secret_code == user_code
|
32
35
|
|
33
|
-
Matching.
|
36
|
+
Matching.new(self).create_response
|
34
37
|
end
|
35
38
|
|
36
39
|
def save_result
|
37
40
|
@stats.push(Statistic.generate_stats(self))
|
38
|
-
DbUtils.add(@db,
|
41
|
+
DbUtils.add(@db, stats)
|
39
42
|
end
|
40
43
|
|
41
44
|
def won?(result)
|
42
|
-
result ==
|
45
|
+
result == WINNING_RESULT
|
43
46
|
end
|
44
47
|
|
45
48
|
def lost?
|
@@ -1,41 +1,48 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
user_code_clone.delete_at(user_code_clone.index(user_number)) if user_number == number
|
7
|
-
end
|
8
|
-
end
|
9
|
-
matches.compact! || matches
|
1
|
+
class Matching
|
2
|
+
attr_reader :game
|
3
|
+
|
4
|
+
def initialize(game)
|
5
|
+
@game = game
|
10
6
|
end
|
11
7
|
|
12
|
-
def
|
8
|
+
def create_response
|
13
9
|
@pluses = ''
|
14
10
|
@minuses = ''
|
15
|
-
|
16
|
-
|
17
|
-
"#{@pluses}#{@minuses}#{@spaces}"
|
11
|
+
check_the_code
|
12
|
+
"#{@pluses}#{@minuses}"
|
18
13
|
end
|
19
14
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
@minuses += '-'
|
15
|
+
def exact_matches
|
16
|
+
exact_matches = []
|
17
|
+
game.secret_code.each_index do |index|
|
18
|
+
if current_secret_number(index) == current_user_number(index)
|
19
|
+
exact_matches.push(current_secret_number(index))
|
20
|
+
remove_verified_number(current_user_number(index))
|
28
21
|
end
|
29
|
-
remove_verified_number(match, game)
|
30
22
|
end
|
23
|
+
exact_matches
|
24
|
+
end
|
25
|
+
|
26
|
+
def rest_matches
|
27
|
+
@secret_code_clone & game.user_code
|
31
28
|
end
|
32
29
|
|
33
|
-
def
|
34
|
-
|
30
|
+
def current_secret_number(index)
|
31
|
+
game.secret_code.at(index)
|
32
|
+
end
|
33
|
+
|
34
|
+
def current_user_number(index)
|
35
|
+
game.user_code.at(index)
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_the_code
|
39
|
+
@secret_code_clone = game.secret_code.clone
|
40
|
+
exact_matches.length.times { @pluses += '+' }
|
41
|
+
rest_matches.compact.length.times { @minuses += '-' }
|
35
42
|
end
|
36
43
|
|
37
|
-
def
|
38
|
-
game.user_code[game.user_code.index(number)] =
|
39
|
-
@secret_code_clone[@secret_code_clone.index(number)] =
|
44
|
+
def remove_verified_number(number)
|
45
|
+
game.user_code[game.user_code.index(number)] = nil
|
46
|
+
@secret_code_clone[@secret_code_clone.index(number)] = nil
|
40
47
|
end
|
41
48
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codebreaker_paratskiy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Paratskiy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|