codebreaker_kub 0.1.9 → 0.2.5

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: 785cfab5d2e074a0914b08aff16f83623f895eeaf4348bcc1e87c283b6b2d4da
4
- data.tar.gz: 52cf47e5ea7c5c5421efa0b00d118f64db300e9a77b4c2002aecaa99a4d46ea7
3
+ metadata.gz: d3999defb3a7f8d534184fbe210075d9a2e7db373c9f3f03ee274eac2386b61c
4
+ data.tar.gz: f35e7d26ae5e52db50ed64164d1226cb10c2aabcb1f153ae7e73d8c35f959c30
5
5
  SHA512:
6
- metadata.gz: b01b8c3ccf2b578470405c70511f7f9d816f48f16a690cb44af0d05e4231268701021f0488909738a7893a4efe2cdab271e2f5aa12d079f032c9b7512d375523
7
- data.tar.gz: 379731f286acf3d954d4c336acf7e1d72ba533102a548cbd7e7df66ff69bb100ec8363d9b5f722ff6c4fc1ec1dd26a8396adb776bb04bb7073cbb17a4585767a
6
+ metadata.gz: 5e07b1939bd46e0a0244dcc92f14ff9de0113b8becf263e5b337f613a7c4c6104d99282723baca44266d5da04764399e1375106561deca721e8531eb9d0f156f
7
+ data.tar.gz: b6c04918739abc7d95e8a8426df36eb858d9e8befac9dce84fff4fc84aba703d7dfb0c463aebd7688e0d5d0f4f66173ba5bc57e0ab1b45c90954507884e35949
@@ -5,10 +5,7 @@ Style/Documentation:
5
5
  Enabled: false
6
6
 
7
7
  Layout/LineLength:
8
- Max: 110
9
-
10
- Style/FrozenStringLiteralComment:
11
- Enabled: false
8
+ Max: 120
12
9
 
13
10
  Metrics/BlockLength:
14
11
  Enabled: false
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in codebreaker.gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_kub (0.1.8)
4
+ codebreaker_kub (0.2.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'codebreaker'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/codebreaker/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'codebreaker/guess_checker'
1
4
  require_relative 'codebreaker/game'
2
5
  require_relative 'codebreaker/loader'
3
6
  require_relative 'codebreaker/output'
@@ -1,22 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  class Game
3
- attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
4
- attr_reader :minuse, :plus, :none
5
+ attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
6
+ :all_attempts
7
+ attr_reader :hints_code
8
+
9
+ CODE_LENGTH = 4
10
+ CODE_RANGE = (1..6).freeze
11
+
5
12
  def initialize
6
13
  @difficulties = Codebreaker::Loader.load('difficulties')
7
14
  @code = generate_code
8
- symbols
9
- end
10
-
11
- def symbols(minuse = '-', plus = '+', none = '')
12
- @minuse = minuse
13
- @plus = plus
14
- @none = none
15
+ @hints_code = @code.chars.shuffle
15
16
  end
16
17
 
17
18
  def game_option(name, difficulty)
18
19
  @name = name
19
20
  @difficulty = difficulty
21
+ @all_attempts = difficulty_option[:attempts]
22
+ @all_hints = difficulty_option[:hints]
20
23
  @attempts_left = difficulty_option[:attempts]
21
24
  @hints_left = difficulty_option[:hints]
22
25
  end
@@ -29,7 +32,7 @@ module Codebreaker
29
32
  end
30
33
 
31
34
  def hints_left?
32
- @hints_left.positive?
35
+ hints_left.positive?
33
36
  end
34
37
 
35
38
  def input_operation(input_code)
@@ -37,57 +40,51 @@ module Codebreaker
37
40
  return unless attempts_left?
38
41
 
39
42
  @attempts_left -= 1
40
- check_input
43
+ check_input(@code, @input_code)
41
44
  end
42
45
 
43
46
  def attempts_left?
44
- @attempts_left.positive?
47
+ attempts_left.positive?
45
48
  end
46
49
 
47
50
  def win?
48
- @input_code == @code
51
+ input_code == code
49
52
  end
50
53
 
51
- def save
52
- Codebreaker::Loader.save(to_h, 'stats')
54
+ def save(filename = 'stats')
55
+ Codebreaker::Loader.save(to_h, filename)
53
56
  end
54
57
 
55
58
  private
56
59
 
57
- def check_input(code = @code.chars)
58
- input = @input_code.chars
59
- minuses = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
60
- result = @minuse * minuses
61
- input.each.with_index do |element, index|
62
- result.sub!(@minuse, @plus) if element == code[index]
63
- end
64
- return result unless result.empty?
65
-
66
- @none
67
- end
68
-
69
60
  def generate_code
70
- Array.new(4) { rand(1..6) }.join
61
+ Array.new(CODE_LENGTH) { rand(CODE_RANGE) }.join
71
62
  end
72
63
 
73
64
  def generate_hint
74
- @code.chars.shuffle.pop
65
+ @hints_code.pop
75
66
  end
76
67
 
77
68
  def difficulty_option
78
69
  @difficulties[@difficulty]
79
70
  end
80
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
+
81
78
  def to_h
82
79
  {
83
80
  name: @name,
84
81
  difficulty: @difficulty,
85
- attempts: difficulty_option[:attempts],
86
- hints: difficulty_option[:hints],
82
+ attempts: @all_attempts,
83
+ hints: @all_hints,
87
84
  code: @code,
88
- used_attempts: difficulty_option[:attempts] - @attempts_left,
89
- used_hints: difficulty_option[:hints] - @hints_left,
90
- win: win?
85
+ used_attempts: @all_attempts - @attempts_left,
86
+ used_hints: @all_hints - @hints_left,
87
+ date: Time.now
91
88
  }
92
89
  end
93
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
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  module Loader
3
5
  PATH = File.join(File.dirname(__FILE__), 'data/').freeze
4
- EXTENCTION = '.yml'.freeze
6
+ EXTENCTION = '.yml'
5
7
  def self.load(file_name)
6
8
  file_name = PATH + file_name + EXTENCTION.to_s
7
9
  if File.exist?(file_name)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  class Output
3
5
  attr_reader :stats
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  module Validation
5
+ MIN_NAME_LENGTH = 3
6
+ MAX_NAME_LENGTH = 20
3
7
  def name_is_valid?(name)
4
- name.instance_of?(String) && name.length.between?(3, 20)
8
+ name.instance_of?(String) && name.length.between?(MIN_NAME_LENGTH, MAX_NAME_LENGTH)
5
9
  end
6
10
 
7
11
  def input_is_valid?(input)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
- VERSION = '0.1.9'.freeze
4
+ VERSION = '0.2.5'
3
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.1.9
4
+ version: 0.2.5
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-13 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