codebracker_simb 0.2.4 → 0.9.0

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: ec36a3431299f8c34da1a786c40837dfc1d63e73a43244f07c67673fa0aa1157
4
- data.tar.gz: af4ede3b02cbeaa9146cc71873a2d99637a6ac1d70ef3bbdb0c1cc197ded7553
3
+ metadata.gz: 5435653ed5020dc12fce5f812025ca52ab4aa068d9835962d65ec735897d2b4f
4
+ data.tar.gz: fe29d8332ca574cc900eb6f02e41408e1c03cf5e118bfbc072570733759740da
5
5
  SHA512:
6
- metadata.gz: ff65838b01566b939ddf487a22de604e676eb77422a2ba0e4af4fe23954f1502e77530a6466f9c01c021afab2abdbb06345ca1ce748098863ebb00bfd9538582
7
- data.tar.gz: 4a5e655e1b7f755d631088cfcc91cd280456cdbf615416afdaff7e61166c8acef6b426acd64c1e37b08db7a1a0d18facefed59ae62f2323f73bd95e7f6cd9613
6
+ metadata.gz: 322b9b991f70fb787cddde04d7192aefc91341c169669196dcf01c84fe728feb35444566faeaaca05342824da2f7111ec372196bf75657b071c4b2288526b271
7
+ data.tar.gz: 25a6c510d2a31f5e9a752b30748feeb0a386beb8e2b5eacb5d7e7b6581ba88eeccb1c11981f4ce3178c4b070dd48caef33b55d5a5b22214c3e1f5b4aa559b0f7
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
@@ -1,25 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/codebracker_simb/version"
3
+ require_relative 'lib/codebracker_simb/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "codebracker_simb"
6
+ spec.name = 'codebracker_simb'
7
7
  spec.version = CodebrackerSimb::VERSION
8
- spec.authors = ["max"]
9
- spec.email = ["laterty@gmail.com"]
8
+ spec.authors = ['max']
9
+ spec.email = ['laterty@gmail.com']
10
10
 
11
- spec.summary = "Write a short summary, because RubyGems requires one."
12
- spec.description = "Write a longer description or delete this line."
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 2.6.0"
11
+ spec.summary = 'Write a short summary, because RubyGems requires one.'
12
+ spec.description = 'Write a longer description or delete this line.'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 3.0.0'
15
15
 
16
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
17
17
 
18
18
  # Specify which files should be added to the gem when it is released.
19
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
- spec.files = Dir['README.md', 'LICENSE', 'lib/**/*.rb', 'lib/**/*.rake', 'Gemfile', 'Rakefile', 'codebracker_simb.gemspec', '.github/*.md', 'CHANGELOG.md']
21
- spec.bindir = "exe"
20
+ spec.files = Dir['README.md', 'LICENSE', 'lib/**/*.rb', 'lib/**/*.rake', 'Gemfile', 'Rakefile',
21
+ 'codebracker_simb.gemspec', '.github/*.md', 'CHANGELOG.md']
22
+ spec.bindir = 'exe'
22
23
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
23
- spec.require_paths = ["lib"]
24
+ spec.require_paths = ['lib']
24
25
  spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'fasterer'
27
+ spec.add_development_dependency 'rubocop-rspec'
28
+ spec.add_development_dependency 'simplecov'
29
+ spec.add_runtime_dependency 'sqlite3'
25
30
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CodebrackerSimb
4
+ # builds table of equality for answer and code and gives string result
4
5
  class Checker < Array
5
6
  def initialize(code, ans)
6
7
  @ans = ans
@@ -29,6 +30,7 @@ module CodebrackerSimb
29
30
  end
30
31
  output.sort.join('')
31
32
  end
33
+
32
34
  def fill_with_plus
33
35
  each_with_index do |line, i|
34
36
  if @code[i] == @ans[i]
@@ -41,11 +43,11 @@ module CodebrackerSimb
41
43
  def fill_with_minus
42
44
  each_with_index do |line, i|
43
45
  line.each_with_index do |_el, j|
44
- if @code[i] == @ans[j] && !line[j] && !@filled_column.include?(j)
45
- next if line.include? :minus
46
- line[j] = :minus
47
- @filled_column << j
48
- end
46
+ next unless @code[i] == @ans[j] && !line[j] && !@filled_column.include?(j)
47
+ next if line.include? :minus
48
+
49
+ line[j] = :minus
50
+ @filled_column << j
49
51
  end
50
52
  end
