codebracker_simb 0.2.4 → 0.9.2

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: 5fb48922b8d9ad8dde56070931894b9999355fa3da6b03f964df7aacfb41578a
4
+ data.tar.gz: 8a3c6f2e00c2bedcb37f021ff4a86e4518ec3e9674eb96e16f39eb062b1bada8
5
5
  SHA512:
6
- metadata.gz: ff65838b01566b939ddf487a22de604e676eb77422a2ba0e4af4fe23954f1502e77530a6466f9c01c021afab2abdbb06345ca1ce748098863ebb00bfd9538582
7
- data.tar.gz: 4a5e655e1b7f755d631088cfcc91cd280456cdbf615416afdaff7e61166c8acef6b426acd64c1e37b08db7a1a0d18facefed59ae62f2323f73bd95e7f6cd9613
6
+ metadata.gz: 68e47ec8fb79997e7be48e5c06425bdf484454fe1e2731fbbd0648976b16b58c4baae18d03996d56722ee09a79b3883382c20768bca389df3f3389314d335bdf
7
+ data.tar.gz: 7c446f5ec698df5c6c9483404ed6dbfde9e2fa0b0d7f4ed4fbb7857fb3b32a72734015202bf2688aa60c37ba9179742b8cafd80cc327110fa690bf67200b9b5c
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/**/**/*.rb', 'lib/codebracker_simb.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,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CodebrackerSimb
4
- class Checker < Array
5
- def initialize(code, ans)
6
- @ans = ans
4
+ # builds table of equality for answer and code and gives string result
5
+ class Checker
6
+ def initialize(code, answer)
7
+ @answer = answer
7
8
  @code = code
8
9
  @filled_column = []
9
- super([[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]])
10
+ @table = [[nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil], [nil, nil, nil, nil]]
10
11
  end
11
12
 
12
13
  def compare
@@ -23,29 +24,30 @@ module CodebrackerSimb
23
24
 
24
25
  def output
25
26
  output = []
26
- each do |line|
27
+ @table.each do |line|
27
28
  output << '+' if line.include?(:plus)
28
29
  output << '-' if line.include?(:minus) && !line.include?(:plus)
29
30
  end
30
31
  output.sort.join('')
31
32
  end
33
+
32
34
  def fill_with_plus
33
- each_with_index do |line, i|
34
- if @code[i] == @ans[i]
35
- line[i] = :plus
36
- @filled_column << i
35
+ @table.each_with_index do |line, index|
36
+ if @code[index] == @answer[index]
37
+ line[index] = :plus
38
+ @filled_column << index
37
39
  end
38
40
  end
39
41
  end
40
42
 
41
43
  def fill_with_minus
42
- each_with_index do |line, i|
43
- 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
44
+ @table.each_with_index do |line, index|
45
+ line.each_with_index do |_, jndex|
46
+ next unless @code[index] == @answer[jndex] && !line[jndex] && !@filled_column.include?(jndex)
47
+ next if line.include? :minus
48
+
49
+ line[jndex] = :minus
50
+ @filled_column << jndex
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
@@ -1,33 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './code'
4
3
  require_relative './checker'
5
4
  require 'rspec'
5
+ require 'pry'
6
+ require 'sqlite3'
7
+
6
8
  module CodebrackerSimb
7
- # game cl
9
+ # game cl
8
10
  class Game
9
- attr_reader :code
10
- attr_reader :attempts
11
- attr_reader :player
12
- attr_accessor :answer
11
+ attr_reader :code, :attempts, :player, :complexity
12
+ attr_accessor :answer, :hint_positions, :hints
13
13
 
14
- ERR_UNEXPECTED_COMPLEXITY = 'complexity could be easy, medium or hard'
15
14
  IS_OVER = 'Game is over, you lose'
16
15
  GREETING = 'Hello! Welcome to CodeBracker Game! Have fun!'
17
16
  CONGRATULATIONS = 'Congratulations! You broked a code, master!'
18
17
 
18
+ class << self
19
+ def scores
20
+ db.execute 'select * from scores order by attempts_total asc, attempts_used asc, hints_used asc'
21
+ end
22
+
23
+ def db
24
+ SQLite3::Database.new 'score.db'
25
+ end
26
+ end
27
+
19
28
  def initialize(complexity, player)
20
- @attempts = set_options(complexity)[:attempts]
21
- @hints = set_options(complexity)[:hints]
29
+ @attempts = game_options(complexity)[:attempts]
30
+ @hints = game_options(complexity)[:hints]
22
31
  @player = player
32
+ @hint_positions = []
33
+ @complexity = complexity
34
+ define_code
23
35
  end
24
36
 
25
37
  def input_answer(input)
26
- @answer = Code.new(input)
38
+ @answer = input.split('').map(&:to_i)
27
39
  end
28
40
 
29
41
  def check_answer
30
- Checker.new(code, answer).compare
42
+ refresh_attempts_quantity
43
+ Checker.new(@code, @answer).compare
31
44
  end
32
45
 
33
46
  def define_code
@@ -35,30 +48,55 @@ 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"
45
57
  end
46
58
  end
47
-
59
+
60
+ def hint
61
+ position = nil
62
+ loop do
63
+ position = rand(0..3)
64
+ break unless hint_positions.include? position
65
+ end
66
+ if @hints.positive?
67
+ hint_positions << position
68
+ @hints -= 1
69
+ code[position]
70
+ else
71
+ raise NoHintsError
72
+ end
73
+ end
74
+
75
+ def write_to_db
76
+ db = Game.db
77
+ db.execute 'insert into scores (name, difficulty, attempts_total, attempts_used, hints_total, hints_used) values (?,?,?,?,?,?)',
78
+ params
79
+ end
80
+
81
+ def params
82
+ total_attempts = game_options(complexity)[:attempts]
83
+ total_hints = game_options(complexity)[:hints]
84
+ used_hints = total_hints - hints
85
+ used_attempts = total_attempts - attempts
86
+ [player, complexity, total_attempts, used_attempts, total_hints, used_hints]
87
+ end
88
+
48
89
  private
49
90
 
50
- def set_options(complexity)
91
+ def game_options(complexity)
51
92
  case complexity
52
93
  when 'easy'
53
- {attempts: 14, hints: 2}
94
+ { attempts: 14, hints: 2 }
54
95
  when 'medium'
55
- {attempts: 10, hints: 1}
96
+ { attempts: 10, hints: 1 }
56
97
  when 'hard'
57
- {attempts: 5, hints: 1}
58
- else
59
- raise ERR_UNEXPECTED_COMPLEXITY
98
+ { attempts: 5, hints: 1 }
60
99
  end
61
100
  end
62
101
  end
63
102
  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.2'
5
5
  end
@@ -3,7 +3,14 @@
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
15
 
9
16
  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.2
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-02 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
@@ -35,8 +91,13 @@ files:
35
91
  - codebracker_simb.gemspec
36
92
  - lib/codebracker_simb.rb
37
93
  - lib/codebracker_simb/checker.rb
38
- - lib/codebracker_simb/code.rb
94
+ - lib/codebracker_simb/errors/code_error.rb
95
+ - lib/codebracker_simb/errors/complexity_error.rb
96
+ - lib/codebracker_simb/errors/intro_error.rb
97
+ - lib/codebracker_simb/errors/name_error.rb
98
+ - lib/codebracker_simb/errors/no_hints_error.rb
39
99
  - lib/codebracker_simb/game.rb
100
+ - lib/codebracker_simb/text.rb
40
101
  - lib/codebracker_simb/validator.rb
41
102
  - lib/codebracker_simb/version.rb
42
103
  homepage:
@@ -52,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
113
  requirements:
53
114
  - - ">="
54
115
  - !ruby/object:Gem::Version
55
- version: 2.6.0
116
+ version: 3.0.0
56
117
  required_rubygems_version: !ruby/object:Gem::Requirement
57
118
  requirements:
58
119
  - - ">="
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CodebrackerSimb
4
- # return array from users input
5
- class Code < Array
6
- def initialize(str)
7
- arr = str.split('').map(&:to_i)
8
- super(arr)
9
- end
10
- end
11
- end