codebracker_simb 0.2.2 → 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 +4 -4
- data/Gemfile +2 -0
- data/codebracker_simb.gemspec +17 -12
- data/lib/codebracker_simb/checker.rb +7 -5
- data/lib/codebracker_simb/errors/code_error.rb +8 -0
- data/lib/codebracker_simb/errors/complexity_error.rb +8 -0
- data/lib/codebracker_simb/errors/intro_error.rb +8 -0
- data/lib/codebracker_simb/errors/name_error.rb +8 -0
- data/lib/codebracker_simb/errors/no_hints_error.rb +8 -0
- data/lib/codebracker_simb/game.rb +59 -23
- data/lib/codebracker_simb/text.rb +33 -0
- data/lib/codebracker_simb/validator.rb +14 -0
- data/lib/codebracker_simb/version.rb +1 -1
- data/lib/codebracker_simb.rb +8 -1
- metadata +66 -5
- data/lib/codebracker_simb/code_validator.rb +0 -13
- data/lib/codebracker_simb/name_validator.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5435653ed5020dc12fce5f812025ca52ab4aa068d9835962d65ec735897d2b4f
|
4
|
+
data.tar.gz: fe29d8332ca574cc900eb6f02e41408e1c03cf5e118bfbc072570733759740da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 322b9b991f70fb787cddde04d7192aefc91341c169669196dcf01c84fe728feb35444566faeaaca05342824da2f7111ec372196bf75657b071c4b2288526b271
|
7
|
+
data.tar.gz: 25a6c510d2a31f5e9a752b30748feeb0a386beb8e2b5eacb5d7e7b6581ba88eeccb1c11981f4ce3178c4b070dd48caef33b55d5a5b22214c3e1f5b4aa559b0f7
|
data/Gemfile
CHANGED
data/codebracker_simb.gemspec
CHANGED
@@ -1,25 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
3
|
+
require_relative 'lib/codebracker_simb/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
6
|
+
spec.name = 'codebracker_simb'
|
7
7
|
spec.version = CodebrackerSimb::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
8
|
+
spec.authors = ['max']
|
9
|
+
spec.email = ['laterty@gmail.com']
|
10
10
|
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.license =
|
14
|
-
spec.required_ruby_version =
|
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[
|
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',
|
21
|
-
|
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 = [
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
@@ -3,26 +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
|
-
|
11
|
-
attr_reader :player
|
12
|
-
attr_accessor :answer
|
13
|
-
|
14
|
-
include ::CodebrackerSimb::CodeValidator
|
15
|
-
include ::CodebrackerSimb::NameValidator
|
12
|
+
attr_reader :code, :attempts, :player, :complexity
|
13
|
+
attr_accessor :answer, :hint_positions, :hints
|
16
14
|
|
17
|
-
ERR_UNEXPECTED_COMPLEXITY = 'complexity could be easy, medium or hard'
|
18
15
|
IS_OVER = 'Game is over, you lose'
|
19
16
|
GREETING = 'Hello! Welcome to CodeBracker Game! Have fun!'
|
20
17
|
CONGRATULATIONS = 'Congratulations! You broked a code, master!'
|
21
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
|
+
|
22
29
|
def initialize(complexity, player)
|
23
|
-
@attempts =
|
24
|
-
@hints =
|
30
|
+
@attempts = game_options(complexity)[:attempts]
|
31
|
+
@hints = game_options(complexity)[:hints]
|
25
32
|
@player = player
|
33
|
+
@hint_positions = []
|
34
|
+
@complexity = complexity
|
35
|
+
define_code
|
26
36
|
end
|
27
37
|
|
28
38
|
def input_answer(input)
|
@@ -30,7 +40,7 @@ module CodebrackerSimb
|
|
30
40
|
end
|
31
41
|
|
32
42
|
def check_answer
|
33
|
-
Checker.new(code, answer).compare
|
43
|
+
Checker.new(@code, @answer).compare
|
34
44
|
end
|
35
45
|
|
36
46
|
def define_code
|
@@ -38,30 +48,56 @@ module CodebrackerSimb
|
|
38
48
|
end
|
39
49
|
|
40
50
|
def end_with_win?
|
41
|
-
code == answer
|
51
|
+
code == answer
|
42
52
|
end
|
43
53
|
|
44
54
|
def refresh_attempts_quantity
|
45
|
-
if @attempts
|
55
|
+
if @attempts.positive?
|
46
56
|
@attempts -= 1
|
47
|
-
|
57
|
+
"#{@attempts} left"
|
48
58
|
end
|
49
59
|
end
|
50
|
-
|
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
|
73
|
+
end
|
74
|
+
end
|
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
|
+
|
51
90
|
private
|
52
91
|
|
53
|
-
def
|
92
|
+
def game_options(complexity)
|
54
93
|
case complexity
|
55
94
|
when 'easy'
|
56
|
-
{attempts: 14, hints: 2}
|
95
|
+
{ attempts: 14, hints: 2 }
|
57
96
|
when 'medium'
|
58
|
-
{attempts: 10, hints: 1}
|
97
|
+
{ attempts: 10, hints: 1 }
|
59
98
|
when 'hard'
|
60
|
-
{attempts: 5, hints: 1}
|
61
|
-
else
|
62
|
-
raise ERR_UNEXPECTED_COMPLEXITY
|
99
|
+
{ attempts: 5, hints: 1 }
|
63
100
|
end
|
64
101
|
end
|
65
102
|
end
|
66
103
|
end
|
67
|
-
|
@@ -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
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodebrackerSimb
|
4
|
+
# validator class
|
5
|
+
class Validator
|
6
|
+
def self.valid_name?(name)
|
7
|
+
name.is_a?(String) && name.length > 3 && name.length < 20
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.valid_code?(code)
|
11
|
+
code =~ /^[1-6]{4}$/
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/codebracker_simb.rb
CHANGED
@@ -2,7 +2,14 @@
|
|
2
2
|
|
3
3
|
require_relative 'codebracker_simb/version'
|
4
4
|
require_relative 'codebracker_simb/game'
|
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'
|
5
12
|
|
13
|
+
# gem module
|
6
14
|
module CodebrackerSimb
|
7
|
-
|
8
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.
|
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-
|
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,9 +92,14 @@ files:
|
|
36
92
|
- lib/codebracker_simb.rb
|
37
93
|
- lib/codebracker_simb/checker.rb
|
38
94
|
- lib/codebracker_simb/code.rb
|
39
|
-
- lib/codebracker_simb/
|
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
|
40
100
|
- lib/codebracker_simb/game.rb
|
41
|
-
- lib/codebracker_simb/
|
101
|
+
- lib/codebracker_simb/text.rb
|
102
|
+
- lib/codebracker_simb/validator.rb
|
42
103
|
- lib/codebracker_simb/version.rb
|
43
104
|
homepage:
|
44
105
|
licenses:
|
@@ -53,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
114
|
requirements:
|
54
115
|
- - ">="
|
55
116
|
- !ruby/object:Gem::Version
|
56
|
-
version:
|
117
|
+
version: 3.0.0
|
57
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
119
|
requirements:
|
59
120
|
- - ">="
|