new_super_codebreaker_2021 0.4.4 → 0.4.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: 310473574af90875c8838fadf808a3293e7667f1452c90b28618906f20f4d43d
4
- data.tar.gz: 7df218a49809e4811ed1adc27093fc4bc9a7353c23b82bf81160421989b7c5a3
3
+ metadata.gz: e6e66f5272359e959f412ca6dc0d7363d7ede198844dcdbcd9878701b90347cf
4
+ data.tar.gz: 450d2f983d577a9ccc0c979cb29b8ae8364c3542d809db80dec89482910604c5
5
5
  SHA512:
6
- metadata.gz: 690e606a26291342c8fe91387db9d467403e179bbed937f9bc0ad1ae11cb9f7a7970d51ea2295a836910a500a7b73608f51cbbaab24190f2f4fff58d5cd84f07
7
- data.tar.gz: 7b89164a1189f3a81f360fec5cf5fe839708c314b9497e1dbd307b12f607be24e083fa4a78f3790b6b225936fe0e3d925a4bd5e39756cc44bf5e02b21c3f73ea
6
+ metadata.gz: 15def3bd39bf2b810370cf2310d1ac613013a3179da4565459214f2533fe057222b16b4f27bbea189e236f13f4eb022cce560c8983420c0f5a7fa41ff89392f1
7
+ data.tar.gz: 12586743080a5fc9dff5b8e32155f327a95de55ca7355f982f70b530f548daadc8098c268777a78bc6e35a631d7febe1e8d1da309fd933ca272863e388640fae
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- new_super_codebreaker_2021 (0.1.0)
4
+ new_super_codebreaker_2021 (0.4.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -13,6 +13,7 @@ GEM
13
13
  fasterer (0.9.0)
14
14
  colorize (~> 0.7)
15
15
  ruby_parser (>= 3.14.1)
16
+ ffaker (2.19.0)
16
17
  parallel (1.20.1)
17
18
  parser (3.0.2.0)
18
19
  ast (~> 2.4.1)
@@ -54,6 +55,8 @@ GEM
54
55
  simplecov_json_formatter (~> 0.1)
55
56
  simplecov-html (0.12.3)
56
57
  simplecov_json_formatter (0.1.3)
58
+ terminal-table (3.0.1)
59
+ unicode-display_width (>= 1.1.1, < 3)
57
60
  unicode-display_width (2.0.0)
58
61
 
59
62
  PLATFORMS
@@ -61,11 +64,13 @@ PLATFORMS
61
64
 
62
65
  DEPENDENCIES
63
66
  fasterer (~> 0.9.0)
67
+ ffaker
64
68
  new_super_codebreaker_2021!
65
69
  rake (~> 13.0)
66
70
  rspec (~> 3.10.0)
67
71
  rubocop (~> 1.7)
68
- simplecov
72
+ simplecov (~> 0.21.2)
73
+ terminal-table (~> 3.0.1)
69
74
 
70
75
  BUNDLED WITH
71
76
  2.2.26
data/lib/game.rb CHANGED
@@ -5,12 +5,15 @@ require_relative 'db_methods'
5
5
  require_relative 'user'
6
6
  module NewSuperCodebreaker2021
7
7
  class Game
8
+ attr_reader :code
9
+
8
10
  include Validate
9
11
  include ShowContent
10
12
  include DBMethods
11
13
 
12
14
  def initialize
13
15
  @code = generate_code
16
+ @code_copy = @code.dup
14
17
  end
15
18
 
16
19
  GUESS_COMMANDS = %i[hint rules exit].freeze
@@ -36,23 +39,18 @@ module NewSuperCodebreaker2021
36
39
  end
37
40
 
38
41
  def user_guess(code)
39
- if code.to_i != 0
40
- validate_user_code(code)
41
- elsif GUESS_COMMANDS.include?(code.to_sym)
42
- code.to_sym
43
- else false
44
- end
42
+ return validate_user_code(code) unless code.to_i.zero?
43
+
44
+ symbol_code = code.to_sym
45
+ GUESS_COMMANDS.include?(symbol_code) ? symbol_code : nil
45
46
  end
46
47
 
47
48
  def take_hint(user, used_hints)
48
- code_copy = @code.dup
49
- if user.hints_total > user.hints_used
50
- user.hints_used += 1
51
- used_hints.each { |hint| code_copy.delete(hint) }
52
- code_copy.sample
53
- else
54
- false
55
- end
49
+ return unless user.hints_total > user.hints_used
50
+
51
+ user.hints_used += 1
52
+ used_hints.each { |hint| @code_copy.delete(hint) }
53
+ @code_copy.sample
56
54
  end
57
55
 
58
56
  def after_game_commands(command)
@@ -64,27 +62,34 @@ module NewSuperCodebreaker2021
64
62
  end
65
63
 
66
64
  def compare_codes(user_code)
67
- matches, u_char = number_on_right_place(user_code)
68
- number_in_secret_code(user_code, matches, u_char)
65
+ matches, user_code, code_copy = number_on_right_place(user_code)
66
+ number_in_secret_code(user_code, matches, code_copy)
69
67
  end
70
68
 
71
69
  private
72
70
 
73
71
  def number_on_right_place(user_code)
72
+ code_copy = @code.dup
74
73
  matches = []
75
- u_char = []
76
74
  user_code.each_index do |i|
77
- if @code[i] == user_code[i]
78
- matches.unshift('+')
79
- u_char << user_code[i]
80
- end
75
+ next unless @code[i] == user_code[i]
76
+
77
+ matches.unshift('+')
78
+ user_code[i] = nil
79
+ code_copy[i] = false
81
80
  end
82
- [matches, u_char]
81
+ [matches, user_code, code_copy]
83
82
  end
84
83
 
85
- def number_in_secret_code(user_code, matches, u_char)
84
+ def number_in_secret_code(user_code, matches, code_copy)
85
+ amount_numbers_in_secret_code = Hash.new(0)
86
+ amount_numbers_in_user_code = Hash.new(0)
87
+ code_copy.each { |number| amount_numbers_in_secret_code[number] += 1 }
86
88
  user_code.each do |element|
87
- matches.push('-') if @code.include?(element) && !u_char.include?(element)
89
+ if code_copy.include?(element) && amount_numbers_in_user_code[element] < amount_numbers_in_secret_code[element]
90
+ matches.push('-')
91
+ amount_numbers_in_user_code[element] += 1
92
+ end
88
93
  end
89
94
  matches
90
95
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NewSuperCodebreaker2021
4
- VERSION = '0.4.4'
4
+ VERSION = '0.4.5'
5
5
  end
data/lib/show_content.rb CHANGED
@@ -1,15 +1,9 @@
1
1
  require 'terminal-table'
2
2
 
3
3
  module ShowContent
4
- DIFFICULTY = {
5
- easy: { attempts: 15, hints: 2 },
6
- medium: { attempts: 10, hints: 1 },
7
- hell: { attempts: 5, hints: 1 }
8
- }.freeze
9
-
10
4
  def show_stats(file)
11
5
  data = YAML.load_file(file) || []
12
- data.sort_by(&:hints_used).sort_by(&:attempts_used).sort_by { |game| -game.difficulty }
6
+ data.sort_by! { |game| [-game.difficulty, game.attempts_used, game.hints_used] }
13
7
  rescue Errno::ENOENT
14
8
  []
15
9
  end
data/lib/user.rb CHANGED
@@ -1,32 +1,28 @@
1
+ require_relative 'show_content'
2
+
1
3
  class User
2
4
  attr_reader :name, :difficulty, :hints_total, :attempts_total
3
5
  attr_accessor :hints_used, :attempts_used
4
6
 
7
+ DIFFICULTY = {
8
+ easy: { attempts: 15, hints: 2 },
9
+ medium: { attempts: 10, hints: 1 },
10
+ hell: { attempts: 5, hints: 1 }
11
+ }.freeze
12
+
5
13
  def initialize(name, difficulty)
6
14
  @name = name
7
15
  @difficulty = difficulty
8
16
  @hints_used = 0
9
17
  @attempts_used = 0
10
- check_difficulty(difficulty)
18
+ set_total_fields
11
19
  end
12
20
 
13
21
  private
14
22
 
15
- def check_difficulty(difficulty)
16
- case difficulty
17
- when 0
18
- set_total_fields(2, 15)
19
- when 1
20
- set_total_fields(1, 10)
21
- when 2
22
- set_total_fields(1, 5)
23
- else
24
- false
25
- end
26
- end
27
-
28
- def set_total_fields(hints_total, attempts_total)
29
- @hints_total = hints_total
30
- @attempts_total = attempts_total
23
+ def set_total_fields
24
+ difficulty = DIFFICULTY.keys[@difficulty]
25
+ @hints_total = DIFFICULTY[difficulty][:hints]
26
+ @attempts_total = DIFFICULTY[difficulty][:attempts]
31
27
  end
32
28
  end
data/lib/validate.rb CHANGED
@@ -1,35 +1,32 @@
1
1
  module Validate
2
2
  def validate_name(name)
3
- if name.length >= 3 && name.length <= 20
4
- name
5
- else
6
- false
7
- end
3
+ name if valid_name?(name)
8
4
  end
9
5
 
10
6
  def validate_user_code(us_code)
11
- arr_code = check_splitting(us_code)
12
- if valid_number?(arr_code)
13
- arr_code
14
- else
15
- false
16
- end
7
+ arr_code = split_to_integer_array(us_code)
8
+ return unless valid_number?(arr_code)
9
+
10
+ arr_code
17
11
  end
18
12
 
19
13
  def check_input(input, command_list)
20
- if input.to_i.zero? && command_list.include?(input.to_sym)
21
- input.to_sym
22
- else false
23
- end
14
+ return unless valid_input?(input, command_list)
15
+
16
+ input.to_sym
24
17
  end
25
18
 
26
19
  private
27
20
 
21
+ def valid_input?(input, command_list)
22
+ input.to_i.zero? && command_list.include?(input.to_sym)
23
+ end
24
+
28
25
  def valid_number?(arr_code)
29
- arr_code && check_length?(arr_code) && check_numbers?(arr_code)
26
+ arr_code && check_code_length?(arr_code) && check_numbers?(arr_code)
30
27
  end
31
28
 
32
- def check_splitting(code)
29
+ def split_to_integer_array(code)
33
30
  code.chars.map!(&:to_i) if integer?(code)
34
31
  end
35
32
 
@@ -37,14 +34,15 @@ module Validate
37
34
  code.to_i.to_s == code
38
35
  end
39
36
 
40
- def check_length?(code)
37
+ def check_code_length?(code)
41
38
  code.length == 4
42
39
  end
43
40
 
44
41
  def check_numbers?(code)
45
- code.each do |number|
46
- return if number < 1 || number > 6
47
- end
48
- true
42
+ code.all? { |value| value.between?(1, 6) }
43
+ end
44
+
45
+ def valid_name?(name)
46
+ name.length >= 3 && name.length <= 20
49
47
  end
50
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: new_super_codebreaker_2021
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nazar Dakhno
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-10 00:00:00.000000000 Z
11
+ date: 2021-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: ffaker
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.19.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.19.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement