alex_codebreaker 0.1.7 → 0.1.8

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: 9906ea2d8ba009f676ae54c4e40fe92a6c32c2027e925c75b2cc578139d0c0d0
4
- data.tar.gz: e1b386d362478ee47deb158c476ed73fe38c33a85f7a2991967552009e399782
3
+ metadata.gz: e2eafa2c0be75cf61d237253aabc6ef2aeebf618454212a6e334f28904206a99
4
+ data.tar.gz: de56ea76a924a1aa0e720a5a34ba74ecefc31a370910daa685afb1bf5c0af38a
5
5
  SHA512:
6
- metadata.gz: b3bf4021bf22dbbfb4bde1d3e8a73898adc68eb7607c0c05511576e41661eb2032588a3a38cc5895f0be61ea19861c4487e294c97bf1f4fba101e243c11e1856
7
- data.tar.gz: 61c34662972de0c1e127c3740a04e12b33f4ac2fba86c834661f85c55f8321fddcfda9ac97f749a88ef10d1645bbac0e0bca5491b0fa46226aa83ba868a77e4b
6
+ metadata.gz: 892cd158961a99924ea7f5ecf7a1d2f805b13babe3853b70d03bc8c8bb050ab0067d0134cc3353f22945212170c36ea981ee47efdcf1b8a1ebcf77982a3ab90b
7
+ data.tar.gz: b67d9f9d6b214692617a422ea03ab9561b86607accbf1944e34fd11ae2b4aa3df76b1bed0bf3cdfce136c3e34a86850f257ad9e508410058198bc31e8671e4b0
@@ -7,6 +7,7 @@ require_relative 'alex_codebreaker/modules/difficulty_levels'
7
7
  require_relative 'alex_codebreaker/modules/validators'
8
8
  require_relative 'alex_codebreaker/modules/arguments_validation'
9
9
  require_relative 'alex_codebreaker/secret_code_generator'
10
+ require_relative 'alex_codebreaker/comparison'
10
11
  require_relative 'alex_codebreaker/players_rating'
11
12
  require_relative 'alex_codebreaker/session'
12
13
  require_relative 'alex_codebreaker/game'
@@ -0,0 +1,41 @@
1
+ module AlexCodebreaker
2
+ class Comparison
3
+ def initialize(user_input, secret_code_for_comparison)
4
+ @user_input = user_input
5
+ @secret_code_for_comparison = secret_code_for_comparison
6
+ @matching = { place: 0, presence: 0 }
7
+ end
8
+
9
+ def response
10
+ place_matches
11
+ presence_matches
12
+ format_response
13
+ end
14
+
15
+ private
16
+
17
+ def place_matches
18
+ @user_input.each_with_index do |value, index|
19
+ next unless value == @secret_code_for_comparison[index]
20
+
21
+ @secret_code_for_comparison[index] = nil
22
+ @user_input[index] = nil
23
+ @matching[:place] += 1
24
+ end
25
+ end
26
+
27
+ def presence_matches
28
+ @user_input.compact.each do |value|
29
+ if @secret_code_for_comparison.include?(value)
30
+ @matching[:presence] += 1
31
+ @secret_code_for_comparison.delete_at(@secret_code_for_comparison.index(value))
32
+ end
33
+ end
34
+ end
35
+
36
+ def format_response
37
+ AlexCodebreaker::Modules::Settings::MATCHING[:place] * @matching[:place] +
38
+ AlexCodebreaker::Modules::Settings::MATCHING[:presence] * @matching[:presence]
39
+ end
40
+ end
41
+ end
@@ -2,7 +2,7 @@ module AlexCodebreaker
2
2
  class Game
3
3
  include AlexCodebreaker::Modules::ArgumentsValidation
4
4
 
5
- attr_reader :secret_code, :session, :win_status
5
+ attr_reader :secret_code, :session
6
6
 
7
7
  def initialize
8
8
  @secret_code = AlexCodebreaker::SecretCodeGenerator.new.secret_code
@@ -13,60 +13,31 @@ module AlexCodebreaker
13
13
  def hint
14
14
  return unless @session.check_hints
15
15
 
16
- hint_processing
16
+ process_hint
17
17
  end
18
18
 
19
19
  def guess(user_input)
20
20
  return unless guess_validation(user_input) && @session.check_attempts
21
21
 
22
- comparator(user_input)
22
+ compare_codes(user_input)
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def hint_processing
27
+ def process_hint
28
28
  return @secret_code_for_hint.first if @secret_code_for_hint.one?
29
29
 
30
30
  @secret_code_for_hint.delete(@secret_code_for_hint.sample)
31
31
  end
32
32
 
33
- def comparator(user_input)
34
- user_input = user_input.chars.map!(&:to_i)
35
- secret_code_for_comparison = @secret_code.clone
36
- user_input = matching_numbers_and_indexes(user_input, secret_code_for_comparison).compact
37
- user_input = matching_only_numbers(user_input, secret_code_for_comparison).compact
38
- matching_absent(user_input)
33
+ def compare_codes(user_input)
34
+ user_input = format_user_input(user_input)
35
+ comparison = Comparison.new(user_input, @secret_code.clone)
36
+ comparison.response
39
37
  end
40
38
 
