codebreaker_kub 0.1.8 → 0.2.4

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: 4ca339901e4380146ff8f4454ad542b513fd422331f53f9ddd8a14efdb2e72c5
4
- data.tar.gz: 7b349065f626cdc2b54fcebfbc6601ea0d66a4d66fe42309b2d7cc61d7e02783
3
+ metadata.gz: 9b0615cbf8d40317a2e5352dab95e976cdd1e573520751ed35fcda4d231ce983
4
+ data.tar.gz: bbccf67ba8e10d137208430b08402099781b77ed0556230993b8d54393ed5628
5
5
  SHA512:
6
- metadata.gz: 197df5af39c7c1e49b9d689f0042cd820a42c7ab9fae406331a9b17dc2f07ff07738700349c85053165f0add09262f630f4103416bd7c1e16509604888aa1b28
7
- data.tar.gz: 96974c45577b6193a2e0d86e217e95ee0869cb1a821883d1a9fa038ba004ff9eaf4e4072a13e3cc44c159f54ee7616070e7a0019cd17659c9d9dc6b6debc7da8
6
+ metadata.gz: 4cd86ab7a4a57953649870cb06af1acd31981fe5f5969b376a30766a67fe1abf5286b228b4acc148ebbd2608e96f54f9c468fb40934ebcf5f990a116f43f9242
7
+ data.tar.gz: 6395a16dc75d491deb9bc770c12c074a1bb45a6b2070a3b2ee5c766e17c25359d42160a4527e7cbdb200e0c395de808bcfc337f9fbd4e18cf20c53c080fc19d9
@@ -7,9 +7,6 @@ Style/Documentation:
7
7
  Layout/LineLength:
8
8
  Max: 110
9
9
 
10
- Style/FrozenStringLiteralComment:
11
- Enabled: false
12
-
13
10
  Metrics/BlockLength:
14
11
  Enabled: false
15
12
 
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.7)
4
+ codebreaker_kub (0.2.3)
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'
@@ -26,5 +26,4 @@
26
26
  code: "The code was: "
27
27
  no_stats: "Sorry, you have not any statistic yet"
28
28
  save_result: "Do you want to save result y/n"
29
- game_over: "GAME OVER"
30
29
 
@@ -1,19 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  class Game
5
+ include Codebreaker::GuessChecker
6
+
3
7
  attr_accessor :input_code, :code, :name, :difficulties, :difficulty, :hints_left, :attempts_left
4
- attr_reader :minuse, :plus, :none
8
+ attr_reader :hints_code
9
+
10
+ CODE_LENGTH = 4
11
+ CODE_RANGE = (1..6).freeze
12
+
5
13
  def initialize
6
14
  @difficulties = Codebreaker::Loader.load('difficulties')
7
15
  @code = generate_code
16
+ @hints_code = @code.chars.shuffle
8
17
  symbols
9
18
  end
10
19
 
11
- def symbols(minuse = '-', plus = '+', none = '')
12
- @minuse = minuse
13
- @plus = plus
14
- @none = none
15
- end
16
-
17
20
  def game_option(name, difficulty)
18
21
  @name = name
19
22
  @difficulty = difficulty
@@ -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,41 +40,29 @@ 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
54
  def save
52
- Codebreaker::Loader.save(to_h, 'stat')
55
+ Codebreaker::Loader.save(to_h, 'stats')
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
@@ -86,8 +77,7 @@ module Codebreaker
86
77
  hints: difficulty_option[:hints],
87
78
  code: @code,
88
79
  used_attempts: difficulty_option[:attempts] - @attempts_left,
89
- used_hints: difficulty_option[:hints] - @hints_left,
90
- win: win?
80
+ used_hints: difficulty_option[:hints] - @hints_left
91
81
  }
92
82
  end
93
83
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ module GuessChecker
5
+ MINUSE = '-'
6
+ PLUS = '+'
7
+ NONE = ''
8
+
9
+ def symbols(inexact = MINUSE, exact = PLUS, none = NONE)
10
+ @inexact = inexact
11
+ @exact = exact
12
+ @none = none
13
+ end
14
+
15
+ def check_input(code, input)
16
+ raw_result = inexact(code.chars, input.chars)
17
+ exact(code.chars, input.chars, raw_result)
18
+ end
19
+
20
+ def inexact(code, input)
21
+ inexact = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
22
+ @inexact * inexact
23
+ end
24
+
25
+ def exact(code, input, result)
26
+ input.each.with_index do |element, index|
27
+ result.sub!(@inexact, @exact) if element == code[index]
28
+ end
29
+ return result unless result.empty?
30
+
31
+ @none
32
+ end
33
+ end
34
+ end
@@ -1,12 +1,17 @@
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
- raise LoadError, 'No such file to load' unless File.exist?(file_name)
8
-
9
- YAML.load_file(file_name)
9
+ if File.exist?(file_name)
10
+ YAML.load_file(file_name)
11
+ else
12
+ File.new(file_name, 'w')
13
+ []
14
+ end
10
15
  end
11
16
 
12
17
  def self.save(obj, file_name)
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Codebreaker
2
4
  class Output
3
5
  attr_reader :stats
4
6
  def initialize
5
7
  I18n.load_path << Dir[File.expand_path(File.join(File.dirname(__FILE__), 'data/locales/')) + '/*.yml']
6
8
  I18n.config.available_locales = :en
7
- @stats = Codebreaker::Loader.load('stat')
9
+ @stats = Codebreaker::Loader.load('stats')
8
10
  end
9
11
 
10
12
  def greeting
@@ -57,7 +59,6 @@ module Codebreaker
57
59
 
58
60
  def no_hints
59
61
  puts I18n.t(:no_hints)
60
- puts I18n.t(:code)
61
62
  end
62
63
 
63
64
  def choose_difficulty
@@ -82,6 +83,7 @@ module Codebreaker
82
83
 
83
84
  def lose
84
85
  puts I18n.t(:lose)
86
+ puts I18n.t(:code)
85
87
  end
86
88
 
87
89
  def save_result
@@ -91,9 +93,5 @@ module Codebreaker
91
93
  def goodbye
92
94
  puts I18n.t(:goodbye)
93
95
  end
94
-
95
- def game_over
96
- puts I18n.t(:game_over)
97
- end
98
96
  end
99
97
  end
@@ -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.8'.freeze
4
+ VERSION = '0.2.4'
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.8
4
+ version: 0.2.4
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-12 00:00:00.000000000 Z
11
+ date: 2020-07-17 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