codebreaker_paratskiy 0.1.3 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc88f1830d8e2791f5f3e8ea508fbb5f80799b1d988d078c1dcd00d29fd6a9d5
4
- data.tar.gz: 80e9b346a177f00ba3db89d97cf28ad26e503db15b53f45d90581c13de806bd3
3
+ metadata.gz: 4c7f0de90040b9ec274970f552b12d959b39648b2c38d26fe21514a9b18000cb
4
+ data.tar.gz: 0e0070f5f7e6f6472076344851b1f649821a8beb8a875bd4e89ffa91cc16f24f
5
5
  SHA512:
6
- metadata.gz: 48dec98c1fb5c73da45dfe8364504ce608495fafec2cc6e2bcabf2ab252cc934ca36878911fa269a7c5043144d1758777a3b1803f2f48b2edae13c0147632e4d
7
- data.tar.gz: 4fd5da4b654e8e7953853a10631f67140436ad176483b9fc6f81fa06c59cc76813b46439cc813b4ee923012cc5be464770ed9eef69a9d9ec6835a0faa5428a93
6
+ metadata.gz: 5ea31579120ac5943764fac800d60416382f1edf30b23d91b8baf18bf0ed6f33db63faf34ed1bd3ea260c7cb3601f5ba20ddf6237cbaeed3da33cd1edaeb1dc6
7
+ data.tar.gz: 148bc5ff6fb9570d0e58925298479450df85d2c6e87160d5b2e56e22e4670ef7869cecaf1819461a54cd12d7fcc4bbc3caaa126730dd6487c4f5685469e6551e
@@ -1,3 +1,3 @@
1
1
  module CodebreakerParatskiy
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
@@ -6,4 +6,3 @@ module CodebreakerParatskiy
6
6
  game
7
7
  end
8
8
  end
9
- # game = CodebreakerParatskiy.run_game('asdf', { attempts: 5, hints: 1})
@@ -12,3 +12,11 @@ MAIN_MENU_COMMANDS = {
12
12
  'stats' => :show_stats,
13
13
  'exit' => :exit
14
14
  }.freeze
15
+
16
+ WINNING_RESULT = '++++'.freeze
17
+
18
+ START_COMMAND = 'start'.freeze
19
+
20
+ CONFIRM_COMMAND = 'yes'.freeze
21
+
22
+ EXIT_COMMAND = 'exit'.freeze
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 @game.lost?
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 @game.won?(check_code(answer))
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 != 'start'
53
+ main_menu if answer != START_COMMAND
54
54
  end
55
55
 
56
56
  def request_of_hint
57
- @game.hints.zero? ? show_msg(:HintsEnded) : (puts @game.give_hint)
57
+ game.hints.zero? ? show_msg(:HintsEnded) : (puts game.use_hint)
58
58
  end
59
59
 
60
60
  def check_code(answer)
61
- result = @game.result(answer)
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 == 'yes'
91
+ user_enter == CONFIRM_COMMAND
92
92
  end
93
93
 
94
94
  def won
95
95
  show_msg(:Won)
96
- @game.save_result if save_result?
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 @game.secret_code.join
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 == 'exit'
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 give_hint
17
+ def use_hint
16
18
  @hints -= 1
17
- @secret_code.sample
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 '++++' if @secret_code == user_code
34
+ return WINNING_RESULT if @secret_code == user_code
32
35
 
33
- Matching.create_response(self)
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, @stats)
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
- module Matching
2
- def self.matches(game)
3
- user_code_clone = game.user_code.clone
4
- matches = game.secret_code.map do |number|
5
- user_code_clone.find do |user_number|
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 self.create_response(game)
8
+ def create_response
13
9
  @pluses = ''
14
10
  @minuses = ''
15
- @spaces = ''
16
- check_the_code(game)
17
- "#{@pluses}#{@minuses}#{@spaces}"
11
+ check_the_code
12
+ "#{@pluses}#{@minuses}"
18
13
  end
19
14
 
20
- def self.check_the_code(game)
21
- @secret_code_clone = game.secret_code.clone
22
- (4 - matches(game).length).times { @spaces += ' ' }
23
- matches(game).each do |match|
24
- if same_position?(game, match)
25
- @pluses += '+'
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 self.same_position?(game, match)
34
- [game.user_code[@secret_code_clone.index(match)], @secret_code_clone[game.user_code.index(match)]].include? match
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 self.remove_verified_number(number, game)
38
- game.user_code[game.user_code.index(number)] = 0
39
- @secret_code_clone[@secret_code_clone.index(number)] = 0
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
@@ -1,6 +1,8 @@
1
1
  require_relative '../utils/db_utils'
2
2
  require_relative '../config/constants'
3
- module Statistic
3
+ class Statistic
4
+ attr_reader :game
5
+
4
6
  def self.generate_stats(game)
5
7
  {
6
8
  name: game.player_name,
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.3
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-29 00:00:00.000000000 Z
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler