andrii_codebreaker 0.1.7 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 614726c17f68e0208588be0daa78170ff2dca52f3cbb48017faa565b96673a0c
4
- data.tar.gz: fd3c636ff49aaace2fda7055aa2ce90155709e0c56edb889cad671d6e4f43105
3
+ metadata.gz: 32857bb09a13937194578bcc74d883ab93b151300e6948a0b3b0b1a2121c50f7
4
+ data.tar.gz: 6b01d5ac0c18f89b6006bb48aa0ff21ad98a5ced20c0209d728f95f986dae566
5
5
  SHA512:
6
- metadata.gz: 0c5603c20de947e67d699355cec83a2f4160da98d133c8516b10bd45cff5ccfc76600092f9ef74ab040d05631ae8032ec08147908dcee0775e4a2f202e618cc3
7
- data.tar.gz: b6ae17bb6609a3ea2b6dbe9dd78e024d425c31b1428dce613660ab004389bd93d17dd2235b222fe591db4fa6fb2aea3c2b08717f39009a4ecb9ba13f61d96ebc
6
+ metadata.gz: eed8485cfdbe585ab513a4b87f00baa661546e62351f53fa565861a28aad46ddcacc4bbdf94066b256c956f98bf5927e6033536c09cdd03e6445146d32c61eac
7
+ data.tar.gz: 20d8163febd298a40a69034893b372a27af6ca6b36de323d17359c67df0855685ff4566514527b808598c4d904f338e2e7f7e62917bb326be050e4e5066d96e1
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.11)
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,93 +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
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
20
+ @available_hints = @secret_code.join.dup
30
21
  end
31
22
 
32
- def hints
33
- return unless @hints_total > @hints_left
23
+ def hint
24
+ return unless left_hint?
34
25
 
35
- @hints_left += 1
36
- @secret_code_copy.pop
26
+ hint = @available_hints.chars.sample
27
+ @available_hints.sub!(hint, '')
28
+ @used_hints += 1
29
+ hint
37
30
  end
38
31
 
39
- def compare_codes(guess)
40
- return '' if !guess.match(/^[1-6]{4}$/) && !guess.match(/^\?$/)
41
- return WIN if guess == @secret_code
32
+ def left_hint?
33
+ @used_hints < @difficulties.hints
34
+ end
42
35
 
43
- result = ''
44
- index = []
45
- code_guess = string_to_integer(guess)
46
- secret_code = string_to_integer(@secret_code)
36
+ def left_attempts?
37
+ @used_attempts < @difficulties.attempts
38
+ end
47
39
 
48
- result, index = check_plus(code_guess, result, secret_code, index)
40
+ def win?(guees)
41
+ return true if @secret_code.join == guees
49
42
 
50
- delete_index(code_guess, index)
51
- delete_index(secret_code, index)
43
+ false
44
+ end
45
+
46
+ def lose
47
+ return true if left_attempts?
52
48
 
53
- check_minus(code_guess, result, secret_code, index)
49
+ false
54
50
  end
55
51
 
56
- private
52
+ def compare_codes(guess)
53
+ @used_attempts += 1
57
54
 
58
- def check_plus(*info)
59
- 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
60
58
 
61
- code_guess.each_index do |i|
62
- if code_guess[i] == secret_code[i]
63
- result += '+'
64
- index << i
65
- end
66
- end
67
- [result, index]
59
+ check_result(result, guess)
68
60
  end
69
61
 
70
- def check_minus(*info)
71
- code_guess, result, secret_code, index = info
72
- code_guess.each do |i|
73
- next unless secret_code.include?(i)
74
-
75
- result << MINUS
76
- index = secret_code.index(i)
77
- 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]
78
65
  end
79
66
  result
80
67
  end
81
68
 
82
- def string_to_integer(input_code)
83
- array_int = []
84
- array_chars = input_code.chars
85
- array_chars.each { |i| array_int << i.to_i }
86
- array_int
87
- end
88
-
89
- def delete_index(array, index)
90
- 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) }
91
71
  end
92
72
 
93
- def generate_code
94
- (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}$/)
95
75
  end
96
76
  end
97
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.7'
4
+ VERSION = '0.1.12'
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.7
4
+ version: 0.1.12
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-28 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
  - - ">="