51
53
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # error when answer isn't valid
4
+ class CodeError < StandardError
5
+ def initialize(msg = "\nCode must include only four digits from 1 to 6\n\n")
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # error when user inputs unexpected complexity
4
+ class ComplexityError < StandardError
5
+ def initialize(msg = "\nIt could be easy, medium or hard!\n\n")
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # error when user inputs unexpected command
4
+ class IntroError < StandardError
5
+ def initialize(msg = "\nIt could be start, rules, stats or exit!\n\n")
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # error when name isn't valid
4
+ class NameError < StandardError
5
+ def initialize(msg = "\nName should be longer than 3 chars and shorter than 20 chars!\n\n")
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # error when hints are over
4
+ class NoHintsError < StandardError
5
+ def initialize(msg = "\nYou used all your hints!\n\n")
6
+ super
7
+ end
8
+ end
@@ -3,23 +3,36 @@
3
3
  require_relative './code'
4
4
  require_relative './checker'
5
5
  require 'rspec'
6
+ require 'pry'
7
+ require 'sqlite3'
8
+
6
9
  module CodebrackerSimb
7
- # game cl
10
+ # game cl
8
11
  class Game
9
- attr_reader :code
10
- attr_reader :attempts
11
- attr_reader :player
12
- attr_accessor :answer
12
+ attr_reader :code, :attempts, :player, :complexity
13
+ attr_accessor :answer, :hint_positions, :hints
13
14
 
14
- ERR_UNEXPECTED_COMPLEXITY = 'complexity could be easy, medium or hard'
15
15
  IS_OVER = 'Game is over, you lose'
16
16
  GREETING = 'Hello! Welcome to CodeBracker Game! Have fun!'
17
17
  CONGRATULATIONS = 'Congratulations! You broked a code, master!'
18
18
 
19
+ class << self
20
+ def scores
21
+ db.execute 'select * from scores order by attempts_total asc, attempts_used asc, hints_used asc'
22
+ end
23
+
24
+ def db
25
+ SQLite3::Database.new 'score.db'
26
+ end
27
+ end
28
+
19
29
  def initialize(complexity, player)
20
- @attempts = set_options(complexity)[:attempts]
21
- @hints = set_options(complexity)[:hints]
30
+ @attempts = game_options(complexity)[:attempts]
31
+ @hints = game_options(complexity)[:hints]
22
32
  @player = player
33
+ @hint_positions = []
34
+ @complexity = complexity
35
+ define_code
23
36
  end
24
37
 
25
38
  def input_answer(input)
@@ -27,7 +40,7 @@ module CodebrackerSimb
27
40
  end
28
41
 
29
42
  def check_answer
30
- Checker.new(code, answer).compare
43
+ Checker.new(@code, @answer).compare
31
44
  end
32
45
 
33
46
  def define_code
@@ -35,30 +48,56 @@ module CodebrackerSimb
35
48
  end
36
49
 
37
50
  def end_with_win?
38
- code == answer
51
+ code == answer
39
52
  end
40
53
 
41
54
  def refresh_attempts_quantity
42
- if @attempts > 0
55
+ if @attempts.positive?
43
56
  @attempts -= 1
44
- message = "#{@attempts} left"
57
+ "#{@attempts} left"
58
+ end
59
+ end
60
+
61
+ def hint
62
+ pos = nil
63
+ loop do
64
+ pos = rand(0..3)
65
+ break unless hint_positions.include? pos
66
+ end
67
+ if @hints.positive?
68
+ hint_positions << pos
69
+ @hints -= 1
70
+ code[pos]
71
+ else
72
+ raise NoHintsError
45
73
  end
46
74
  end
47
-
75
+
76
+ def write_to_db
77
+ db = Game.db
78
+ db.execute 'insert into scores (name, difficulty, attempts_total, attempts_used, hints_total, hints_used) values (?,?,?,?,?,?)',
79
+ params
80
+ end
81
+
82
+ def params
83
+ total_attempts = game_options(complexity)[:attempts]
84
+ total_hints = game_options(complexity)[:hints]
85
+ used_hints = total_hints - hints
86
+ used_attempts = total_attempts - attempts
87
+ [player, complexity, total_attempts, used_attempts, total_hints, used_hints]
88
+ end
89
+
48
90
  private
49
91
 
50
- def set_options(complexity)
92
+ def game_options(complexity)
51
93
  case complexity
52
94
  when 'easy'
