GemCodebreaker 0.1.6.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +62 -0
- data/.fasterer.yml +21 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/.rubocop.yml +22 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +77 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/gem_codebreaker.gemspec +38 -0
- data/lib/gem_codebreaker/classes/code_maker.rb +41 -0
- data/lib/gem_codebreaker/classes/game.rb +73 -0
- data/lib/gem_codebreaker/classes/game_config.rb +65 -0
- data/lib/gem_codebreaker/classes/game_statistic.rb +17 -0
- data/lib/gem_codebreaker/classes/serializer_deserializer.rb +27 -0
- data/lib/gem_codebreaker/classes/user.rb +18 -0
- data/lib/gem_codebreaker/config/i18n_locales.rb +5 -0
- data/lib/gem_codebreaker/errors/inclusion_error.rb +10 -0
- data/lib/gem_codebreaker/errors/length_error.rb +10 -0
- data/lib/gem_codebreaker/errors/no_attempts_error.rb +10 -0
- data/lib/gem_codebreaker/errors/no_hints_error.rb +10 -0
- data/lib/gem_codebreaker/errors/value_range_error.rb +10 -0
- data/lib/gem_codebreaker/errors/wrong_class_error.rb +10 -0
- data/lib/gem_codebreaker/gem_codebreaker.rb +18 -0
- data/lib/gem_codebreaker/modules/validator.rb +17 -0
- data/lib/gem_codebreaker/version.rb +5 -0
- metadata +46 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69e49f4ffb2630a8bef188ec68997d6d8c43607b16e695f2ca421468142cad86
|
4
|
+
data.tar.gz: 53bc491c509d388aefa0cd97a6baf0155efb841bc95b0d4bf634419ff2f6c0ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3dc85b66fb94ceec95703abe269233f662729fd3ace276c7d73a978a223928a1baed552bc8dfdfa8e3570f80507c9a461330d3dff8a76ee143a5e0eddb30343
|
7
|
+
data.tar.gz: 85ff71d2571239835c83b07a2de0cb313d0feb53beae271525e89407a8f7c9a089d4d7048bf1e51adc0b5eb4955ca6e3539878175e623e5a4b6056d443d33f3d
|
@@ -0,0 +1,62 @@
|
|
1
|
+
version: 2.1
|
2
|
+
|
3
|
+
executors:
|
4
|
+
default:
|
5
|
+
working_directory: ~/codebreaker
|
6
|
+
description: Gem for game codebreaker
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.7.1
|
9
|
+
|
10
|
+
caches:
|
11
|
+
- &bundle_cache_full v1-codebreaker-{{ checksum "Gemfile.lock" }}
|
12
|
+
- &bundle_cache v1-codebreaker
|
13
|
+
|
14
|
+
commands:
|
15
|
+
setup_environment:
|
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
|
+
paths:
|
25
|
+
- vendor/bundle
|
26
|
+
key: *bundle_cache_full
|
27
|
+
run_linters:
|
28
|
+
description: Start lintering
|
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
|
+
description: Run specs
|
38
|
+
steps:
|
39
|
+
- run:
|
40
|
+
name: run spec
|
41
|
+
command: bundle exec rspec --format progress spec
|
42
|
+
|
43
|
+
jobs:
|
44
|
+
lintering:
|
45
|
+
executor: default
|
46
|
+
steps:
|
47
|
+
- setup_environment
|
48
|
+
- run_linters
|
49
|
+
run_specs:
|
50
|
+
executor: default
|
51
|
+
steps:
|
52
|
+
- setup_environment
|
53
|
+
- run_specs
|
54
|
+
|
55
|
+
workflows:
|
56
|
+
version: 2
|
57
|
+
build:
|
58
|
+
jobs:
|
59
|
+
- lintering
|
60
|
+
- run_specs:
|
61
|
+
requires:
|
62
|
+
- lintering
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,21 @@
|
|
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: false
|
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
|
+
exclude_paths:
|
21
|
+
- 'vendor/**/*.rb'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.7.1
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
RSpec/MessageSpies:
|
16
|
+
EnforcedStyle: receive
|
17
|
+
|
18
|
+
RSpec/NestedGroups:
|
19
|
+
Max: 6
|
20
|
+
|
21
|
+
Metrics/BlockLength:
|
22
|
+
ExcludedMethods: ['describe', 'context']
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
GemCodebreaker (0.2.0)
|
5
|
+
i18n (~> 1.8.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.1)
|
11
|
+
colorize (0.8.1)
|
12
|
+
concurrent-ruby (1.1.6)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
docile (1.3.2)
|
15
|
+
fasterer (0.8.3)
|
16
|
+
colorize (~> 0.7)
|
17
|
+
ruby_parser (>= 3.14.1)
|
18
|
+
i18n (1.8.3)
|
19
|
+
concurrent-ruby (~> 1.0)
|
20
|
+
parallel (1.19.2)
|
21
|
+
parser (2.7.1.3)
|
22
|
+
ast (~> 2.4.0)
|
23
|
+
rainbow (3.0.0)
|
24
|
+
rake (13.0.1)
|
25
|
+
rexml (3.2.4)
|
26
|
+
rspec (3.7.0)
|
27
|
+
rspec-core (~> 3.7.0)
|
28
|
+
rspec-expectations (~> 3.7.0)
|
29
|
+
rspec-mocks (~> 3.7.0)
|
30
|
+
rspec-core (3.7.1)
|
31
|
+
rspec-support (~> 3.7.0)
|
32
|
+
rspec-expectations (3.7.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.7.0)
|
35
|
+
rspec-mocks (3.7.0)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.7.0)
|
38
|
+
rspec-support (3.7.1)
|
39
|
+
rubocop (0.84.0)
|
40
|
+
parallel (~> 1.10)
|
41
|
+
parser (>= 2.7.0.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
rexml
|
44
|
+
rubocop-ast (>= 0.0.3)
|
45
|
+
ruby-progressbar (~> 1.7)
|
46
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
47
|
+
rubocop-ast (0.0.3)
|
48
|
+
parser (>= 2.7.0.1)
|
49
|
+
rubocop-performance (1.6.1)
|
50
|
+
rubocop (>= 0.71.0)
|
51
|
+
rubocop-rspec (1.41.0)
|
52
|
+
rubocop (>= 0.68.1)
|
53
|
+
ruby-progressbar (1.10.1)
|
54
|
+
ruby_parser (3.14.2)
|
55
|
+
sexp_processor (~> 4.9)
|
56
|
+
sexp_processor (4.15.0)
|
57
|
+
simplecov (0.18.5)
|
58
|
+
docile (~> 1.1)
|
59
|
+
simplecov-html (~> 0.11)
|
60
|
+
simplecov-html (0.12.2)
|
61
|
+
unicode-display_width (1.7.0)
|
62
|
+
|
63
|
+
PLATFORMS
|
64
|
+
ruby
|
65
|
+
|
66
|
+
DEPENDENCIES
|
67
|
+
GemCodebreaker!
|
68
|
+
fasterer (~> 0.8.3)
|
69
|
+
rake (~> 13.0.1)
|
70
|
+
rspec (~> 3.7.0)
|
71
|
+
rubocop (~> 0.84.0)
|
72
|
+
rubocop-performance (~> 1.6.1)
|
73
|
+
rubocop-rspec (~> 1.41.0)
|
74
|
+
simplecov (~> 0.18.5)
|
75
|
+
|
76
|
+
BUNDLED WITH
|
77
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Vasyl
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# GemCodebreaker
|
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/GemCodebreaker`. 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 'GemCodebreaker'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install GemCodebreaker
|
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]/GemCodebreaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/GemCodebreaker/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the GemCodebreaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/GemCodebreaker/blob/master/CODE_OF_CONDUCT.md).
|
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'GemCodebreaker/gem_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
@@ -0,0 +1,38 @@
|
|
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/gem_codebreaker/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'GemCodebreaker'
|
9
|
+
spec.version = GemCodebreaker::VERSION
|
10
|
+
spec.authors = ['Vasyl']
|
11
|
+
spec.email = ['wkichira@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Codebreaker Game'
|
14
|
+
spec.description = 'This gem for Codebreaker Game'
|
15
|
+
spec.homepage = 'https://github.com/vasivaas/GemCodebreaker'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
18
|
+
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
20
|
+
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
spec.metadata['source_code_uri'] = 'https://github.com/vasivaas/GemCodebreaker'
|
23
|
+
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
spec.add_development_dependency 'fasterer', '~> 0.8.3'
|
31
|
+
spec.add_dependency 'i18n', '~> 1.8.3'
|
32
|
+
spec.add_development_dependency 'rake', '~> 13.0.1'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.7.0'
|
34
|
+
spec.add_development_dependency 'rubocop', '~> 0.84.0'
|
35
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.6.1'
|
36
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.41.0'
|
37
|
+
spec.add_development_dependency 'simplecov', '~> 0.18.5'
|
38
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class CodeMaker
|
5
|
+
attr_reader :plus_match, :minus_match, :empty_match
|
6
|
+
|
7
|
+
def initialize(game_code, user_code)
|
8
|
+
@game_code = game_code
|
9
|
+
@user_code = user_code
|
10
|
+
@plus_match = 0
|
11
|
+
@minus_match = 0
|
12
|
+
@empty_match = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def calculate_result_code
|
16
|
+
count_plus_marker
|
17
|
+
count_minus_and_empty_answer
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def count_plus_marker
|
23
|
+
data = @game_code.zip(@user_code)
|
24
|
+
data.delete_if { |game_code_item, user_code_item| @plus_match += 1 if game_code_item == user_code_item }
|
25
|
+
@game_code, @user_code = data.transpose
|
26
|
+
end
|
27
|
+
|
28
|
+
def count_minus_and_empty_answer
|
29
|
+
return if @game_code.nil?
|
30
|
+
|
31
|
+
@game_code.each do |secret_code_item|
|
32
|
+
if @user_code.include?(secret_code_item)
|
33
|
+
@minus_match += 1
|
34
|
+
@user_code.delete_at(@user_code.index(secret_code_item))
|
35
|
+
else
|
36
|
+
@empty_match += 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class Game
|
5
|
+
LENGTH_OF_SECRET_CODE = 4
|
6
|
+
DIGIT_RANGE = (1..6).freeze
|
7
|
+
|
8
|
+
include Validator
|
9
|
+
attr_reader :statistic, :matcher_hash, :config
|
10
|
+
|
11
|
+
def initialize(user, level)
|
12
|
+
@config = GameConfig.new(user, level)
|
13
|
+
@statistic = []
|
14
|
+
secrete_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def guess_start(input_code)
|
18
|
+
guess_valid?(input_code)
|
19
|
+
raise GemCodebreaker::NoAttemptsError if @config.attempts_used?
|
20
|
+
|
21
|
+
@config.increase_user_attempts
|
22
|
+
user_code = input_code.chars.map(&:to_i)
|
23
|
+
code_marker = CodeMaker.new(@secrete_code, user_code)
|
24
|
+
code_marker.calculate_result_code
|
25
|
+
@matcher_hash = { plus: code_marker.plus_match, minus: code_marker.minus_match, empty: code_marker.empty_match }
|
26
|
+
end
|
27
|
+
|
28
|
+
def win_game?
|
29
|
+
@matcher_hash[:plus] == LENGTH_OF_SECRET_CODE
|
30
|
+
end
|
31
|
+
|
32
|
+
def game_over?
|
33
|
+
@config.attempts_used? && win_game? == false
|
34
|
+
end
|
35
|
+
|
36
|
+
def show_secrete_code
|
37
|
+
@secrete_code if win_game? || game_over?
|
38
|
+
end
|
39
|
+
|
40
|
+
def hint
|
41
|
+
raise GemCodebreaker::NoHintsError if @config.hints_used?
|
42
|
+
|
43
|
+
@config.increase_user_hints
|
44
|
+
@secrete_code.sample
|
45
|
+
end
|
46
|
+
|
47
|
+
def restart_game
|
48
|
+
@config.reset_user_attempts_hints
|
49
|
+
@secrete_code = false
|
50
|
+
secrete_code
|
51
|
+
end
|
52
|
+
|
53
|
+
def save_user_statistic
|
54
|
+
@statistic << GameStatistic.new(self)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def guess_valid?(user_code)
|
60
|
+
validate_class(user_code, String)
|
61
|
+
raise GemCodebreaker::LengthError, I18n.t('errors.incorrect_length') if user_code.length != LENGTH_OF_SECRET_CODE
|
62
|
+
unless user_code[/\A[#{DIGIT_RANGE.first}-#{DIGIT_RANGE.last}]{#{LENGTH_OF_SECRET_CODE}}\z/]
|
63
|
+
raise GemCodebreaker::ValueRangeError
|
64
|
+
end
|
65
|
+
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
def secrete_code
|
70
|
+
@secrete_code ||= (1..LENGTH_OF_SECRET_CODE).map { rand DIGIT_RANGE }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class GameConfig
|
5
|
+
GAME_LEVEL = {
|
6
|
+
easy: {
|
7
|
+
attempts: 15,
|
8
|
+
hints: 2
|
9
|
+
},
|
10
|
+
medium: {
|
11
|
+
attempts: 10,
|
12
|
+
hints: 1
|
13
|
+
},
|
14
|
+
hell: {
|
15
|
+
attempts: 5,
|
16
|
+
hints: 1
|
17
|
+
},
|
18
|
+
strong_hell: {
|
19
|
+
attempts: 1,
|
20
|
+
hints: 0
|
21
|
+
}
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
include Validator
|
25
|
+
|
26
|
+
attr_reader :user_nick_name, :difficulty, :max_attempts, :max_hints, :user_attempts, :user_hints
|
27
|
+
|
28
|
+
def initialize(user, level)
|
29
|
+
check_difficulty(level)
|
30
|
+
@user_nick_name = user.nick_name
|
31
|
+
@difficulty = level
|
32
|
+
@max_attempts = GAME_LEVEL[level][:attempts]
|
33
|
+
@max_hints = GAME_LEVEL[level][:hints]
|
34
|
+
@user_attempts = 0
|
35
|
+
@user_hints = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def increase_user_attempts
|
39
|
+
@user_attempts += 1
|
40
|
+
end
|
41
|
+
|
42
|
+
def increase_user_hints
|
43
|
+
@user_hints += 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def attempts_used?
|
47
|
+
@user_attempts >= @max_attempts
|
48
|
+
end
|
49
|
+
|
50
|
+
def hints_used?
|
51
|
+
@user_hints >= @max_hints
|
52
|
+
end
|
53
|
+
|
54
|
+
def reset_user_attempts_hints
|
55
|
+
@user_attempts = 0
|
56
|
+
@user_hints = 0
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def check_difficulty(difficulty)
|
62
|
+
validate_inclusion(GAME_LEVEL.keys, difficulty)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class GameStatistic
|
5
|
+
attr_reader :user_nickname, :difficulty, :attempts_total, :attempts_used, :hints_total, :hints_used, :date
|
6
|
+
|
7
|
+
def initialize(game)
|
8
|
+
@user_nickname = game.config.user_nick_name
|
9
|
+
@difficulty = game.config.difficulty
|
10
|
+
@attempts_total = game.config.max_attempts
|
11
|
+
@attempts_used = game.config.user_attempts
|
12
|
+
@hints_total = game.config.max_hints
|
13
|
+
@hints_used = game.config.user_hints
|
14
|
+
@date = Date.today.to_s
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class SerializerDeserializer
|
5
|
+
def initialize
|
6
|
+
@file_name = 'game_statistic.yml'
|
7
|
+
@statistic_dir = 'data'
|
8
|
+
@storage_path = File.expand_path("./#{@statistic_dir}/#{@file_name}")
|
9
|
+
end
|
10
|
+
|
11
|
+
def data_dir_get
|
12
|
+
data_directory = File.dirname(@storage_path)
|
13
|
+
Dir.mkdir(data_directory) unless File.directory?(data_directory)
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_game_statistic
|
17
|
+
File.exist?(@storage_path) ? YAML.load_file(@storage_path) : []
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_game_statistic(game_statistic)
|
21
|
+
save_game_stat = game_statistic | load_game_statistic
|
22
|
+
File.open(@storage_path, 'w') do |file|
|
23
|
+
YAML.dump(save_game_stat, file)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
class User
|
5
|
+
include Validator
|
6
|
+
attr_reader :nick_name
|
7
|
+
|
8
|
+
def initialize(nick_name)
|
9
|
+
@nick_name = nick_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def nickname_valid?
|
13
|
+
validate_class(@nick_name, String)
|
14
|
+
validate_length(@nick_name, (3..20), I18n.t('errors.name_length'))
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'gem_codebreaker/config/i18n_locales'
|
5
|
+
require 'gem_codebreaker/version'
|
6
|
+
require 'gem_codebreaker/errors/length_error'
|
7
|
+
require 'gem_codebreaker/errors/no_attempts_error'
|
8
|
+
require 'gem_codebreaker/errors/no_hints_error'
|
9
|
+
require 'gem_codebreaker/errors/inclusion_error'
|
10
|
+
require 'gem_codebreaker/errors/value_range_error'
|
11
|
+
require 'gem_codebreaker/errors/wrong_class_error'
|
12
|
+
require 'gem_codebreaker/modules/validator'
|
13
|
+
require 'gem_codebreaker/classes/serializer_deserializer'
|
14
|
+
require 'gem_codebreaker/classes/user'
|
15
|
+
require 'gem_codebreaker/classes/code_maker'
|
16
|
+
require 'gem_codebreaker/classes/game_config'
|
17
|
+
require 'gem_codebreaker/classes/game_statistic'
|
18
|
+
require 'gem_codebreaker/classes/game'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
module Validator
|
5
|
+
def validate_inclusion(valid_values, check_value)
|
6
|
+
raise GemCodebreaker::InclusionError unless valid_values.include?(check_value)
|
7
|
+
end
|
8
|
+
|
9
|
+
def validate_class(user_code, klass)
|
10
|
+
raise GemCodebreaker::WrongClassError unless user_code.is_a?(klass)
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_length(check_value, valid_values, errors_message)
|
14
|
+
raise GemCodebreaker::LengthError, errors_message unless valid_values.member?(check_value.length)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: GemCodebreaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vasyl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fasterer
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.8.3
|
34
|
-
type: :
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.84.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-performance
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.6.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.6.1
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rubocop-rspec
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,7 +128,35 @@ email:
|
|
114
128
|
executables: []
|
115
129
|
extensions: []
|
116
130
|
extra_rdoc_files: []
|
117
|
-
files:
|
131
|
+
files:
|
132
|
+
- ".circleci/config.yml"
|
133
|
+
- ".fasterer.yml"
|
134
|
+
- ".gitignore"
|
135
|
+
- ".rspec"
|
136
|
+
- ".rubocop.yml"
|
137
|
+
- Gemfile
|
138
|
+
- Gemfile.lock
|
139
|
+
- LICENSE.txt
|
140
|
+
- README.md
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- gem_codebreaker.gemspec
|
144
|
+
- lib/gem_codebreaker/classes/code_maker.rb
|
145
|
+
- lib/gem_codebreaker/classes/game.rb
|
146
|
+
- lib/gem_codebreaker/classes/game_config.rb
|
147
|
+
- lib/gem_codebreaker/classes/game_statistic.rb
|
148
|
+
- lib/gem_codebreaker/classes/serializer_deserializer.rb
|
149
|
+
- lib/gem_codebreaker/classes/user.rb
|
150
|
+
- lib/gem_codebreaker/config/i18n_locales.rb
|
151
|
+
- lib/gem_codebreaker/errors/inclusion_error.rb
|
152
|
+
- lib/gem_codebreaker/errors/length_error.rb
|
153
|
+
- lib/gem_codebreaker/errors/no_attempts_error.rb
|
154
|
+
- lib/gem_codebreaker/errors/no_hints_error.rb
|
155
|
+
- lib/gem_codebreaker/errors/value_range_error.rb
|
156
|
+
- lib/gem_codebreaker/errors/wrong_class_error.rb
|
157
|
+
- lib/gem_codebreaker/gem_codebreaker.rb
|
158
|
+
- lib/gem_codebreaker/modules/validator.rb
|
159
|
+
- lib/gem_codebreaker/version.rb
|
118
160
|
homepage: https://github.com/vasivaas/GemCodebreaker
|
119
161
|
licenses:
|
120
162
|
- MIT
|