codebreaker_marian 0.1.5
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 +7 -0
- data/.circleci/config.yml +65 -0
- data/.fasterer.yml +22 -0
- data/.gitignore +8 -0
- data/.rspec +3 -0
- data/.rubocop.yml +24 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +86 -0
- data/README.md +36 -0
- data/Rakefile +8 -0
- data/autoload.rb +14 -0
- data/bin/console +16 -0
- data/bin/setup +9 -0
- data/codebreaker.gemspec +45 -0
- data/database/.gitignore +4 -0
- data/index.rb +5 -0
- data/lib/codebreaker.rb +7 -0
- data/lib/codebreaker/entities/data_storage.rb +31 -0
- data/lib/codebreaker/entities/game.rb +75 -0
- data/lib/codebreaker/entities/menu.rb +163 -0
- data/lib/codebreaker/entities/processor.rb +38 -0
- data/lib/codebreaker/entities/renderer.rb +67 -0
- data/lib/codebreaker/entities/statistics.rb +24 -0
- data/lib/codebreaker/i18n_config.rb +4 -0
- data/lib/codebreaker/locales/en.yml +39 -0
- data/lib/codebreaker/modules/validator.rb +21 -0
- data/lib/codebreaker/version.rb +5 -0
- metadata +212 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b2fab79cea8eddbaf7005de8ee122e76cf0debd9490fc8e73d150e4f78a334ae
|
4
|
+
data.tar.gz: 5c355f6fb8c3fe20a504b6aa6d16ccfc1fe7e3f6ea6c6873d4442db6a69bd6c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18dd9a7af8263d81a6ac85fabb1ab9a4328781e372d72a2b016741139dd41613a7e0c3a0e18cba1f68c52fb47417085b8010e58c7c3115fbaf0d5b12a35cb02c
|
7
|
+
data.tar.gz: e6eccd2a50248555d32a26e35f0f20a51f49e3d1f8a063ce529db7da1276a1220cc74f000f4a70af94630bbe1117f4d33c8adece98d6ca0c0d1cd38a978149ae
|
@@ -0,0 +1,65 @@
|
|
1
|
+
version: 2.1
|
2
|
+
|
3
|
+
executors:
|
4
|
+
default:
|
5
|
+
working_directory: ~/repo
|
6
|
+
description: The official CircleCI Ruby Docker image
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.7.0
|
9
|
+
|
10
|
+
caches:
|
11
|
+
- &bundle_cache_full v2-repo-{{ checksum "Gemfile.lock" }}
|
12
|
+
- &bundle_cache v2-repo-
|
13
|
+
|
14
|
+
commands:
|
15
|
+
defaults:
|
16
|
+
steps:
|
17
|
+
- checkout
|
18
|
+
- restore_cache:
|
19
|
+
keys:
|
20
|
+
- *bundle_cache_full
|
21
|
+
- *bundle_cache
|
22
|
+
- run: bundle install --path vendor/bundle
|
23
|
+
- save_cache:
|
24
|
+
key: *bundle_cache_full
|
25
|
+
paths:
|
26
|
+
- vendor/bundle
|
27
|
+
run_linters:
|
28
|
+
description: command to start linters
|
29
|
+
steps:
|
30
|
+
- run:
|
31
|
+
name: rubocop
|
32
|
+
command: bundle exec rubocop
|
33
|
+
- run:
|
34
|
+
name: fasterer
|
35
|
+
command: bundle exec fasterer
|
36
|
+
run_specs:
|
37
|
+
steps:
|
38
|
+
- run:
|
39
|
+
name: run specs
|
40
|
+
command: |
|
41
|
+
mkdir /tmp/test-results
|
42
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
43
|
+
bundle exec rspec --format progress \
|
44
|
+
--out /tmp/test-results/rspec.xml \
|
45
|
+
$TEST_FILES
|
46
|
+
jobs:
|
47
|
+
lintering:
|
48
|
+
executor: default
|
49
|
+
steps:
|
50
|
+
- defaults
|
51
|
+
- run_linters
|
52
|
+
run_specs:
|
53
|
+
executor: default
|
54
|
+
steps:
|
55
|
+
- defaults
|
56
|
+
- run_specs
|
57
|
+
|
58
|
+
workflows:
|
59
|
+
version: 2
|
60
|
+
build:
|
61
|
+
jobs:
|
62
|
+
- lintering
|
63
|
+
- run_specs:
|
64
|
+
requires:
|
65
|
+
- lintering
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
speedups:
|
2
|
+
rescue_vs_respond_to: true
|
3
|
+
module_eval: true
|
4
|
+
shuffle_first_vs_sample: true
|
5
|
+
for_loop_vs_each: true
|
6
|
+
each_with_index_vs_while: true
|
7
|
+
map_flatten_vs_flat_map: true
|
8
|
+
reverse_each_vs_reverse_each: true
|
9
|
+
select_first_vs_detect: true
|
10
|
+
sort_vs_sort_by: true
|
11
|
+
fetch_with_argument_vs_block: true
|
12
|
+
keys_each_vs_each_key: true
|
13
|
+
hash_merge_bang_vs_hash_brackets: true
|
14
|
+
block_vs_symbol_to_proc: true
|
15
|
+
proc_call_vs_yield: true
|
16
|
+
gsub_vs_tr: true
|
17
|
+
select_last_vs_reverse_detect: true
|
18
|
+
getter_vs_attr_reader: true
|
19
|
+
setter_vs_attr_writer: true
|
20
|
+
|
21
|
+
exclude_paths:
|
22
|
+
- 'vendor/**/*.rb'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
AllCops:
|
3
|
+
TargetRubyVersion: 2.7
|
4
|
+
NewCops: enable
|
5
|
+
|
6
|
+
Metrics/LineLength:
|
7
|
+
Max: 120
|
8
|
+
|
9
|
+
Metrics/ClassLength:
|
10
|
+
Max: 125
|
11
|
+
|
12
|
+
Metrics/BlockLength:
|
13
|
+
ExcludedMethods: ['describe', 'context']
|
14
|
+
|
15
|
+
RSpec/MessageSpies:
|
16
|
+
EnforcedStyle: receive
|
17
|
+
|
18
|
+
RSpec/SubjectStub:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Documentation:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
require: rubocop-rspec
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rg-codebreaker
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
codebreaker_marian (0.1.5)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.1)
|
10
|
+
coderay (1.1.3)
|
11
|
+
colorize (0.8.1)
|
12
|
+
concurrent-ruby (1.1.6)
|
13
|
+
diff-lcs (1.4.4)
|
14
|
+
docile (1.3.2)
|
15
|
+
fasterer (0.8.3)
|
16
|
+
colorize (~> 0.7)
|
17
|
+
ruby_parser (>= 3.14.1)
|
18
|
+
i18n (1.8.5)
|
19
|
+
concurrent-ruby (~> 1.0)
|
20
|
+
method_source (1.0.0)
|
21
|
+
parallel (1.19.2)
|
22
|
+
parser (2.7.1.4)
|
23
|
+
ast (~> 2.4.1)
|
24
|
+
pry (0.13.1)
|
25
|
+
coderay (~> 1.1)
|
26
|
+
method_source (~> 1.0)
|
27
|
+
rainbow (3.0.0)
|
28
|
+
rake (10.5.0)
|
29
|
+
regexp_parser (1.7.1)
|
30
|
+
rexml (3.2.4)
|
31
|
+
rspec (3.9.0)
|
32
|
+
rspec-core (~> 3.9.0)
|
33
|
+
rspec-expectations (~> 3.9.0)
|
34
|
+
rspec-mocks (~> 3.9.0)
|
35
|
+
rspec-core (3.9.2)
|
36
|
+
rspec-support (~> 3.9.3)
|
37
|
+
rspec-expectations (3.9.2)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-mocks (3.9.1)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.9.0)
|
43
|
+
rspec-support (3.9.3)
|
44
|
+
rspec_junit_formatter (0.4.1)
|
45
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
46
|
+
rubocop (0.88.0)
|
47
|
+
parallel (~> 1.10)
|
48
|
+
parser (>= 2.7.1.1)
|
49
|
+
rainbow (>= 2.2.2, < 4.0)
|
50
|
+
regexp_parser (>= 1.7)
|
51
|
+
rexml
|
52
|
+
rubocop-ast (>= 0.1.0, < 1.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
55
|
+
rubocop-ast (0.2.0)
|
56
|
+
parser (>= 2.7.0.1)
|
57
|
+
rubocop-rspec (1.42.0)
|
58
|
+
rubocop (>= 0.87.0)
|
59
|
+
ruby-progressbar (1.10.1)
|
60
|
+
ruby_parser (3.14.2)
|
61
|
+
sexp_processor (~> 4.9)
|
62
|
+
sexp_processor (4.15.0)
|
63
|
+
simplecov (0.18.5)
|
64
|
+
docile (~> 1.1)
|
65
|
+
simplecov-html (~> 0.11)
|
66
|
+
simplecov-html (0.12.2)
|
67
|
+
unicode-display_width (1.7.0)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
bundler (~> 2.1.4)
|
74
|
+
codebreaker_marian!
|
75
|
+
fasterer
|
76
|
+
i18n
|
77
|
+
pry
|
78
|
+
rake (~> 10.0)
|
79
|
+
rspec (~> 3.0)
|
80
|
+
rspec_junit_formatter
|
81
|
+
rubocop
|
82
|
+
rubocop-rspec
|
83
|
+
simplecov
|
84
|
+
|
85
|
+
BUNDLED WITH
|
86
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Codebreaker
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codebreaker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'codebreaker'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install codebreaker
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreaker.
|
36
|
+
|
data/Rakefile
ADDED
data/autoload.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'i18n'
|
4
|
+
require 'yaml'
|
5
|
+
require_relative 'lib/codebreaker/i18n_config'
|
6
|
+
require_relative 'lib/codebreaker/modules/validator'
|
7
|
+
require_relative 'lib/codebreaker/entities/game'
|
8
|
+
require_relative 'lib/codebreaker/entities/game'
|
9
|
+
require_relative 'lib/codebreaker/entities/data_storage'
|
10
|
+
require_relative 'lib/codebreaker/entities/processor'
|
11
|
+
require_relative 'lib/codebreaker/entities/menu'
|
12
|
+
require_relative 'lib/codebreaker/entities/renderer'
|
13
|
+
require_relative 'lib/codebreaker/entities/statistics'
|
14
|
+
require_relative 'lib/codebreaker/version'
|
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#!/usr/bin/env ruby
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'codebreaker'
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
# require 'pry'
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
require 'irb'
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/codebreaker.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require_relative 'lib/codebreaker/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'codebreaker_marian'
|
9
|
+
spec.version = Codebreaker::VERSION
|
10
|
+
spec.authors = ['Marian Rebeha']
|
11
|
+
spec.email = ['marikrebega@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Codebreaker app'
|
14
|
+
spec.description = 'Logic game'
|
15
|
+
spec.homepage = 'https://github.com/marikrebega/RubyGarage_2_Codebreaker_gem'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = 'exe'
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ['lib']
|
34
|
+
|
35
|
+
spec.add_development_dependency 'bundler', '~> 2.1.4'
|
36
|
+
spec.add_development_dependency 'fasterer'
|
37
|
+
spec.add_development_dependency 'i18n'
|
38
|
+
spec.add_development_dependency 'pry'
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
40
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
41
|
+
spec.add_development_dependency 'rspec_junit_formatter'
|
42
|
+
spec.add_development_dependency 'rubocop'
|
43
|
+
spec.add_development_dependency 'rubocop-rspec'
|
44
|
+
spec.add_development_dependency 'simplecov'
|
45
|
+
end
|
data/database/.gitignore
ADDED
data/index.rb
ADDED
data/lib/codebreaker.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class DataStorage
|
6
|
+
FILE_NAME = 'database/data.yml'
|
7
|
+
|
8
|
+
def create
|
9
|
+
File.new(FILE_NAME, 'w')
|
10
|
+
File.write(FILE_NAME, [].to_yaml)
|
11
|
+
end
|
12
|
+
|
13
|
+
def load
|
14
|
+
YAML.safe_load(File.open(FILE_NAME), [Symbol, Time]) if storage_exist?
|
15
|
+
end
|
16
|
+
|
17
|
+
def save(object)
|
18
|
+
File.open(FILE_NAME, 'w') { |file| file.write(YAML.dump(object)) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def storage_exist?
|
22
|
+
File.exist?(FILE_NAME)
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_game_result(object)
|
26
|
+
create unless storage_exist?
|
27
|
+
save(load.push(object))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class Game
|
6
|
+
DIGITS_COUNT = 4
|
7
|
+
DIFFICULTIES = {
|
8
|
+
easy: {
|
9
|
+
attempts: 15,
|
10
|
+
hints: 2
|
11
|
+
},
|
12
|
+
medium: {
|
13
|
+
attempts: 10,
|
14
|
+
hints: 1
|
15
|
+
},
|
16
|
+
hell: {
|
17
|
+
attempts: 5,
|
18
|
+
hints: 1
|
19
|
+
}
|
20
|
+
}.freeze
|
21
|
+
RANGE = (1..6).freeze
|
22
|
+
|
23
|
+
attr_reader :attempts, :hints, :code
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
@process = Processor.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def generate(difficulty)
|
30
|
+
@difficulty = difficulty
|
31
|
+
@code = generate_secret_code
|
32
|
+
@hints = @code.sample(difficulty[:hints])
|
33
|
+
@attempts = difficulty[:attempts]
|
34
|
+
end
|
35
|
+
|
36
|
+
def start_process(command)
|
37
|
+
@process.secret_code_proc(code.join, command)
|
38
|
+
end
|
39
|
+
|
40
|
+
def win?(guess)
|
41
|
+
code.join == guess
|
42
|
+
end
|
43
|
+
|
44
|
+
def decrease_attempts!
|
45
|
+
@attempts -= 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_h(name)
|
49
|
+
{
|
50
|
+
name: name,
|
51
|
+
difficulty: DIFFICULTIES.key(@difficulty),
|
52
|
+
all_attempts: @difficulty[:attempts],
|
53
|
+
all_hints: @difficulty[:hints],
|
54
|
+
attempts_used: @difficulty[:attempts] - @attempts,
|
55
|
+
hints_used: @difficulty[:hints] - @hints.length,
|
56
|
+
date: Time.now
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def hints_spent?
|
61
|
+
hints.empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
def take_a_hint!
|
65
|
+
hints.pop
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def generate_secret_code
|
71
|
+
Array.new(DIGITS_COUNT) { rand(RANGE) }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class Menu
|
6
|
+
include Modules::Validator
|
7
|
+
attr_reader :storage, :renderer, :game, :guess
|
8
|
+
|
9
|
+
COMMANDS = {
|
10
|
+
start: 'start',
|
11
|
+
exit: 'exit',
|
12
|
+
rules: 'rules',
|
13
|
+
stats: 'stats'
|
14
|
+
}.freeze
|
15
|
+
CHOOSE_COMMANDS = {
|
16
|
+
yes: 'yes'
|
17
|
+
}.freeze
|
18
|
+
HINT_COMMAND = 'hint'
|
19
|
+
MIN_SIZE_VALUE = 3
|
20
|
+
MAX_SIZE_VALUE = 20
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@storage = DataStorage.new
|
24
|
+
@renderer = Renderer.new
|
25
|
+
@game = Game.new
|
26
|
+
@statistics = Statistics.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def game_menu
|
30
|
+
renderer.start_message
|
31
|
+
|
32
|
+
choice_menu_process(ask(:choice_options, commands: COMMANDS.keys.join(' | ')))
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def rules
|
38
|
+
renderer.rules
|
39
|
+
game_menu
|
40
|
+
end
|
41
|
+
|
42
|
+
def start
|
43
|
+
@name = registrate_user
|
44
|
+
level_choice
|
45
|
+
game_process
|
46
|
+
end
|
47
|
+
|
48
|
+
def stats
|
49
|
+
scores = storage.load
|
50
|
+
render_stats(@statistics.stats(scores)) if scores
|
51
|
+
game_menu
|
52
|
+
end
|
53
|
+
|
54
|
+
def ask(phrase_key = nil, options = {})
|
55
|
+
renderer.message(phrase_key, options) if phrase_key
|
56
|
+
gets.chomp
|
57
|
+
end
|
58
|
+
|
59
|
+
def save_result
|
60
|
+
storage.save_game_result(game.to_h(@name)) if ask(:save_result_message) == CHOOSE_COMMANDS[:yes]
|
61
|
+
end
|
62
|
+
|
63
|
+
def registrate_user
|
64
|
+
loop do
|
65
|
+
name = ask(:registration)
|
66
|
+
|
67
|
+
return name if name_valid?(name)
|
68
|
+
|
69
|
+
renderer.registration_name_length_error
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def name_valid?(name)
|
74
|
+
!check_emptyness(name) && check_length(name, MIN_SIZE_VALUE, MAX_SIZE_VALUE)
|
75
|
+
end
|
76
|
+
|
77
|
+
def level_choice
|
78
|
+
loop do
|
79
|
+
level = ask(:hard_level, levels: Game::DIFFICULTIES.keys.join(' | '))
|
80
|
+
|
81
|
+
return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
|
82
|
+
return game_menu if level == COMMANDS[:exit]
|
83
|
+
|
84
|
+
renderer.command_error
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def generate_game(difficulty)
|
89
|
+
game.generate(difficulty)
|
90
|
+
renderer.message(:difficulty, hints: difficulty[:hints], attempts: difficulty[:attempts])
|
91
|
+
end
|
92
|
+
|
93
|
+
def game_process
|
94
|
+
while game.attempts.positive?
|
95
|
+
@guess = ask
|
96
|
+
return handle_win if game.win?(guess)
|
97
|
+
|
98
|
+
choice_code_process
|
99
|
+
end
|
100
|
+
handle_lose
|
101
|
+
end
|
102
|
+
|
103
|
+
def choice_code_process
|
104
|
+
case guess
|
105
|
+
when HINT_COMMAND then hint_process
|
106
|
+
when COMMANDS[:exit] then game_menu
|
107
|
+
else handle_command
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def handle_command
|
112
|
+
return renderer.command_error unless check_command_range(guess)
|
113
|
+
|
114
|
+
p game.start_process(guess)
|
115
|
+
renderer.round_message
|
116
|
+
game.decrease_attempts!
|
117
|
+
end
|
118
|
+
|
119
|
+
def handle_win
|
120
|
+
renderer.win_game_message
|
121
|
+
save_result
|
122
|
+
game_menu
|
123
|
+
end
|
124
|
+
|
125
|
+
def handle_lose
|
126
|
+
renderer.lost_game_message(game.code)
|
127
|
+
game_menu
|
128
|
+
end
|
129
|
+
|
130
|
+
def hint_process
|
131
|
+
return renderer.no_hints_message? if game.hints_spent?
|
132
|
+
|
133
|
+
renderer.print_hint_number(game.take_a_hint!)
|
134
|
+
end
|
135
|
+
|
136
|
+
def exit_from_game
|
137
|
+
renderer.goodbye_message
|
138
|
+
exit
|
139
|
+
end
|
140
|
+
|
141
|
+
def choice_menu_process(command_name)
|
142
|
+
case command_name
|
143
|
+
when COMMANDS[:start] then start
|
144
|
+
when COMMANDS[:exit] then exit_from_game
|
145
|
+
when COMMANDS[:rules] then rules
|
146
|
+
when COMMANDS[:stats] then stats
|
147
|
+
else
|
148
|
+
renderer.command_error
|
149
|
+
game_menu
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def render_stats(list)
|
154
|
+
index = 0
|
155
|
+
while index < list.size
|
156
|
+
puts "#{index + 1}: "
|
157
|
+
list[index].each { |param, value| puts "#{param}:#{value}" }
|
158
|
+
index += 1
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class Processor
|
6
|
+
MATCHED_DIGIT_CHAR = '+'
|
7
|
+
UNMATCHED_DIGIT_CHAR = '-'
|
8
|
+
|
9
|
+
attr_reader :guess, :code, :result
|
10
|
+
|
11
|
+
def secret_code_proc(code, guess)
|
12
|
+
@code = code.split('')
|
13
|
+
@guess = guess.split('')
|
14
|
+
handle_matched_digits.join + handle_matched_digits_with_wrong_position.join
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def handle_matched_digits
|
20
|
+
code.map.with_index do |_, index|
|
21
|
+
next unless code[index] == guess[index]
|
22
|
+
|
23
|
+
@guess[index], @code[index] = nil
|
24
|
+
MATCHED_DIGIT_CHAR
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def handle_matched_digits_with_wrong_position
|
29
|
+
guess.compact.map do |number|
|
30
|
+
next unless @code.include?(number)
|
31
|
+
|
32
|
+
@code.delete_at(code.index(number))
|
33
|
+
UNMATCHED_DIGIT_CHAR
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class Renderer
|
6
|
+
def message(msg_name, hashee = {})
|
7
|
+
puts I18n.t(msg_name, **hashee)
|
8
|
+
end
|
9
|
+
|
10
|
+
def start_message
|
11
|
+
message(:start_message)
|
12
|
+
end
|
13
|
+
|
14
|
+
def rules
|
15
|
+
message(:rules)
|
16
|
+
end
|
17
|
+
|
18
|
+
def goodbye_message
|
19
|
+
message(:goodbye_message)
|
20
|
+
end
|
21
|
+
|
22
|
+
def save_results_message
|
23
|
+
message(:save_results_message)
|
24
|
+
end
|
25
|
+
|
26
|
+
def win_game_message
|
27
|
+
message(:win_game_message)
|
28
|
+
end
|
29
|
+
|
30
|
+
def round_message
|
31
|
+
message(:round_message)
|
32
|
+
end
|
33
|
+
|
34
|
+
def lost_game_message(code)
|
35
|
+
message(:lost_game_message, code: code)
|
36
|
+
end
|
37
|
+
|
38
|
+
def no_hints_message?
|
39
|
+
message(:have_no_hints_message)
|
40
|
+
end
|
41
|
+
|
42
|
+
def print_hint_number(code)
|
43
|
+
message(:print_hint_number, code: code)
|
44
|
+
end
|
45
|
+
|
46
|
+
def registration_name_emptyness_error
|
47
|
+
message(:registration_name_emptyness_error)
|
48
|
+
end
|
49
|
+
|
50
|
+
def registration_name_length_error
|
51
|
+
message(:registration_name_length_error)
|
52
|
+
end
|
53
|
+
|
54
|
+
def command_error
|
55
|
+
message(:command_error)
|
56
|
+
end
|
57
|
+
|
58
|
+
def command_length_error
|
59
|
+
message(:command_length_error)
|
60
|
+
end
|
61
|
+
|
62
|
+
def command_int_error
|
63
|
+
message(:command_int_error)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Entities
|
5
|
+
class Statistics
|
6
|
+
def stats(list)
|
7
|
+
difficulties = list.group_by { |score| score[:difficulty] }
|
8
|
+
%i[hell medium easy].reduce([]) do |sorted_difficulties, difficulty_name|
|
9
|
+
if difficulties[difficulty_name]
|
10
|
+
sorted_difficulties + stats_sort(difficulties[difficulty_name])
|
11
|
+
else
|
12
|
+
sorted_difficulties
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def stats_sort(scores)
|
20
|
+
scores.sort_by! { |score| [score[:attempts_used], score[:hints_used]] }.reverse
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
en:
|
2
|
+
start_message: "Welcome to CodeBreaker"
|
3
|
+
choice_options: "Choose option:
|
4
|
+
%{commands}"
|
5
|
+
hard_level: "Which level you do you want to play?
|
6
|
+
%{levels}"
|
7
|
+
goodbye_message: "We are waiting for you. Come back!"
|
8
|
+
command_error: "You have passed unexpected command. Please choose one from listed commands"
|
9
|
+
rules: "Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
|
10
|
+
The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
|
11
|
+
|
12
|
+
A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code and in the same position. For example:
|
13
|
+
Secret number - 1234
|
14
|
+
Input number - 6264
|
15
|
+
Number of pluses - 2 (second and fourth position)
|
16
|
+
|
17
|
+
A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code but in a different position. For example:
|
18
|
+
Secret number - 1234
|
19
|
+
Input number - 6462
|
20
|
+
Number of minuses - 2 (second and fourth position)
|
21
|
+
|
22
|
+
An empty space indicates that there is not a current digit in a secret number.
|
23
|
+
|
24
|
+
If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
|
25
|
+
|
26
|
+
Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint - he receives back a separate digit of the secret code.
|
27
|
+
"
|
28
|
+
registration: "Before start, enter your name, please "
|
29
|
+
difficulty: "You had to %{hints} hints and %{attempts} attempts"
|
30
|
+
round_message: "1. Enter your secret code
|
31
|
+
2. hint
|
32
|
+
3. exit "
|
33
|
+
guess: "Opps, secret code must to be from four digits in range from 1 to 6"
|
34
|
+
lost_game_message: "Oh, your attempts ended... Your code was: %{code}"
|
35
|
+
win_game_message: "Yayy, u won the game! Congrats!"
|
36
|
+
save_results_message: "Do you want to save result? 1. yes 2. no"
|
37
|
+
have_no_hints_message: "Oh, your hints ended"
|
38
|
+
print_hint_number: "Hint number: %{code}"
|
39
|
+
registration_name_length_error: "It must be more than 3 and less than 20 symbols."
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
module Modules
|
5
|
+
module Validator
|
6
|
+
VALUE_FORMAT = /^[1-6]{4}$/.freeze
|
7
|
+
|
8
|
+
def check_emptyness(value)
|
9
|
+
value.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
def check_length(value, min_size, max_size)
|
13
|
+
value.size.between?(min_size, max_size)
|
14
|
+
end
|
15
|
+
|
16
|
+
def check_command_range(command)
|
17
|
+
command =~ VALUE_FORMAT
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker_marian
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marian Rebeha
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-28 00:00:00.000000000 Z
|
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.4
|
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.4
|
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: i18n
|
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: pry
|
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: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec_junit_formatter
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Logic game
|
154
|
+
email:
|
155
|
+
- marikrebega@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".circleci/config.yml"
|
161
|
+
- ".fasterer.yml"
|
162
|
+
- ".gitignore"
|
163
|
+
- ".rspec"
|
164
|
+
- ".rubocop.yml"
|
165
|
+
- ".ruby-gemset"
|
166
|
+
- ".ruby-version"
|
167
|
+
- ".travis.yml"
|
168
|
+
- Gemfile
|
169
|
+
- Gemfile.lock
|
170
|
+
- README.md
|
171
|
+
- Rakefile
|
172
|
+
- autoload.rb
|
173
|
+
- bin/console
|
174
|
+
- bin/setup
|
175
|
+
- codebreaker.gemspec
|
176
|
+
- database/.gitignore
|
177
|
+
- index.rb
|
178
|
+
- lib/codebreaker.rb
|
179
|
+
- lib/codebreaker/entities/data_storage.rb
|
180
|
+
- lib/codebreaker/entities/game.rb
|
181
|
+
- lib/codebreaker/entities/menu.rb
|
182
|
+
- lib/codebreaker/entities/processor.rb
|
183
|
+
- lib/codebreaker/entities/renderer.rb
|
184
|
+
- lib/codebreaker/entities/statistics.rb
|
185
|
+
- lib/codebreaker/i18n_config.rb
|
186
|
+
- lib/codebreaker/locales/en.yml
|
187
|
+
- lib/codebreaker/modules/validator.rb
|
188
|
+
- lib/codebreaker/version.rb
|
189
|
+
homepage: https://github.com/marikrebega/RubyGarage_2_Codebreaker_gem
|
190
|
+
licenses: []
|
191
|
+
metadata:
|
192
|
+
allowed_push_host: https://rubygems.org
|
193
|
+
post_install_message:
|
194
|
+
rdoc_options: []
|
195
|
+
require_paths:
|
196
|
+
- lib
|
197
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
requirements: []
|
208
|
+
rubygems_version: 3.1.4
|
209
|
+
signing_key:
|
210
|
+
specification_version: 4
|
211
|
+
summary: Codebreaker app
|
212
|
+
test_files: []
|