andrii_codebreaker 0.1.6 → 0.1.11

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: 850117beac1f8b9441630a8a0f97ef5a6c374e671dce56bedc85d9f43b74d828
4
- data.tar.gz: c52fce0dcceb07cb99bb0ce693b7e38469b380e4310c990e9783cf53acb4fbb7
3
+ metadata.gz: 4bbc75e2ca53efd3c8c664765094884f8b2f1238c3b010b66f1652d41fbe3875
4
+ data.tar.gz: c929acbe775de6c16915289aab8ebcba90cc64c6e1f57bd671526552adeb6688
5
5
  SHA512:
6
- metadata.gz: 614863a6b0e5d83c2e3752d5244146fc8aaebf1452dcdb367172da66bb8c6c9c6a838fc347e4af237d57792b76676b30dc55224acf1d7cc963cc0a746aa5e00a
7
- data.tar.gz: 32267d352720c6009cd3b4bff6f3c130e1dae175f6b90b423c1b4ff8ac42459f9b252b0934d55c2fc0e4a22118d2c89f72637083c8ee4a9def75b16ae6331dc0
6
+ metadata.gz: a034dc7c27ac804ca5ccfa9810134a34d71d9ca10af0da69123ea91e5d5e0bf88842ce272fc4c7d0946cd8b89738bcb87c7c61168119c9c042c46ddf9603c592
7
+ data.tar.gz: 48344173cf96f64068804b5e5e5074cbf3338a43731b36c509d5fade4db96f4dee327ac2e7565ea5a41042dba5d3174c996478c80424b765fcb313817110dd87
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.6)
4
+ andrii_codebreaker (0.1.10)
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,16 +2,21 @@
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 },
13
15
  hell: { attempts: 5, hints: 1 }
14
16
  }.freeze
15
17
  DIFFICULTY_SORT = { easy: 1, medium: 2, hell: 3 }.freeze
18
+ WIN = '++++'
19
+ MINUS = '-'
20
+ PLUS = '+'
16
21
  end
17
22
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AndriiCodebreaker
4
+ class Difficulty
5
+ include Constant
6
+
7
+ attr_reader :name, :hints, :attempts
8
+
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]
14
+ end
15
+
16
+ def validate_difficulty(name)
17
+ return DIFFICULTY[name.to_sym] if DIFFICULTY.keys.include? name.to_sym
18
+
19
+ raise ArgumentError
20
+ end
21
+ end
22
+ end
@@ -2,82 +2,76 @@
2
2
 
3
3
  module AndriiCodebreaker
4
4
  class Game
5
- include Validate
6
5
  include Statistic
7
6
  include Constant
8
7
 
9
- attr_reader :player_name, :difficulty, :attempts_total, :hints_total, :secret_code
10
- attr_accessor :hints_left, :attempts_left, :secret_code_copy
11
-
12
- CONST_COMMAND = {
13
- start: 'start',
14
- rules: 'rules',
15
- stats: 'stats',
16
- exit: 'exit'
17
- }.freeze
8
+ attr_reader :player_name, :difficulties, :secret_code
9
+ attr_accessor :used_hints, :used_attempts, :available_hints
18
10
 
19
11
  def initialize(player_name, difficulty)
20
- @player_name = player_name
21
- @difficulty = difficulty
22
- @attempts_left = 0
23
- @hints_left = 0
24
- initialize_class
12
+ @player_name = User.new(player_name)
13
+ @difficulties = Difficulty.new(difficulty)
14
+ @used_attempts = 0
15
+ @used_hints = 0
25
16
  end
26
17
 
27
- def initialize_class
28
- difficult = DIFFICULTY[@difficulty.to_sym]
29
- @hints_total = difficult[:hints]
30
- @attempts_total = difficult[:attempts]
18
+ def start
31
19
  @secret_code = generate_code
32
- @secret_code_copy = @secret_code.dup.chars
20
+ @available_hints = @secret_code.join.dup
33
21
  end
34
22
 
35
- def show_hints
36
- hints_left
23
+ def hint
24
+ return unless left_hint?
25
+
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 exact_match(guess)
32
+ def left_hint?
33
+ @used_hints < @difficulties.hints
34
+ end
41
35
 
42
- ('+' * pluses(guess)) + ('-' * minuses(guess))
36
+ def left_attempts?
37
+ @used_attempts < @difficulties.attempts
43
38
  end
44
39
 
45
- private
40
+ def win?(guees)
41
+ return true if @secret_code.join == guees
46
42
 
47
- def exact_match(guess)
48
- @secret_code == guess
43
+ false
49
44
  end
50
45
 
51
- def pluses(guess)
52
- zipped(guess)
53
- .select { |el| el[0] == el[1] }
54
- .count
46
+ def lose
47
+ return false unless left_attempts?
48
+
49
+ true
55
50
  end
56
51
 