41
- def matching_numbers_and_indexes(user_input, secret_code_for_comparison)
42
- user_input.map.with_index do |value, index|
43
- if value == secret_code_for_comparison[index]
44
- secret_code_for_comparison[index] = nil
45
- AlexCodebreaker::Modules::Settings::MATCHING[:place]
46
- else
47
- value
48
- end
49
- end
50
- end
51
-
52
- def matching_only_numbers(user_input, secret_code_for_comparison)
53
- user_input.map do |value|
54
- if secret_code_for_comparison.include?(value)
55
- secret_code_for_comparison.delete_at(secret_code_for_comparison.index(value))
56
- AlexCodebreaker::Modules::Settings::MATCHING[:presence]
57
- elsif value == AlexCodebreaker::Modules::Settings::MATCHING[:place]
58
- value
59
- end
60
- end
61
- end
62
-
63
- def matching_absent(user_input)
64
- AlexCodebreaker::Modules::Settings::MATCHING[:place] *
65
- user_input.count(AlexCodebreaker::Modules::Settings::MATCHING[:place]) +
66
- AlexCodebreaker::Modules::Settings::MATCHING[:presence] *
67
- user_input.count(AlexCodebreaker::Modules::Settings::MATCHING[:presence]) +
68
- AlexCodebreaker::Modules::Settings::MATCHING[:absent] *
69
- (AlexCodebreaker::Modules::Settings::CODE_LENGTH - user_input.length)
39
+ def format_user_input(user_input)
40
+ user_input.chars.map(&:to_i)
70
41
  end
71
42
  end
72
43
  end
@@ -3,23 +3,23 @@ module AlexCodebreaker
3
3
  module DifficultyLevels
4
4
  DIFFICULTY_LEVELS = {
5
5
  easy: {
6
- name: 'Easy',
6
+ name: :easy,
7
7
  level: 0,
8
8
  attempts_total: 15,
9
9
  hints_total: 2
10
- },
10
+ }.freeze,
11
11
  medium: {
12
- name: 'Medium',
12
+ name: :medium,
13
13
  level: 1,
14
14
  attempts_total: 10,
15
15
  hints_total: 1
16
- },
16
+ }.freeze,
17
17
  hell: {
18
- name: 'Hell',
18
+ name: :hell,
19
19
  level: 2,
20
20
  attempts_total: 5,
21
21
  hints_total: 1
22
- }
22
+ }.freeze
23
23
  }.freeze
24
24
  end
25
25
  end
@@ -1,7 +1,7 @@
1
1
  module AlexCodebreaker
2
2
  module Modules
3
3
  module Settings
4
- MATCHING = { place: '+', presence: '-', absent: ' ' }.freeze
4
+ MATCHING = { place: '+', presence: '-' }.freeze
5
5
  CODE_MIN_DIGIT = 1
6
6
  CODE_MAX_DIGIT = 6
7
7
  CODE_LENGTH = 4
@@ -4,25 +4,25 @@ module AlexCodebreaker
4
4
 
5
5
  def initialize
6
6
  @stats = []
7
- stats_loader
7
+ load_and_sort_stats
8
8
  end
9
9
 
10
10
  private
11
11
 
12
- def stats_loader
12
+ def load_and_sort_stats
13
13
  return unless File.exist?(AlexCodebreaker::Modules::Files::STATS_FILE)
14
14
 
15
15
  load_stats
16
- sort_players_rating
16
+ sort_stats
17
17
  end
18
18
 
19
19
  def load_stats
20
20
  File.open(AlexCodebreaker::Modules::Files::STATS_FILE) do |file|
21
- @stats = Array.new(Psych.load_stream(file))
21
+ @stats = Array.new(YAML.load_stream(file))
22
22
  end
23
23
  end
24
24
 
25
- def sort_players_rating
25
+ def sort_stats
26
26
  @stats.sort_by! { |value| [-value.difficulty_level, value.attempts_used, value.hints_used] }
27
27
  end
28
28
  end
@@ -18,9 +18,13 @@ module AlexCodebreaker
18
18
  end
19
19
 
20
20
  def add_difficulty(difficulty)
21
- return unless AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS.include?(difficulty.downcase.to_sym)
21
+ difficulty_level = AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS[difficulty.downcase.to_sym]
22
+ return unless difficulty_level
22
23
 
23
- difficulty(AlexCodebreaker::Modules::DifficultyLevels::DIFFICULTY_LEVELS[difficulty.downcase.to_sym])
24
+ @difficulty_name = difficulty_level[:name]
25
+ @difficulty_level = difficulty_level[:level]
26
+ @attempts_total = difficulty_level[:attempts_total]
27
+ @hints_total = difficulty_level[:hints_total]
24
28
  end
25
29
 
26
30
  def check_hints
@@ -34,14 +38,5 @@ module AlexCodebreaker
34
38
  def save_statistic
35
39
  File.open(AlexCodebreaker::Modules::Files::STATS_FILE, 'a') { |file| file.write(to_yaml) }
36
40
  end
37
-
38
- private
39
-
40
- def difficulty(difficulty)
41
- @difficulty_name = difficulty[:name]
42
- @difficulty_level = difficulty[:level]
43
- @attempts_total = difficulty[:attempts_total]
44
- @hints_total = difficulty[:hints_total]
45
- end
46
41
  end
47
42
  end
@@ -1,5 +1,5 @@
1
1
  module AlexCodebreaker
2
2
  module Version
3
- VERSION = '0.1.7'.freeze
3
+ VERSION = '0.1.8'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alex_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksandr Loza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-16 00:00:00.000000000 Z
11
+ date: 2020-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -160,6 +160,7 @@ files:
160
160
  - LICENSE.txt
161
161
  - README.md
162
162
  - lib/alex_codebreaker.rb
163
+ - lib/alex_codebreaker/comparison.rb
163
164
  - lib/alex_codebreaker/game.rb
164
165
  - lib/alex_codebreaker/modules/arguments_validation.rb
165
166
  - lib/alex_codebreaker/modules/difficulty_levels.rb