GemCodebreaker 0.1.6.1 → 0.1.7
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 +67 -0
- data/.fasterer.yml +21 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +15 -0
- data/GemCodebreaker.gemspec +37 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +74 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/GemCodebreaker/classes/codemaker.rb +42 -0
- data/lib/GemCodebreaker/classes/game.rb +83 -0
- data/lib/GemCodebreaker/classes/game_config.rb +18 -0
- data/lib/GemCodebreaker/classes/game_statistic.rb +17 -0
- data/lib/GemCodebreaker/classes/user.rb +19 -0
- data/lib/GemCodebreaker/config/i18n_locales.rb +5 -0
- data/lib/GemCodebreaker/errors/incorrect_length_error.rb +10 -0
- data/lib/GemCodebreaker/errors/name_length_error.rb +10 -0
- data/lib/GemCodebreaker/errors/no_attempts_error.rb +10 -0
- data/lib/GemCodebreaker/errors/no_hints_error.rb +10 -0
- data/lib/GemCodebreaker/errors/unknow_level_error.rb +10 -0
- data/lib/GemCodebreaker/errors/value_range_error.rb +10 -0
- data/lib/GemCodebreaker/errors/wrong_class_error.rb +10 -0
- data/lib/GemCodebreaker/gem_codebreaker.rb +23 -0
- data/lib/GemCodebreaker/modules/game_configurations.rb +18 -0
- data/lib/GemCodebreaker/modules/serializer_deserializer.rb +26 -0
- data/lib/GemCodebreaker/modules/validator.rb +35 -0
- data/lib/GemCodebreaker/version.rb +5 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 165c0f083158238518f77331d6deac350b0e04ceef650c360c76ef5b8f10fb9e
|
4
|
+
data.tar.gz: 98b51e7044010d4bb3b30a906066f076ebec34202fa5c53ac449a5e45094f1bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 382808a328022ed926e26f7dd2fbda09bebeb68e2cd491f7cc1dc1fd407e288564bdbf90c7affccadd8c1f9c032f5d332e348d469f6034c22be265e762fae4f6
|
7
|
+
data.tar.gz: f84fa6609821fc98ab0054073f2ffc96d6138dde50472575af417da3b73ceb5e7b48f6666eee6c47712f94545764c7841c74046b270c46a1040cec60b9cda987
|
@@ -0,0 +1,67 @@
|
|
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: |
|
42
|
+
mkdir /tmp/test-results
|
43
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
|
44
|
+
bundle exec rspec --format progress \
|
45
|
+
--out /tmp/test-results/rspec.xml \
|
46
|
+
$TEST_FILES
|
47
|
+
|
48
|
+
jobs:
|
49
|
+
lintering:
|
50
|
+
executor: default
|
51
|
+
steps:
|
52
|
+
- setup_environment
|
53
|
+
- run_linters
|
54
|
+
run_specs:
|
55
|
+
executor: default
|
56
|
+
steps:
|
57
|
+
- setup_environment
|
58
|
+
- run_specs
|
59
|
+
|
60
|
+
workflows:
|
61
|
+
version: 2
|
62
|
+
build:
|
63
|
+
jobs:
|
64
|
+
- lintering
|
65
|
+
- run_specs:
|
66
|
+
requires:
|
67
|
+
- 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,15 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
TargetRubyVersion: 2.7.1
|
6
|
+
NewCops: enable
|
7
|
+
|
8
|
+
Layout/LineLength:
|
9
|
+
Max: 120
|
10
|
+
Metrics/ModuleLength:
|
11
|
+
Max: 270
|
12
|
+
RSpec/MessageSpies:
|
13
|
+
EnforcedStyle: receive
|
14
|
+
Metrics/BlockLength:
|
15
|
+
ExcludedMethods: ['describe', 'context']
|
@@ -0,0 +1,37 @@
|
|
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/GemCodebreaker/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_development_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-rspec', '~> 1.41.0'
|
36
|
+
spec.add_development_dependency 'simplecov', '~> 0.18.5'
|
37
|
+
end
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
GemCodebreaker (0.1.7)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.1)
|
10
|
+
colorize (0.8.1)
|
11
|
+
concurrent-ruby (1.1.6)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
docile (1.3.2)
|
14
|
+
fasterer (0.8.3)
|
15
|
+
colorize (~> 0.7)
|
16
|
+
ruby_parser (>= 3.14.1)
|
17
|
+
i18n (1.8.3)
|
18
|
+
concurrent-ruby (~> 1.0)
|
19
|
+
parallel (1.19.2)
|
20
|
+
parser (2.7.1.3)
|
21
|
+
ast (~> 2.4.0)
|
22
|
+
rainbow (3.0.0)
|
23
|
+
rake (13.0.1)
|
24
|
+
rexml (3.2.4)
|
25
|
+
rspec (3.7.0)
|
26
|
+
rspec-core (~> 3.7.0)
|
27
|
+
rspec-expectations (~> 3.7.0)
|
28
|
+
rspec-mocks (~> 3.7.0)
|
29
|
+
rspec-core (3.7.1)
|
30
|
+
rspec-support (~> 3.7.0)
|
31
|
+
rspec-expectations (3.7.0)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.7.0)
|
34
|
+
rspec-mocks (3.7.0)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.7.0)
|
37
|
+
rspec-support (3.7.1)
|
38
|
+
rubocop (0.84.0)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 2.7.0.1)
|
41
|
+
rainbow (>= 2.2.2, < 4.0)
|
42
|
+
rexml
|
43
|
+
rubocop-ast (>= 0.0.3)
|
44
|
+
ruby-progressbar (~> 1.7)
|
45
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
46
|
+
rubocop-ast (0.0.3)
|
47
|
+
parser (>= 2.7.0.1)
|
48
|
+
rubocop-rspec (1.41.0)
|
49
|
+
rubocop (>= 0.68.1)
|
50
|
+
ruby-progressbar (1.10.1)
|
51
|
+
ruby_parser (3.14.2)
|
52
|
+
sexp_processor (~> 4.9)
|
53
|
+
sexp_processor (4.15.0)
|
54
|
+
simplecov (0.18.5)
|
55
|
+
docile (~> 1.1)
|
56
|
+
simplecov-html (~> 0.11)
|
57
|
+
simplecov-html (0.12.2)
|
58
|
+
unicode-display_width (1.7.0)
|
59
|
+
|
60
|
+
PLATFORMS
|
61
|
+
ruby
|
62
|
+
|
63
|
+
DEPENDENCIES
|
64
|
+
GemCodebreaker!
|
65
|
+
fasterer (~> 0.8.3)
|
66
|
+
i18n (~> 1.8.3)
|
67
|
+
rake (~> 13.0.1)
|
68
|
+
rspec (~> 3.7.0)
|
69
|
+
rubocop (~> 0.84.0)
|
70
|
+
rubocop-rspec (~> 1.41.0)
|
71
|
+
simplecov (~> 0.18.5)
|
72
|
+
|
73
|
+
BUNDLED WITH
|
74
|
+
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,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Marker class for game
|
5
|
+
class CodeMaker
|
6
|
+
attr_reader :plus_match, :minus_match, :empty_match
|
7
|
+
|
8
|
+
def initialize(game_code, user_code)
|
9
|
+
@game_code = game_code
|
10
|
+
@user_code = user_code
|
11
|
+
@plus_match = 0
|
12
|
+
@minus_match = 0
|
13
|
+
@empty_match = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def calculate_result_code
|
17
|
+
count_plus_marker
|
18
|
+
count_minus_and_empty_answer
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def count_plus_marker
|
24
|
+
data = @game_code.zip(@user_code)
|
25
|
+
data.delete_if { |game_code_item, user_code_item| @plus_match += 1 if game_code_item == user_code_item }
|
26
|
+
@game_code, @user_code = data.transpose
|
27
|
+
end
|
28
|
+
|
29
|
+
def count_minus_and_empty_answer
|
30
|
+
return if @game_code.nil?
|
31
|
+
|
32
|
+
@game_code.each do |secret_code_item|
|
33
|
+
if @user_code.include?(secret_code_item)
|
34
|
+
@minus_match += 1
|
35
|
+
@user_code.delete(secret_code_item)
|
36
|
+
else
|
37
|
+
@empty_match += 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Game class
|
5
|
+
class Game
|
6
|
+
LENGTH_OF_SECRET_CODE = 4
|
7
|
+
DIGIT_RANGE = (1..6).freeze
|
8
|
+
|
9
|
+
include GameConfigurations
|
10
|
+
include Validator
|
11
|
+
attr_reader :secrete_code, :statistic, :matcher_hash
|
12
|
+
attr_accessor :config
|
13
|
+
|
14
|
+
def initialize(user, level)
|
15
|
+
@config = GameConfig.new(user, level.to_sym)
|
16
|
+
@statistic = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def check_configuration
|
20
|
+
validate_game_level(@config.difficulty)
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_secret_code
|
24
|
+
@secrete_code = (1..LENGTH_OF_SECRET_CODE).map { rand DIGIT_RANGE }
|
25
|
+
end
|
26
|
+
|
27
|
+
def guess_valid?(user_code)
|
28
|
+
validate_string(user_code)
|
29
|
+
validate_guess_length if user_code.length != LENGTH_OF_SECRET_CODE
|
30
|
+
validate_chars_range unless user_code[/\A[1-6]{4}\z/]
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def guess_start(input_code)
|
35
|
+
validate_number_of_attempts if @config.user_attempts >= @config.max_attempts
|
36
|
+
|
37
|
+
@config.user_attempts += 1
|
38
|
+
user_code = convert_string_to_array(input_code)
|
39
|
+
code_marker = CodeMaker.new(@secrete_code, user_code)
|
40
|
+
code_marker.calculate_result_code
|
41
|
+
@matcher_hash = { plus: code_marker.plus_match, minus: code_marker.minus_match, empty: code_marker.empty_match }
|
42
|
+
end
|
43
|
+
|
44
|
+
def win_game?
|
45
|
+
@matcher_hash[:plus] == LENGTH_OF_SECRET_CODE
|
46
|
+
end
|
47
|
+
|
48
|
+
def game_over?
|
49
|
+
(@config.user_attempts >= @config.max_attempts) && win_game? == false
|
50
|
+
end
|
51
|
+
|
52
|
+
def hints_used?
|
53
|
+
@config.user_hints >= @config.max_hints
|
54
|
+
end
|
55
|
+
|
56
|
+
def hint
|
57
|
+
validate_number_of_hints if hints_used?
|
58
|
+
|
59
|
+
@config.user_hints += 1
|
60
|
+
@secrete_code.sample
|
61
|
+
end
|
62
|
+
|
63
|
+
def reset
|
64
|
+
@config.user_attempts = 0
|
65
|
+
@config.user_hints = 0
|
66
|
+
generate_secret_code
|
67
|
+
end
|
68
|
+
|
69
|
+
def save_user_statistic
|
70
|
+
@statistic << current_user_statistic
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def current_user_statistic
|
76
|
+
GameStatistic.new(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
def convert_string_to_array(user_code)
|
80
|
+
user_code.chars.map(&:to_i)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Configuration class for game
|
5
|
+
class GameConfig
|
6
|
+
include GameConfigurations
|
7
|
+
attr_accessor :user_nick_name, :difficulty, :max_attempts, :max_hints, :user_attempts, :user_hints
|
8
|
+
|
9
|
+
def initialize(user, level)
|
10
|
+
@user_nick_name = user.nick_name
|
11
|
+
@difficulty = level
|
12
|
+
@max_attempts = GAME_LEVEL[level][:attempts]
|
13
|
+
@max_hints = GAME_LEVEL[level][:hints]
|
14
|
+
@user_attempts = 0
|
15
|
+
@user_hints = 0
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Game statistic class
|
5
|
+
class GameStatistic
|
6
|
+
attr_reader :user_nickname, :difficulty, :attempts_total, :attempts_used, :hints_total, :hints_used
|
7
|
+
|
8
|
+
def initialize(game)
|
9
|
+
@user_nickname = game.config.user_nick_name
|
10
|
+
@difficulty = game.config.difficulty
|
11
|
+
@attempts_total = game.config.max_attempts
|
12
|
+
@attempts_used = game.config.user_attempts
|
13
|
+
@hints_total = game.config.max_hints
|
14
|
+
@hints_used = game.config.user_hints
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# User class for game
|
5
|
+
class User
|
6
|
+
include Validator
|
7
|
+
attr_accessor :nick_name
|
8
|
+
|
9
|
+
def initialize(nick_name)
|
10
|
+
@nick_name = nick_name
|
11
|
+
end
|
12
|
+
|
13
|
+
def nickname_valid?
|
14
|
+
validate_string(@nick_name)
|
15
|
+
validate_nickname_length(@nick_name)
|
16
|
+
true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'GemCodebreaker/config/i18n_locales'
|
4
|
+
require 'GemCodebreaker/version'
|
5
|
+
require 'GemCodebreaker/errors/incorrect_length_error'
|
6
|
+
require 'GemCodebreaker/errors/name_length_error'
|
7
|
+
require 'GemCodebreaker/errors/no_attempts_error'
|
8
|
+
require 'GemCodebreaker/errors/no_hints_error'
|
9
|
+
require 'GemCodebreaker/errors/unknow_level_error'
|
10
|
+
require 'GemCodebreaker/errors/value_range_error'
|
11
|
+
require 'GemCodebreaker/errors/wrong_class_error'
|
12
|
+
require 'GemCodebreaker/modules/validator'
|
13
|
+
require 'GemCodebreaker/modules/serializer_deserializer'
|
14
|
+
require 'GemCodebreaker/modules/game_configurations'
|
15
|
+
require 'GemCodebreaker/classes/user'
|
16
|
+
require 'GemCodebreaker/classes/codemaker'
|
17
|
+
require 'GemCodebreaker/classes/game_config'
|
18
|
+
require 'GemCodebreaker/classes/game_statistic'
|
19
|
+
require 'GemCodebreaker/classes/game'
|
20
|
+
|
21
|
+
module GemCodebreaker
|
22
|
+
class Error < StandardError; end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
module GameConfigurations
|
5
|
+
GAME_LEVEL = { easy:
|
6
|
+
{ attempts: 15,
|
7
|
+
hints: 2 },
|
8
|
+
medium:
|
9
|
+
{ attempts: 10,
|
10
|
+
hints: 1 },
|
11
|
+
hell:
|
12
|
+
{ attempts: 5,
|
13
|
+
hints: 1 },
|
14
|
+
strong_hell:
|
15
|
+
{ attempts: 1,
|
16
|
+
hints: 0 } }.freeze
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Serialize and Deserialize data for game statistic
|
5
|
+
module SerializerDeserializer
|
6
|
+
FILE_NAME = 'game_statistic.yml'
|
7
|
+
STATISTIC_DIR = 'data'
|
8
|
+
STORAGE_PATH = File.expand_path("./#{STATISTIC_DIR}/#{FILE_NAME}")
|
9
|
+
|
10
|
+
def data_dir_get
|
11
|
+
data_directory = File.dirname(STORAGE_PATH)
|
12
|
+
Dir.mkdir(data_directory) unless File.directory?(data_directory)
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_game_statistic
|
16
|
+
File.exist?(STORAGE_PATH) ? YAML.load_file(STORAGE_PATH) : []
|
17
|
+
end
|
18
|
+
|
19
|
+
def save_game_statistic(game_statistic)
|
20
|
+
save_game_stat = game_statistic | load_game_statistic
|
21
|
+
File.open(STORAGE_PATH, 'w') do |file|
|
22
|
+
YAML.dump(save_game_stat, file)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GemCodebreaker
|
4
|
+
# Validator
|
5
|
+
module Validator
|
6
|
+
def validate_game_level(game_level)
|
7
|
+
level = %i[easy medium hell strong_hell]
|
8
|
+
raise GemCodebreaker::UnknowLevelError unless level.include?(game_level)
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_number_of_hints
|
12
|
+
raise GemCodebreaker::NoHintsError
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_number_of_attempts
|
16
|
+
raise GemCodebreaker::NoAttemptsError
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_string(object)
|
20
|
+
raise GemCodebreaker::WrongClassError unless object.is_a?(String)
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate_guess_length
|
24
|
+
raise GemCodebreaker::IncorrectLengthError
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate_chars_range
|
28
|
+
raise GemCodebreaker::ValueRangeError
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate_nickname_length(nickname)
|
32
|
+
raise GemCodebreaker::NameLengthError if nickname.length < 3 || nickname.length > 20
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: GemCodebreaker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vasyl
|
@@ -114,7 +114,37 @@ email:
|
|
114
114
|
executables: []
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
|
-
files:
|
117
|
+
files:
|
118
|
+
- ".circleci/config.yml"
|
119
|
+
- ".fasterer.yml"
|
120
|
+
- ".gitignore"
|
121
|
+
- ".rspec"
|
122
|
+
- ".rubocop.yml"
|
123
|
+
- GemCodebreaker.gemspec
|
124
|
+
- Gemfile
|
125
|
+
- Gemfile.lock
|
126
|
+
- LICENSE.txt
|
127
|
+
- README.md
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/GemCodebreaker/classes/codemaker.rb
|
131
|
+
- lib/GemCodebreaker/classes/game.rb
|
132
|
+
- lib/GemCodebreaker/classes/game_config.rb
|
133
|
+
- lib/GemCodebreaker/classes/game_statistic.rb
|
134
|
+
- lib/GemCodebreaker/classes/user.rb
|
135
|
+
- lib/GemCodebreaker/config/i18n_locales.rb
|
136
|
+
- lib/GemCodebreaker/errors/incorrect_length_error.rb
|
137
|
+
- lib/GemCodebreaker/errors/name_length_error.rb
|
138
|
+
- lib/GemCodebreaker/errors/no_attempts_error.rb
|
139
|
+
- lib/GemCodebreaker/errors/no_hints_error.rb
|
140
|
+
- lib/GemCodebreaker/errors/unknow_level_error.rb
|
141
|
+
- lib/GemCodebreaker/errors/value_range_error.rb
|
142
|
+
- lib/GemCodebreaker/errors/wrong_class_error.rb
|
143
|
+
- lib/GemCodebreaker/gem_codebreaker.rb
|
144
|
+
- lib/GemCodebreaker/modules/game_configurations.rb
|
145
|
+
- lib/GemCodebreaker/modules/serializer_deserializer.rb
|
146
|
+
- lib/GemCodebreaker/modules/validator.rb
|
147
|
+
- lib/GemCodebreaker/version.rb
|
118
148
|
homepage: https://github.com/vasivaas/GemCodebreaker
|
119
149
|
licenses:
|
120
150
|
- MIT
|