57
- def minuses(guess)
58
- return 0 if exact_match(guess)
52
+ def compare_codes(guess)
53
+ @used_attempts += 1
59
54
 
60
- arr = delete_pairs(guess)
61
- arr[1].each do |number|
62
- next unless arr[0].include?(number)
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
63
58
 
64
- arr[0].delete_at(arr[0].index(number))
65
- end
66
- arr[1].size - arr[0].size
59
+ check_result(result, guess)
67
60
  end
68
61
 
69
- def zipped(guess)
70
- @secret_code.chars.zip(guess.chars)
62
+ def check_result(result, guess)
63
+ guess.each.with_index do |code, index|
64
+ result.sub!(MINUS, PLUS) if code == @secret_code[index]
65
+ end
66
+ result
71
67
  end
72
68
 
73
- def delete_pairs(guess)
74
- zipped(guess)
75
- .delete_if { |el| el[0] == el[1] }
76
- .transpose
69
+ def generate_code
70
+ Array.new(CODE_LENGTH_COUNT) { rand(RANGE_SECRET_CODE) }
77
71
  end
78
72
 
79
- def generate_code
80
- (Array.new(CODE_LENGTH_COUNT) { rand(CODE_START_LENGTH..CODE_LENGTH) }).join.to_s
73
+ def code_valid?(code)
74
+ code.to_s.match(/^[1-6]{4}$/)
81
75
  end
82
76
  end
83
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
@@ -2,26 +2,16 @@
2
2
 
3
3
  module AndriiCodebreaker
4
4
  class User
5
- include AndriiCodebreaker::Validate
5
+ include Constant
6
6
 
7
7
  attr_reader :name
8
8
 
9
9
  def initialize(name)
10
10
  @name = name
11
- validate_name
12
11
  end
13
12
 
14
- def hints(game)
15
- return unless game.hints_total > game.hints_left
16
-
17
- game.hints_left += 1
18
- game.secret_code_copy.pop
19
- end
20
-
21
- private
22
-
23
- def validate_name
24
- @name = validates_name(name)
13
+ def self.validate_name(name)
14
+ name if name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
25
15
  end
26
16
  end
27
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AndriiCodebreaker
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.11'
5
5
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  require 'yaml'
4
4
  require_relative 'andrii_codebreaker/constants'
5
- require_relative 'andrii_codebreaker/validate/validate'
6
5
  require_relative 'andrii_codebreaker/model'
7
6
  require_relative 'andrii_codebreaker/user'
7
+ require_relative 'andrii_codebreaker/difficulty'
8
8
  require_relative 'andrii_codebreaker/statistic'
9
9
  require_relative 'andrii_codebreaker/version'
10
10
  require_relative 'andrii_codebreaker/game'
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.6
4
+ version: 0.1.11
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-13 00:00:00.000000000 Z
11
+ date: 2021-11-25 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
  - - ">="
@@ -127,17 +127,16 @@ files:
127
127
  - LICENSE.txt
128
128
  - README.md
129
129
  - Rakefile
130
- - andrii_codebreaker-0.1.5.gem
131
130
  - andrii_codebreaker.gemspec
132
131
  - bin/console
133
132
  - bin/setup
134
133
  - lib/andrii_codebreaker.rb
135
134
  - lib/andrii_codebreaker/constants.rb
135
+ - lib/andrii_codebreaker/difficulty.rb
136
136
  - lib/andrii_codebreaker/game.rb
137
137
  - lib/andrii_codebreaker/model.rb
138
138
  - lib/andrii_codebreaker/statistic.rb
139
139
  - lib/andrii_codebreaker/user.rb
140
- - lib/andrii_codebreaker/validate/validate.rb
141
140
  - lib/andrii_codebreaker/version.rb
142
141
  homepage: https://github.com/Andrii-RubyGarage/Codebreker-Gem
143
142
  licenses:
Binary file
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module AndriiCodebreaker
4
- module Validate
5
- include Constant
6
-
7
- def validates_name(name)
8
- name if valid_name?(name)
9
- end
10
-
11
- def validate_guess(code)
12
- code = string_to_integer(code)
13
- true if check_code_length?(code) && check_numbers?(code)
14
- end
15
-
16
- private
17
-
18
- def string_to_integer(input_code)
19
- array_int = []
20
- array_chars = input_code.chars
21
- array_chars.each { |i| array_int << i.to_i }
22
- array_int
23
- end
24
-
25
- def valid_name?(name)
26
- name.length >= NAME_MIN_LENGTH && name.length <= NAME_MAX_LENGTH
27
- end
28
-
29
- def check_code_length?(code)
30
- code.length == CODE_LENGTH_COUNT
31
- end
32
-
33
- def check_numbers?(code)
34
- code.all? { |value| value.between?(CODE_START_LENGTH, CODE_LENGTH) }
35
- end
36
- end
37
- end