codebreaker_kub 0.2.4 → 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: 9b0615cbf8d40317a2e5352dab95e976cdd1e573520751ed35fcda4d231ce983
4
- data.tar.gz: bbccf67ba8e10d137208430b08402099781b77ed0556230993b8d54393ed5628
3
+ metadata.gz: d3999defb3a7f8d534184fbe210075d9a2e7db373c9f3f03ee274eac2386b61c
4
+ data.tar.gz: f35e7d26ae5e52db50ed64164d1226cb10c2aabcb1f153ae7e73d8c35f959c30
5
5
  SHA512:
6
- metadata.gz: 4cd86ab7a4a57953649870cb06af1acd31981fe5f5969b376a30766a67fe1abf5286b228b4acc148ebbd2608e96f54f9c468fb40934ebcf5f990a116f43f9242
7
- data.tar.gz: 6395a16dc75d491deb9bc770c12c074a1bb45a6b2070a3b2ee5c766e17c25359d42160a4527e7cbdb200e0c395de808bcfc337f9fbd4e18cf20c53c080fc19d9
6
+ metadata.gz: 5e07b1939bd46e0a0244dcc92f14ff9de0113b8becf263e5b337f613a7c4c6104d99282723baca44266d5da04764399e1375106561deca721e8531eb9d0f156f
7
+ data.tar.gz: b6c04918739abc7d95e8a8426df36eb858d9e8befac9dce84fff4fc84aba703d7dfb0c463aebd7688e0d5d0f4f66173ba5bc57e0ab1b45c90954507884e35949
@@ -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.2.3)
4
+ codebreaker_kub (0.2.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -2,9 +2,8 @@
2
2
 
3
3
  module Codebreaker
4
4
  class Game
5
- include Codebreaker::GuessChecker
6
-
7
- attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
5
+ attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left, :all_hints,
6
+ :all_attempts
8
7
  attr_reader :hints_code
9
8
 
10
9
  CODE_LENGTH = 4
@@ -14,12 +13,13 @@ module Codebreaker
14
13
  @difficulties = Codebreaker::Loader.load('difficulties')
15
14
  @code = generate_code
16
15
  @hints_code = @code.chars.shuffle
17
- symbols
18
16
  end
19
17
 
20
18
  def game_option(name, difficulty)
21
19
  @name = name
22
20
  @difficulty = difficulty
21
+ @all_attempts = difficulty_option[:attempts]
22
+ @all_hints = difficulty_option[:hints]
23
23
  @attempts_left = difficulty_option[:attempts]
24
24
  @hints_left = difficulty_option[:hints]
25
25
  end
@@ -40,7 +40,7 @@ module Codebreaker
40
40
  return unless attempts_left?
41
41
 
42
42
  @attempts_left -= 1
43
- check_input(@code, input_code)
43
+ check_input(@code, @input_code)
44
44
  end
45
45
 
46
46
  def attempts_left?
@@ -51,8 +51,8 @@ module Codebreaker
51
51
  input_code == code
52
52
  end
53
53
 
54
- def save
55
- Codebreaker::Loader.save(to_h, 'stats')
54
+ def save(filename = 'stats')
55
+ Codebreaker::Loader.save(to_h, filename)
56
56
  end
57
57
 
58
58
  private
@@ -69,15 +69,22 @@ module Codebreaker
69
69
  @difficulties[@difficulty]
70
70
  end
71
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
+
72
78
  def to_h
73
79
  {
74
80
  name: @name,
75
81
  difficulty: @difficulty,
76
- attempts: difficulty_option[:attempts],
77
- hints: difficulty_option[:hints],
82
+ attempts: @all_attempts,
83
+ hints: @all_hints,
78
84
  code: @code,
79
- used_attempts: difficulty_option[:attempts] - @attempts_left,
80
- 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
81
88
  }
82
89
  end
83
90
  end
@@ -1,10 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- module GuessChecker
4
+ class GuessChecker
5
+ attr_accessor :inexact, :exact, :none
6
+
5
7
  MINUSE = '-'
6
8
  PLUS = '+'
7
- NONE = ''
9
+ NONE = ' '
10
+
11
+ def initialize(code, code_input)
12
+ @code = code.chars
13
+ @code_input = code_input.chars
14
+ end
8
15
 
9
16
  def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
10
17
  @inexact = inexact
@@ -12,19 +19,21 @@ module Codebreaker
12
19
  @none = none
13
20
  end
14
21
 
15
- def check_input(code, input)
16
- raw_result = inexact(code.chars, input.chars)
17
- exact(code.chars, input.chars, raw_result)
22
+ def check_input
23
+ raw_result = mismatched
24
+ matched(raw_result)
18
25
  end
19
26
 
20
- def inexact(code, input)
21
- inexact = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
27
+ private
28
+
29
+ def mismatched
30
+ inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
22
31
  @inexact * inexact
23
32
  end
24
33
 
25
- def exact(code, input, result)
26
- input.each.with_index do |element, index|
27
- result.sub!(@inexact, @exact) if element == code[index]
34
+ def matched(result)
35
+ @code_input.each.with_index do |element, index|
36
+ result.sub!(@inexact, @exact) if element == @code[index]
28
37
  end
29
38
  return result unless result.empty?
30
39
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Codebreaker
4
- VERSION = '0.2.4'
4
+ VERSION = '0.2.5'
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.4
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-17 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