andrii_codebreaker 0.1.8 → 0.1.13

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: 1a01bba68d09ade8b529e4f4ced95cdbcb32a89d6dc2dc38a9b3f4f89d9bcad6
4
- data.tar.gz: b19b78984073b7017509da2df5159bcc422ff6359432a88257e84a517369e95b
3
+ metadata.gz: d3d675f4341329d0e515ca662f6b87aec8a6cdb743274481b3aaec5d8608c94a
4
+ data.tar.gz: 3bd3fc3b65b323a88a951b301ec4131ef1aac1bba9c4e7f606256b986776b3a4
5
5
  SHA512:
6
- metadata.gz: b51935c0eb2382f41d9fb8c6859d507eaf7cf21ca7aad9a9f386262ad9b95130af5fdceab79ee424a7b906c934a906bf9358e49316b73e588aebadd5b613939f
7
- data.tar.gz: 594d24a0cd286e63d0b6ab241080d629d4b2bc9bbc5648b57cdae0e159997381587ed7e90c1063e6bd5cc75ab99e6449bbff638e70c2b9c0a43b01bff354d15c
6
+ metadata.gz: ad536af22627af6cd36500bb53dca7edc56bdae0441972774e2550d000cf40f4e65903e95b5bf1912a0732950ab5f1e2b473251a53432f5aca9089cfc9a86a55
7
+ data.tar.gz: e2ab13ed39c070f1c61d27d5f645024753004e90cc7a0d697cf4399be9424f872fd2ac96144e73630b0e8ae6a1093bd3c2c333538a50eca2fb31b16c8f331a94
data/.rubocop.yml CHANGED
@@ -10,12 +10,12 @@ Style/Documentation:
10
10
  Metrics/LineLength:
11
11
  Max: 120
12
12
 
13
- RSpec/AnyInstance:
14
- Enabled: false
15
-
16
13
  Metrics/BlockLength:
17
14
  Exclude:
18
15
  - 'Rakefile'
19
16
  - '**/*.rake'
20
17
  - 'spec/**/*.rb'
21
18
  - '*.gemspec'
