codebreaker_kub 0.2.4 → 0.3.2

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: ff718dfdc1a8c199e5e84ce83a8f777dd7de62bf639655932ee42784db3f951a
4
+ data.tar.gz: 3ebb91ee444a884a3994c5433c3a8b75760f37b23e3cee1809ce30db4e3c0350
5
5
  SHA512:
6
- metadata.gz: 4cd86ab7a4a57953649870cb06af1acd31981fe5f5969b376a30766a67fe1abf5286b228b4acc148ebbd2608e96f54f9c468fb40934ebcf5f990a116f43f9242
7
- data.tar.gz: 6395a16dc75d491deb9bc770c12c074a1bb45a6b2070a3b2ee5c766e17c25359d42160a4527e7cbdb200e0c395de808bcfc337f9fbd4e18cf20c53c080fc19d9
6
+ metadata.gz: 15b525726886ca1803b888d9822113ff953ca5fb64719c3123ea82295b643f7339ee747199611eb876d72ac3df3a65a3b08aecac7cf09c395a9d40b31a381722
7
+ data.tar.gz: 07a069863674bbc923937666fe4998fd9fed51dfa99f47e117f3e69ef7239c38abfa728d62ab70ca22219299efbd092ace32a8759b43cfb7906bff2e679781ef
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .rvmrc
2
2
  /coverage/
3
3
  .rspec_status
4
+ stats.yml
@@ -5,8 +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
12
-
@@ -1,24 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- codebreaker_kub (0.2.3)
4
+ codebreaker_kub (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.1)
10
10
  colorize (0.8.1)
11
- concurrent-ruby (1.1.6)
11
+ concurrent-ruby (1.1.7)
12
12
  diff-lcs (1.4.4)
13
13
  docile (1.3.2)
14
14
  fasterer (0.8.3)
15
15
  colorize (~> 0.7)
16
16
  ruby_parser (>= 3.14.1)
17
- i18n (1.8.3)
17
+ i18n (1.8.5)
18
18
  concurrent-ruby (~> 1.0)
19
19
  parallel (1.19.2)
20
20
  parser (2.7.1.4)
21
21
  ast (~> 2.4.1)
22
+ rack (2.2.3)
23
+ rack-test (1.1.0)
24
+ rack (>= 1.0, < 3)
22
25
  rainbow (3.0.0)
23
26
  rake (12.3.3)
24
27
  regexp_parser (1.7.1)
@@ -36,17 +39,17 @@ GEM
36
39
  diff-lcs (>= 1.2.0, < 2.0)
37
40
  rspec-support (~> 3.9.0)
38
41
  rspec-support (3.9.3)
39
- rubocop (0.87.1)
42
+ rubocop (0.89.1)
40
43
  parallel (~> 1.10)
41
44
  parser (>= 2.7.1.1)
42
45
  rainbow (>= 2.2.2, < 4.0)
43
46
  regexp_parser (>= 1.7)
44
47
  rexml
45
- rubocop-ast (>= 0.1.0, < 1.0)
48
+ rubocop-ast (>= 0.3.0, < 1.0)
46
49
  ruby-progressbar (~> 1.7)
47
50
  unicode-display_width (>= 1.4.0, < 2.0)
48
- rubocop-ast (0.1.0)
49
- parser (>= 2.7.0.1)
51
+ rubocop-ast (0.3.0)
52
+ parser (>= 2.7.1.4)
50
53
  ruby-progressbar (1.10.1)
51
54
  ruby_parser (3.14.2)
52
55
  sexp_processor (~> 4.9)
@@ -61,9 +64,11 @@ PLATFORMS
61
64
  ruby
62
65
 
63
66
  DEPENDENCIES
67
+ bundler (~> 2.1.2)
64
68
  codebreaker_kub!
65
69
  fasterer (~> 0.8)
66
70
  i18n (~> 1.8)
71
+ rack-test
67
72
  rake (~> 12.0)
68
73
  rspec (~> 3.9)
69
74
  rubocop (~> 0.87)
@@ -28,8 +28,10 @@ Gem::Specification.new do |spec|
28
28
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ['lib']
30
30
 
31
+ spec.add_development_dependency 'bundler', '~>2.1.2'
31
32
  spec.add_development_dependency 'fasterer', '~>0.8'
32
33
  spec.add_development_dependency 'i18n', '~>1.8'
34
+ spec.add_development_dependency 'rack-test'
33
35
  spec.add_development_dependency 'rake', '~> 12.0'
34
36
  spec.add_development_dependency 'rspec', '~>3.9'
35
37
  spec.add_development_dependency 'rubocop', '~>0.87'
@@ -3,8 +3,5 @@
3
3
  require_relative 'codebreaker/guess_checker'
4
4
  require_relative 'codebreaker/game'
5
5
  require_relative 'codebreaker/loader'
6
- require_relative 'codebreaker/output'
7
- require_relative 'codebreaker/validation'
8
6
  require_relative 'codebreaker/version'
9
- require 'i18n'
10
7
  require 'yaml'
@@ -2,24 +2,25 @@
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
11
10
  CODE_RANGE = (1..6).freeze