53
- {attempts: 14, hints: 2}
95
+ { attempts: 14, hints: 2 }
54
96
  when 'medium'
55
- {attempts: 10, hints: 1}
97
+ { attempts: 10, hints: 1 }
56
98
  when 'hard'
57
- {attempts: 5, hints: 1}
58
- else
59
- raise ERR_UNEXPECTED_COMPLEXITY
99
+ { attempts: 5, hints: 1 }
60
100
  end
61
101
  end
62
102
  end
63
103
  end
64
-
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebrackerSimb
4
+ RULES = '- Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker.'\
5
+ 'The codemaker, which will be played by the application we’re going to write, creates a secret code of four'\
6
+ 'numbers between 1 and 6.
7
+ - The codebreaker gets some number of chances to break the code (depends on chosen difficulty).'\
8
+ 'In each turn, the codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with'\
9
+ 'up to 4 signs - + or - or empty spaces.
10
+ -A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the'\
11
+ 'secret'\
12
+ 'code and in the same position.'\
13
+ 'For example:
14
+ Secret number - 1234
15
+ Input number - 6264
16
+ Number of pluses - 2 (second and fourth position)
17
+ - A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the'\
18
+ 'secret code but in a different position. For example:
19
+ Secret number - 1234
20
+ Input number - 6462
21
+ Number of minuses - 2 (second and fourth position)
22
+ - An empty space indicates that there is not a current digit in a secret number.
23
+ - If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are'\
24
+ 'spent - codebreaker loses.
25
+ - Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint'\
26
+ '- he receives back a separate digit of the secret code.'
27
+
28
+ INSTRUCTION = "Enter one of the following command:
29
+ - start (start a game)
30
+ - rules (to read the rules of game
31
+ - stats (to see the statistics)
32
+ - exit (but why?)"
33
+ end
@@ -1,13 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CodebrackerSimb
4
+ # validator class
2
5
  class Validator
3
-
4
6
  def self.valid_name?(name)
5
- name.is_a?(String) && name.length > 3 && name.length < 20
7
+ name.is_a?(String) && name.length > 3 && name.length < 20
6
8
  end
7
9
 
8
10
  def self.valid_code?(code)
9
11
  code =~ /^[1-6]{4}$/
10
12
  end
11
-
12
13
  end
13
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CodebrackerSimb
4
- VERSION = "0.2.4"
4
+ VERSION = '0.9.0'
5
5
  end
@@ -3,7 +3,13 @@
3
3
  require_relative 'codebracker_simb/version'
4
4
  require_relative 'codebracker_simb/game'
5
5
  require_relative 'codebracker_simb/validator'
6
+ require_relative 'codebracker_simb/errors/no_hints_error'
7
+ require_relative 'codebracker_simb/errors/code_error'
8
+ require_relative 'codebracker_simb/errors/name_error'
9
+ require_relative 'codebracker_simb/errors/complexity_error'
10
+ require_relative 'codebracker_simb/errors/intro_error'
11
+ require_relative 'codebracker_simb/text'
6
12
 
13
+ # gem module
7
14
  module CodebrackerSimb
8
-
9
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebracker_simb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - max
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-29 00:00:00.000000000 Z
11
+ date: 2022-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -24,6 +24,62 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fasterer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
27
83
  description: Write a longer description or delete this line.
28
84
  email:
29
85
  - laterty@gmail.com
@@ -36,7 +92,13 @@ files:
36
92
  - lib/codebracker_simb.rb
37
93
  - lib/codebracker_simb/checker.rb
38
94
  - lib/codebracker_simb/code.rb
95
+ - lib/codebracker_simb/errors/code_error.rb
96
+ - lib/codebracker_simb/errors/complexity_error.rb
97
+ - lib/codebracker_simb/errors/intro_error.rb
98
+ - lib/codebracker_simb/errors/name_error.rb
99
+ - lib/codebracker_simb/errors/no_hints_error.rb
39
100
  - lib/codebracker_simb/game.rb
101
+ - lib/codebracker_simb/text.rb
40
102
  - lib/codebracker_simb/validator.rb
41
103
  - lib/codebracker_simb/version.rb
42
104
  homepage:
@@ -52,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
114
  requirements:
53
115
  - - ">="
54
116
  - !ruby/object:Gem::Version
55
- version: 2.6.0
117
+ version: 3.0.0
56
118
  required_rubygems_version: !ruby/object:Gem::Requirement
57
119
  requirements:
58
120
  - - ">="