codebreaker_kub 0.2.1 → 0.2.6

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: f04c487aaa2731e942ffff6cb60ef16d1104251d00bb9983a6e7d91c103b7169
4
- data.tar.gz: 4b505fe8bfe8a2e60e0f3915db5f3e4afe8175f06812602d4f1d972c9dcb31f4
3
+ metadata.gz: 34ece6de532a41fce0ebb9bdc5c43b78967db6d7685030efbfecddc76189d846
4
+ data.tar.gz: 64c3f7249d16a91c5244f431d20ddd8d60de2dafdc82a3997c79168b10afa5c9
5
5
  SHA512:
6
- metadata.gz: f4479ce9fa5fc088e8acdacc83756ea2842711fcc410f7885df6a70461906df3d9bbad0c0fe8f54df0ec98afe14d83f7574fbdd9d2b77c7ab7644da3f56481d9
7
- data.tar.gz: 24ef175cf81346eb5a3eb83553ac0e31ea77cb95e09502c3428e4cba77413d1ec55eeb578502bcd1b423d3e02fc9cceac6f622f15ed7fdfc19c77ce7af205141
6
+ metadata.gz: 1820cb27ce2f71ecf10738033bcb81b64a0c9f7e914e4af57d4a2fc565af045a33ed9d14d24a681ccbf94935bcfeb01cd1e9536255806f7f6e7da7e3d1aea772
7
+ data.tar.gz: 2c55801f6013e94165389014838eec3e94afdbad7d1fe2589f83e44000c58aaaceefe062d7e3c101ffbf6f70a73863989f879bc2309394825bd056c8d22a1718
@@ -5,7 +5,7 @@ Style/Documentation:
5
5
  Enabled: false
6
6
 
7
7
  Layout/LineLength:
8
- Max: 110
8
+ Max: 120
9
9
 
10
10
  Metrics/BlockLength:
11
11
  Enabled: false
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_kub (0.1.9)
4
+ codebreaker_kub (0.2.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'codebreaker/guess_checker'
3
4
  require_relative 'codebreaker/game'
4
5
  require_relative 'codebreaker/loader'
5
6
  require_relative 'codebreaker/output'
@@ -2,25 +2,24 @@
2
2
 
3
3
  module Codebreaker
4
4
  class Game
5
+ attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
6
+ :all_attempts
7
+ attr_reader :hints_code
8
+
5
9
  CODE_LENGTH = 4
6
10
  CODE_RANGE = (1..6).freeze
7
- attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
8
- attr_reader :minuse, :plus, :none
11
+
9
12
  def initialize
10
13
  @difficulties = Codebreaker::Loader.load('difficulties')
11
14
  @code = generate_code
12
- symbols
13
- end
14
-
15
- def symbols(minuse = '-', plus = '+', none = '')
16
- @minuse = minuse
17
- @plus = plus
18
- @none = none
15
+ @hints_code = @code.chars.shuffle
19
16
  end
20
17
 
21
18
  def game_option(name, difficulty)
22
19
  @name = name
23
20
  @difficulty = difficulty
21
+ @all_attempts = difficulty_option[:attempts]
22
+ @all_hints = difficulty_option[:hints]
24
23
  @attempts_left = difficulty_option[:attempts]
25
24
  @hints_left = difficulty_option[:hints]
26
25
  end
@@ -33,7 +32,7 @@ module Codebreaker
33
32
  end
34
33
 
35
34
  def hints_left?
36
- @hints_left.positive?
35
+ hints_left.positive?
37
36
  end
38
37
 
39
38
  def input_operation(input_code)
@@ -41,57 +40,51 @@ module Codebreaker
41
40
  return unless attempts_left?
42
41
 
43
42
  @attempts_left -= 1
44
- check_input
43
+ check_input(@code, @input_code)
45
44
  end
46
45
 
47
46
  def attempts_left?
48
- @attempts_left.positive?
47
+ attempts_left.positive?
49
48
  end
50
49
 
51
50
  def win?
52
- @input_code == @code
51
+ input_code == code
53
52
  end
54
53
 
55
- def save
56
- Codebreaker::Loader.save(to_h, 'stats')
54
+ def save(filename = 'stats')
55
+ Codebreaker::Loader.save(to_h, filename)
57
56
  end
58
57
 
59
58
  private
60
59
 
61
- def check_input(code = @code.chars)
62
- input = @input_code.chars
63
- minuses = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
64
- result = @minuse * minuses
65
- input.each.with_index do |element, index|
66
- result.sub!(@minuse, @plus) if element == code[index]
67
- end
68
- return result unless result.empty?
69
-
70
- @none
71
- end
72
-
73
60
  def generate_code
74
61
  Array.new(CODE_LENGTH) { rand(CODE_RANGE) }.join
75
62
  end
76
63
 
77
64
  def generate_hint
78
- hint_code = @code.chars.shuffle
79
- hint_code.pop
65
+ @hints_code.pop
80
66
  end
81
67
 
82
68
  def difficulty_option
83
69
  @difficulties[@difficulty]
84
70
  end
85
71
 
72
+ def check_input(code, input)
73
+ guess_checker = Codebreaker::GuessChecker.new(code, input)
74
+ guess_checker.symbols
75
+ guess_checker.check_input
76
+ end
77
+
86
78
  def to_h
87
79
  {
88
80
  name: @name,
89
81
  difficulty: @difficulty,
90
- attempts: difficulty_option[:attempts],
91
- hints: difficulty_option[:hints],
82
+ attempts: @all_attempts,
83
+ hints: @all_hints,
92
84
  code: @code,
93
- used_attempts: difficulty_option[:attempts] - @attempts_left,
94
- used_hints: difficulty_option[:hints] - @hints_left
85
+ used_attempts: @all_attempts - @attempts_left,
86
+ used_hints: @all_hints - @hints_left,
87
+ date: Time.now
95
88
  }
96
89
  end
97
90
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class GuessChecker
5
+ attr_accessor :inexact, :exact, :none
6
+
7
+ MINUSE = '-'
8
+ PLUS = '+'
9
+ NONE = ' '
10
+
11
+ def initialize(code, code_input)
12
+ @code = code.chars
13
+ @code_input = code_input.chars
14
+ end
15
+
16
+ def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
17
+ @inexact = inexact
18
+ @exact = exact
19
+ @none = none
20
+ end
21
+
22
+ def check_input
23
+ raw_result = mismatched
24
+ matched(raw_result)
25
+ end
26
+
27
+ private
28
+
29
+ def mismatched
30
+ inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
31
+ @inexact * inexact
32
+ end
33
+
34
+ def matched(result)
35
+ @code_input.each.with_index do |element, index|
36
+ result.sub!(@inexact, @exact) if element == @code[index]
37
+ end
38
+ return result unless result.empty?
39
+
40
+ @none
41
+ end
42
+ end
43
+ end
@@ -2,10 +2,12 @@
2
2
 
3
3
  module Codebreaker
4
4
  module Loader
5
- PATH = File.join(File.dirname(__FILE__), 'data/').freeze
5
+ # PATH = File.expand_path("../lib/data/#{file_name}", __FILE__)
6
+ # PATH = File.join(File.dirname(__FILE__), 'data/').freeze
6
7
  EXTENCTION = '.yml'
7
8
  def self.load(file_name)
8
- file_name = PATH + file_name + EXTENCTION.to_s
9
+ path = File.expand_path("./data/#{file_name}", __dir__)
10
+ file_name = path + EXTENCTION.to_s
9
11
  if File.exist?(file_name)
10
12
  YAML.load_file(file_name)
11
13
  else
@@ -15,7 +17,8 @@ module Codebreaker
15
17
  end
16
18
 
17
19
  def self.save(obj, file_name)
18
- file_name = PATH + file_name + EXTENCTION.to_s
20
+ path = File.expand_path("./data/#{file_name}", __dir__)
21
+ file_name = path + EXTENCTION.to_s
19
22
  stats = File.file?(file_name) && !File.zero?(file_name) ? YAML.load_file(file_name) : []
20
23
  stats << obj
21
24
  file = File.open(file_name, 'w')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- VERSION = '0.2.1'
4
+ VERSION = '0.2.6'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_kub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - katia kub
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-16 00:00:00.000000000 Z
11
+ date: 2020-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fasterer
@@ -120,6 +120,7 @@ files:
120
120
  - lib/codebreaker/data/difficulties.yml
121
121
  - lib/codebreaker/data/locales/en.yml
122
122
  - lib/codebreaker/game.rb
123
+ - lib/codebreaker/guess_checker.rb
123
124
  - lib/codebreaker/loader.rb
124
125
  - lib/codebreaker/output.rb
125
126
  - lib/codebreaker/validation.rb