12
11
 
13
- def initialize
14
- @difficulties = Codebreaker::Loader.load('difficulties')
12
+ def initialize(name, difficulty)
13
+ @difficulties = Loader.load('difficulties')
15
14
  @code = generate_code
16
15
  @hints_code = @code.chars.shuffle
17
- symbols
16
+ game_option(name, difficulty)
18
17
  end
19
18
 
20
19
  def game_option(name, difficulty)
21
20
  @name = name
22
- @difficulty = difficulty
21
+ @difficulty = difficulty.to_sym
22
+ @all_attempts = difficulty_option[:attempts]
23
+ @all_hints = difficulty_option[:hints]
23
24
  @attempts_left = difficulty_option[:attempts]
24
25
  @hints_left = difficulty_option[:hints]
25
26
  end
@@ -40,7 +41,7 @@ module Codebreaker
40
41
  return unless attempts_left?
41
42
 
42
43
  @attempts_left -= 1
43
- check_input(@code, input_code)
44
+ check_input(@code, @input_code)
44
45
  end
45
46
 
46
47
  def attempts_left?
@@ -51,8 +52,8 @@ module Codebreaker
51
52
  input_code == code
52
53
  end
53
54
 
54
- def save
55
- Codebreaker::Loader.save(to_h, 'stats')
55
+ def save(filename = 'stats')
56
+ Codebreaker::Loader.save(to_h, filename)
56
57
  end
57
58
 
58
59
  private
@@ -69,15 +70,22 @@ module Codebreaker
69
70
  @difficulties[@difficulty]
70
71
  end
71
72
 
73
+ def check_input(code, input)
74
+ guess_checker = Codebreaker::GuessChecker.new(code, input)
75
+ guess_checker.symbols
76
+ guess_checker.check_input
77
+ end
78
+
72
79
  def to_h
73
80
  {
74
81
  name: @name,
75
82
  difficulty: @difficulty,
76
- attempts: difficulty_option[:attempts],
77
- hints: difficulty_option[:hints],
83
+ attempts: @all_attempts,
84
+ hints: @all_hints,
78
85
  code: @code,
79
- used_attempts: difficulty_option[:attempts] - @attempts_left,
80
- used_hints: difficulty_option[:hints] - @hints_left
86
+ used_attempts: @all_attempts - @attempts_left,
87
+ used_hints: @all_hints - @hints_left,
88
+ date: Time.now
81
89
  }
82
90
  end
83
91
  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,23 +19,32 @@ 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_result = matched(raw_result)
25
+ check_result(matched_result)
18
26
  end
19
27
 
20
- def inexact(code, input)
21
- inexact = (code & input).map { |element| [code.count(element), input.count(element)].min }.sum
28
+ private
29
+
30
+ def mismatched
31
+ inexact = (@code & @code_input).map { |element| [@code.count(element), @code_input.count(element)].min }.sum
22
32
  @inexact * inexact
23
33
  end
24
34
 
25
- def exact(code, input, result)
26
- input.each.with_index do |element, index|
27
- result.sub!(@inexact, @exact) if element == code[index]
35
+ def matched(result)
36
+ @code_input.each.with_index do |element, index|
37
+ result.sub!(@inexact, @exact) if element == @code[index]
28
38
  end
29
39
  return result unless result.empty?
30
40
 
31
41
  @none
32
42
  end
43
+
44
+ def check_result(result)
45
+ result = result.chars
46
+ result = result << @none until result.size == 4
47
+ result.join
48
+ end
33
49
  end
34
50
  end
@@ -2,10 +2,9 @@
2
2
 
3
3
  module Codebreaker
4
4
  module Loader
5
- PATH = File.join(File.dirname(__FILE__), 'data/').freeze
6
5
  EXTENCTION = '.yml'
7
6
  def self.load(file_name)
8
- file_name = PATH + file_name + EXTENCTION.to_s
7
+ file_name = File.expand_path(file_name) + EXTENCTION.to_s
9
8
  if File.exist?(file_name)
10
9
  YAML.load_file(file_name)
11
10
  else
@@ -15,7 +14,7 @@ module Codebreaker
15
14
  end
16
15
 
17
16
  def self.save(obj, file_name)
18
- file_name = PATH + file_name + EXTENCTION.to_s
17
+ file_name = File.expand_path(file_name) + EXTENCTION.to_s
19
18
  stats = File.file?(file_name) && !File.zero?(file_name) ? YAML.load_file(file_name) : []
20
19
  stats << obj
21
20
  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.4'
4
+ VERSION = '0.3.2'
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
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.3.2
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-18 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: fasterer
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rake
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -116,16 +144,15 @@ files:
116
144
  - bin/console
117
145
  - bin/setup
118
146
  - codebreaker.gemspec
147
+ - difficulties.yml
119
148
  - lib/codebreaker.rb
120
- - lib/codebreaker/data/difficulties.yml
121
- - lib/codebreaker/data/locales/en.yml
122
149
  - lib/codebreaker/game.rb
123
150
  - lib/codebreaker/guess_checker.rb