19
+
20
+ RSpec/NamedSubject:
21
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,21 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- andrii_codebreaker (0.1.7)
4
+ andrii_codebreaker (0.1.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
10
  colorize (0.8.1)
11
- concurrent-ruby (1.1.9)
12
11
  diff-lcs (1.4.4)
13
12
  docile (1.4.0)
14
13
  fasterer (0.9.0)
15
14
  colorize (~> 0.7)
16
15
  ruby_parser (>= 3.14.1)
17
- i18n (1.8.11)
18
- concurrent-ruby (~> 1.0)
16
+ ffaker (2.20.0)
19
17
  parallel (1.21.0)
20
18
  parser (3.0.2.0)
21
19
  ast (~> 2.4.1)
@@ -67,7 +65,7 @@ PLATFORMS
67
65
  DEPENDENCIES
68
66
  andrii_codebreaker!
69
67
  fasterer
70
- i18n
68
+ ffaker
71
69
  rake
72
70
  rspec
73
71
  rubocop
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.metadata['source_code_uri'] = spec.homepage
21
21
 
22
22
  spec.add_development_dependency 'fasterer'
23
- spec.add_development_dependency 'i18n'
23
+ spec.add_development_dependency 'ffaker'
24
24
  spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'rspec'
26
26
  spec.add_development_dependency 'rubocop'
@@ -2,11 +2,13 @@
2
2
 
3
3
  module AndriiCodebreaker
4
4
  module Constant
5
+ LALA = 2
5
6
  NAME_MIN_LENGTH = 3
6
7
  NAME_MAX_LENGTH = 20
7
8
  CODE_START_LENGTH = 1
8
9
  CODE_LENGTH = 6
9
10
  CODE_LENGTH_COUNT = 4
11
+ RANGE_SECRET_CODE = (CODE_START_LENGTH..CODE_LENGTH).freeze
10
12
  DIFFICULTY = {
11
13
  easy: { attempts: 15, hints: 2 },
12
14
  medium: { attempts: 10, hints: 1 },
@@ -4,22 +4,19 @@ module AndriiCodebreaker
4
4
  class Difficulty
5
5
  include Constant
6
6
 
7
- attr_reader :difficulty
7
+ attr_reader :name, :hints, :attempts
8
8
 
9
- def initialize(difficulty)
10
- @difficulty = difficulty
9
+ def initialize(name)
10
+ validate_difficulty(name)
11
+ @name = name
12
+ @hints = DIFFICULTY[name.to_sym][:hints]
13
+ @attempts = DIFFICULTY[name.to_sym][:attempts]
11
14
  end
12
15
 
13
- def hint
14
- difficult[:hints]
15
- end
16
-
17
- def attempts
18
- difficult[:attempts]
19
- end
16
+ def validate_difficulty(name)
17
+ return DIFFICULTY[name.to_sym] if DIFFICULTY.keys.include? name.to_sym
20
18
 
21
- def difficult
22
- DIFFICULTY[@difficulty.to_sym]
19
+ raise ArgumentError
23
20
  end
24
21
  end
25
22
  end
@@ -5,94 +5,73 @@ module AndriiCodebreaker
5
5
  include Statistic
6
6
  include Constant
7
7
 
8
- attr_reader :player_name, :difficulties, :attempts_total, :hints_total, :secret_code, :difficulty
9
- attr_accessor :hints_left, :attempts_left, :secret_code_copy
10
-
11
- CONST_COMMAND = {
12
- start: 'start',
13
- rules: 'rules',
14
- stats: 'stats',
15
- exit: 'exit'
16
- }.freeze
8
+ attr_reader :player_name, :difficulties, :secret_code
9
+ attr_accessor :used_hints, :used_attempts, :available_hints
17
10
 
18
11
  def initialize(player_name, difficulty)
19
- @player_name = player_name
12
+ @player_name = User.new(player_name)
20
13
  @difficulties = Difficulty.new(difficulty)
21
- @attempts_left = 0
22
- @hints_left = 0
14
+ @used_attempts = 0
15
+ @used_hints = 0
23
16
  end
24
17
 
25
18
  def start
26
19
  @secret_code = generate_code
27
- @secret_code_copy = @secret_code.chars
28
- @hints_total = @difficulties.hint
29
- @attempts_total = @difficulties.attempts
30
- @difficulty = @difficulties.difficulty
20
+ @available_hints = @secret_code.join.dup
31
21
  end
32
22
 
33
- def hints
34
- return unless @hints_total > @hints_left
23
+ def hint
24
+ return unless left_hint?
35
25
 
36
- @hints_left += 1
37
- @secret_code_copy.pop
26
+ hint = @available_hints.chars.sample
27
+ @available_hints.sub!(hint, '')
28
+ @used_hints += 1
29
+ hint
38
30
  end
39
31
 
40
- def compare_codes(guess)
41
- return '' if !guess.match(/^[1-6]{4}$/) && !guess.match(/^\?$/)
42
- return WIN if guess == @secret_code
32
+ def left_hint?
33
+ @used_hints < @difficulties.hints
34
+ end
43
35
 
44
- result = ''
45
- index = []
46
- code_guess = string_to_integer(guess)
47
- secret_code = string_to_integer(@secret_code)
36
+ def left_attempts?
37
+ @used_attempts < @difficulties.attempts
38
+ end
48
39
 
49
- result, index = check_plus(code_guess, result, secret_code, index)
40
+ def win?(guees)
41
+ return true if @secret_code.join == guees
50
42
 
51
- delete_index(code_guess, index)
52
- delete_index(secret_code, index)
43
+ false
44
+ end
45
+
46
+ def lose
47
+ return false if left_attempts?
53
48
 
54
- check_minus(code_guess, result, secret_code, index)
49
+ true
55
50
  end
56
51
 
57
- private
52
+ def compare_codes(guess)
53
+ @used_attempts += 1
58
54
 
59
- def check_plus(*info)
60
- code_guess, result, secret_code, index = info
55
+ guess = guess.chars.map(&:to_i)
56
+ minuses = (@secret_code & guess).map { |element| [@secret_code.count(element), guess.count(element)].min }.sum
57
+ result = MINUS * minuses
61
58
 
62
- code_guess.each_index do |i|
63
- if code_guess[i] == secret_code[i]
64
- result += PLUS
65
- index << i
66
- end
67
- end
68
- [result, index]
59
+ check_result(result, guess)
69
60
  end
70
61
 
71
- def check_minus(*info)
72
- code_guess, result, secret_code, index = info
73
- code_guess.each do |i|
74
- next unless secret_code.include?(i)
75
-
76
- result += MINUS
77
- index = secret_code.index(i)
78
- secret_code.delete_at(index)
62
+ def check_result(result, guess)
63
+ guess.each.with_index do |code, index|
64
+ result.sub!(MINUS, PLUS) if code == @secret_code[index]
79
65
  end
80
66
  result
81
67
  end
82
68
 
83
- def string_to_integer(input_code)
84
- array_int = []
85
- array_chars = input_code.chars
86
- array_chars.each { |i| array_int << i.to_i }
87
- array_int
88
- end
89
-
90
- def delete_index(array, index)
91
- array.map!.with_index { |item, i| item unless index.include?(i) }.compact!
69
+ def generate_code
70
+ Array.new(CODE_LENGTH_COUNT) { rand(RANGE_SECRET_CODE) }
92
71
  end
93
72
 
94
- def generate_code
95
- (CODE_START_LENGTH..CODE_LENGTH_COUNT).map { rand(CODE_START_LENGTH..CODE_LENGTH) }.join
73
+ def code_valid?(code)
74
+ code.to_s.match(/^[1-6]{4}$/)
96
75
  end
97
76
  end
98
77
  end
@@ -6,7 +6,7 @@ module AndriiCodebreaker
6
6
 
7
7
  def statistics
8
8
  data = DbYaml.load_file_stats('result.yml')
9
- data.sort_by { |item| [-DIFFICULTY_SORT[item.difficulty.to_sym], item.attempts_left, item.hints_left] }
9
+ data.sort_by { |item| [-DIFFICULTY_SORT[item.difficulties.name.to_sym], item.used_attempts, item.used_hints] }
10
10
  end
11
11
  end
12
12
  end
@@ -7,17 +7,11 @@ module AndriiCodebreaker
7
7
  attr_reader :name
8
8
 
9
9
  def initialize(name)
10
- @name = validates_name(name)
10
+ @name = name
11
11
  end
12
12
 
13
- private
14
-
15
- def validates_name(name)
16
- name if valid_name?(name)
17
- end
18
-
19
- def valid_name?(name)
20
- name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
13
+ def self.validate_name(name)
14
+ name if name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
21
15
  end
22
16
  end
23
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AndriiCodebreaker
4
- VERSION = '0.1.8'
4
+ VERSION = '0.1.13'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: andrii_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-21 00:00:00.000000000 Z
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: i18n
28
+ name: ffaker
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="