124
151
  - lib/codebreaker/loader.rb
125
- - lib/codebreaker/output.rb
126
- - lib/codebreaker/validation.rb
127
152
  - lib/codebreaker/version.rb
128
153
  - pkg/codebreaker_kub-0.0.1.gem
154
+ - pkg/codebreaker_kub-0.3.0.gem
155
+ - pkg/codebreaker_kub-0.3.1.gem
129
156
  homepage: https://github.com/katiakub/gCodebreaker
130
157
  licenses:
131
158
  - MIT
@@ -1,29 +0,0 @@
1
- :en:
2
- hey: "Welcome to gCodebreaker!\n You can:
3
- - Start new game - puts 'start'\n
4
- - Check the rules - puts 'rules'\n
5
- - Load game statistics - puts 'stats'\n
6
- - Exit from game - puts 'exit'\n"
7
- goodbye: "Bye bye!\n"
8
- available_commands: "Enter 'start', 'rules', 'stats' or 'exit'"
9
- rules: "GAME RULES:\n
10
- # Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The\n codemaker, which will be played by the application we’re going to write, creates a secret code.\n
11
- # The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the\n codebreaker makes a guess. The codemaker then marks the guess with special symbols.\n
12
- # If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.\n
13
- # Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint - he receives back a\n
14
- separate digit of the secret code.\n
15
- # Good luck!"
16
- choose_name: "Enter you name: "
17
- wrong_name: "Enter valid name, please! Name must be min 3 and max 20 characters.\n"
18
- choose_difficulty: "Choose difficulty: 'easy', 'medium', 'hell'"
19
- wrong_difficulty: "There is no such level. Select from available, please"
20
- get_hint: "You can choose get hint - puts 'hint'"
21
- no_hints: "You have no hints"
22
- enter_guess: "Enter your guess, please"
23
- wrong_input: "Your input is invalid or there is no such command."
24
- win: "YOU WIN!"
25
- lose: "You lose, sorry("
26
- code: "The code was: "
27
- no_stats: "Sorry, you have not any statistic yet"
28
- save_result: "Do you want to save result y/n"
29
-
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreaker
4
- class Output
5
- attr_reader :stats
6
- def initialize
7
- I18n.load_path << Dir[File.expand_path(File.join(File.dirname(__FILE__), 'data/locales/')) + '/*.yml']
8
- I18n.config.available_locales = :en
9
- @stats = Codebreaker::Loader.load('stats')
10
- end
11
-
12
- def greeting
13
- puts I18n.t(:hey)
14
- end
15
-
16
- def available_commands
17
- puts I18n.t(:available_commands)
18
- end
19
-
20
- def choose_name
21
- puts I18n.t(:choose_name)
22
- end
23
-
24
- def wrong_name
25
- puts I18n.t(:wrong_name)
26
- end
27
-
28
- def rules
29
- puts I18n.t(:rules)
30
- end
31
-
32
- def sort_stats
33
- @stats = @stats.sort_by do |game|
34
- [game[:attempts], game[:used_attempts],
35
- game[:used_hints]]
36
- end
37
- end
38
-
39
- def show_stats
40
- sort_stats
41
- @stats.each_with_index do |game, index|
42
- puts "Hey, #{game[:name]}
43
- Rating: #{index + 1}
44
- Attempts: #{game[:attempts]}
45
- Hints: #{game[:hints]}
46
- Chosen difficulty: #{game[:difficulty]}
47
- Used attempts: #{game[:used_attempts]}
48
- Used hints: #{game[:used_hints]}"
49
- end
50
- end
51
-
52
- def no_stats
53
- puts I18n.t(:no_stats)
54
- end
55
-
56
- def show_hint
57
- puts I18n.t(:get_hint)
58
- end
59
-
60
- def no_hints
61
- puts I18n.t(:no_hints)
62
- end
63
-
64
- def choose_difficulty
65
- puts I18n.t(:choose_difficulty)
66
- end
67
-
68
- def wrong_difficulty
69
- puts I18n.t(:wrong_difficulty)
70
- end
71
-
72
- def enter_guess
73
- puts I18n.t(:enter_guess)
74
- end
75
-
76
- def wrong_input
77
- puts I18n.t(:wrong_input)
78
- end
79
-
80
- def win
81
- puts I18n.t(:win)
82
- end
83
-
84
- def lose
85
- puts I18n.t(:lose)
86
- puts I18n.t(:code)
87
- end
88
-
89
- def save_result
90
- puts I18n.t(:save_result)
91
- end
92
-
93
- def goodbye
94
- puts I18n.t(:goodbye)
95
- end
96
- end
97
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Codebreaker
4
- module Validation
5
- MIN_NAME_LENGTH = 3
6
- MAX_NAME_LENGTH = 20
7
- def name_is_valid?(name)
8
- name.instance_of?(String) && name.length.between?(MIN_NAME_LENGTH, MAX_NAME_LENGTH)
9
- end
10
-
11
- def input_is_valid?(input)
12
- /^[1-6]{4}$/.match?(input)
13
- end
14
- end
15
- end