cb-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9356fd9f962bb5aa804f0958aa570a2911389c1581dd0444f36f47348b7283ea
4
+ data.tar.gz: 166ae692e5475313779e355471a303311b7aab05903fba10789074f26dd0596b
5
+ SHA512:
6
+ metadata.gz: 42a5c4f64ba179ec63ccab33aac5ecae75756c7db11a3def7453aa9c936d33c5617c7891fac1f80f47d42bd3967a88314e25f0098f876d56e705af517c60a474
7
+ data.tar.gz: 4273d181bf0a867764099f0851fa20a1ec35d06b53a59243eed88ed1ca15aab1e29b953f9406abbba5dbae32d29a91a018d2e5051dc373fdc03319ea963f267e
@@ -0,0 +1,44 @@
1
+ version: 2.1
2
+
3
+ orbs:
4
+ ruby: circleci/ruby@1.1.2
5
+
6
+ executors:
7
+ ruby:
8
+ docker:
9
+ - image: circleci/ruby:2.7.2
10
+
11
+ commands:
12
+ performance-check:
13
+ steps:
14
+ - run:
15
+ name: Run Fasterer
16
+ command: bundle exec fasterer
17
+
18
+ jobs:
19
+ testing:
20
+ executor: ruby
21
+ steps:
22
+ - checkout
23
+ - ruby/install-deps
24
+ - ruby/rspec-test
25
+ checking:
26
+ executor: ruby
27
+ steps:
28
+ - checkout
29
+ - ruby/install-deps
30
+ - ruby/rubocop-check
31
+ performance:
32
+ executor: ruby
33
+ steps:
34
+ - checkout
35
+ - ruby/install-deps
36
+ - performance-check
37
+
38
+ workflows:
39
+ version: 2
40
+ integration:
41
+ jobs:
42
+ - checking
43
+ - testing
44
+ - performance
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: 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
+
21
+ exclude_paths:
22
+ - 'vendor/**/*.rb'
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+
8
+ Layout/LineLength:
9
+ Max: 120
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - 'spec/**/*.rb'
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Style/FrozenStringLiteralComment:
19
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yakupov.dmitrij@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codebreaker.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cb-core (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ colorize (0.8.1)
11
+ diff-lcs (1.4.4)
12
+ docile (1.3.5)
13
+ fasterer (0.8.3)
14
+ colorize (~> 0.7)
15
+ ruby_parser (>= 3.14.1)
16
+ parallel (1.20.1)
17
+ parser (3.0.0.0)
18
+ ast (~> 2.4.1)
19
+ rainbow (3.0.0)
20
+ regexp_parser (2.1.1)
21
+ rexml (3.2.4)
22
+ rspec (3.10.0)
23
+ rspec-core (~> 3.10.0)
24
+ rspec-expectations (~> 3.10.0)
25
+ rspec-mocks (~> 3.10.0)
26
+ rspec-core (3.10.1)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-expectations (3.10.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-mocks (3.10.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-support (3.10.2)
35
+ rspec_junit_formatter (0.4.1)
36
+ rspec-core (>= 2, < 4, != 2.12.0)
37
+ rubocop (1.12.0)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.0.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml
43
+ rubocop-ast (>= 1.2.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 3.0)
46
+ rubocop-ast (1.4.1)
47
+ parser (>= 2.7.1.5)
48
+ rubocop-performance (1.10.2)
49
+ rubocop (>= 0.90.0, < 2.0)
50
+ rubocop-ast (>= 0.4.0)
51
+ rubocop-rspec (2.2.0)
52
+ rubocop (~> 1.0)
53
+ rubocop-ast (>= 1.1.0)
54
+ ruby-progressbar (1.11.0)
55
+ ruby_parser (3.15.1)
56
+ sexp_processor (~> 4.9)
57
+ sexp_processor (4.15.2)
58
+ simplecov (0.21.2)
59
+ docile (~> 1.1)
60
+ simplecov-html (~> 0.11)
61
+ simplecov_json_formatter (~> 0.1)
62
+ simplecov-html (0.12.3)
63
+ simplecov_json_formatter (0.1.2)
64
+ unicode-display_width (2.0.0)
65
+
66
+ PLATFORMS
67
+ ruby
68
+
69
+ DEPENDENCIES
70
+ cb-core!
71
+ fasterer (~> 0.8.3)
72
+ rspec (~> 3.0)
73
+ rspec_junit_formatter (~> 0.4)
74
+ rubocop (~> 1.12)
75
+ rubocop-performance (~> 1.10)
76
+ rubocop-rspec (~> 2.2)
77
+ simplecov (~> 0.21)
78
+
79
+ BUNDLED WITH
80
+ 2.2.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Dmytro Yakupov
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,43 @@
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 the created tag, 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. 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]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Codebreaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'codebreaker'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/codebreaker/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'cb-core'
5
+ spec.version = Codebreaker::VERSION
6
+ spec.authors = ['Dmytro Yakupov']
7
+ spec.email = ['yakupov.dmitrij@gmail.com']
8
+
9
+ spec.summary = 'Core'
10
+ spec.description = 'Contains a core logic.'
11
+ spec.homepage = 'https://github.com/YakupovDima/rubygarage-codebreaker-core'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/YakupovDima/rubygarage-codebreaker-core'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'fasterer', '~> 0.8.3'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4'
30
+ spec.add_development_dependency 'rubocop', '~> 1.12'
31
+ spec.add_development_dependency 'rubocop-performance', '~> 1.10'
32
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.2'
33
+ spec.add_development_dependency 'simplecov', '~> 0.21'
34
+ end
@@ -0,0 +1,20 @@
1
+ require_relative 'codebreaker/difficulty_factory'
2
+ require_relative 'codebreaker/difficulty_type'
3
+ require_relative 'codebreaker/difficulties/base'
4
+ require_relative 'codebreaker/difficulties/easy'
5
+ require_relative 'codebreaker/difficulties/medium'
6
+ require_relative 'codebreaker/difficulties/hell'
7
+ require_relative 'codebreaker/errors/game_over_error'
8
+ require_relative 'codebreaker/errors/invalid_difficulty_error'
9
+ require_relative 'codebreaker/errors/no_hints_error'
10
+ require_relative 'codebreaker/errors/validation_length_error'
11
+ require_relative 'codebreaker/game'
12
+ require_relative 'codebreaker/hint'
13
+ require_relative 'codebreaker/mixins/validatable'
14
+ require_relative 'codebreaker/player'
15
+ require_relative 'codebreaker/secret_code_generator'
16
+ require_relative 'codebreaker/secret_code_matcher'
17
+ require_relative 'codebreaker/version'
18
+
19
+ module Codebreaker
20
+ end
@@ -0,0 +1,13 @@
1
+ module Codebreaker
2
+ module Difficulties
3
+ class Base
4
+ def attempts
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def hints
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Codebreaker
2
+ module Difficulties
3
+ class Easy < Base
4
+ def attempts
5
+ 15
6
+ end
7
+
8
+ def hints
9
+ 2
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Codebreaker
2
+ module Difficulties
3
+ class Hell < Base
4
+ def attempts
5
+ 5
6
+ end
7
+
8
+ def hints
9
+ 1
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Codebreaker
2
+ module Difficulties
3
+ class Medium < Base
4
+ def attempts
5
+ 10
6
+ end
7
+
8
+ def hints
9
+ 1
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Codebreaker
2
+ class DifficultyFactory
3
+ def self.build(type)
4
+ case type
5
+ when DifficultyType::EASY
6
+ Difficulties::Easy.new
7
+ when DifficultyType::MEDIUM
8
+ Difficulties::Medium.new
9
+ when DifficultyType::HELL
10
+ Difficulties::Hell.new
11
+ else
12
+ raise Errors::InvalidDifficultyError, "unknown difficulty type #{type}"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Codebreaker
2
+ module DifficultyType
3
+ EASY = :easy
4
+ MEDIUM = :medium
5
+ HELL = :hell
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Codebreaker
2
+ module Errors
3
+ class GameOverError < StandardError
4
+ def initialize(msg = 'No attempts available or a player won the game')
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Codebreaker
2
+ module Errors
3
+ class InvalidDifficultyError < StandardError
4
+ def initialize(msg = 'Invalid difficulty type')
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Codebreaker
2
+ module Errors
3
+ class NoHintsError < StandardError
4
+ def initialize(msg = 'No hints available')
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Codebreaker
2
+ module Errors
3
+ class ValidationLengthError < StandardError
4
+ def initialize(msg = 'Invalid string length')
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ module Codebreaker
2
+ class Game
3
+ def initialize(player, difficulty_type)
4
+ @player = player
5
+ @secret_code = SecretCodeGenerator.new.call
6
+ @difficulty = DifficultyFactory.build(difficulty_type)
7
+ @hint = Hint.new(@difficulty, @secret_code)
8
+ @attempts_used = 0
9
+ @is_win = false
10
+ end
11
+
12
+ def take_hint
13
+ ensure_game_is_not_over
14
+ @hint.call
15
+ end
16
+
17
+ def guess_secret_code(guess)
18
+ ensure_game_is_not_over
19
+ @attempts_used += 1
20
+ matching = SecretCodeMatcher.new(@secret_code, guess).call
21
+ check_win(matching)
22
+ matching
23
+ end
24
+
25
+ def loose?
26
+ no_attempts? && !win?
27
+ end
28
+
29
+ def win?
30
+ @is_win
31
+ end
32
+
33
+ private
34
+
35
+ def game_over?
36
+ win? || loose?
37
+ end
38
+
39
+ def no_attempts?
40
+ @difficulty.attempts == @attempts_used
41
+ end
42
+
43
+ def ensure_game_is_not_over
44
+ raise Errors::GameOverError if game_over?
45
+ end
46
+
47
+ def check_win(combination)
48
+ @is_win = true if SecretCodeMatcher.win_combination?(combination)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,14 @@
1
+ module Codebreaker
2
+ class Hint
3
+ def initialize(difficulty, secret_code)
4
+ @difficulty = difficulty
5
+ @hints = secret_code.sample(difficulty.hints)
6
+ end
7
+
8
+ def call
9
+ raise Errors::NoHintsError if @hints.empty?
10
+
11
+ @hints.pop
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module Codebreaker
2
+ module Mixins
3
+ module Validatable
4
+ def validate
5
+ raise NotImplementedError
6
+ end
7
+
8
+ def validate_length(value, min, max)
9
+ return if value.instance_of?(String) && value.size >= min && value.size <= max
10
+
11
+ raise Codebreaker::Errors::ValidationLengthError, "invalid length '#{value}'. min=#{min}, max=#{max}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module Codebreaker
2
+ class Player
3
+ include Mixins::Validatable
4
+
5
+ NAME_MIN_SIZE = 3
6
+ NAME_MAX_SIZE = 20
7
+
8
+ attr_reader :name
9
+
10
+ def initialize(name)
11
+ @name = name
12
+ validate
13
+ end
14
+
15
+ private
16
+
17
+ def validate
18
+ validate_length(@name, NAME_MIN_SIZE, NAME_MAX_SIZE)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module Codebreaker
2
+ class SecretCodeGenerator
3
+ LENGTH = 4
4
+ RANGE = (1..6).freeze
5
+
6
+ def call
7
+ Array.new(LENGTH) { rand(RANGE) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ module Codebreaker
2
+ class SecretCodeMatcher
3
+ EXACT_MATCH_SYMBOL = '+'.freeze
4
+ DIGIT_MATCH_SYMBOL = '-'.freeze
5
+ WIN_COMBINATION = '++++'.freeze
6
+
7
+ def self.win_combination?(combination)
8
+ combination == WIN_COMBINATION
9
+ end
10
+
11
+ def initialize(secret_code, guess_code)
12
+ @secret_code = secret_code.dup
13
+ @guess_code = guess_code.dup
14
+ end
15
+
16
+ def call
17
+ exact_match + digit_match
18
+ end
19
+
20
+ private
21
+
22
+ def exact_match
23
+ result = ''
24
+ @guess_code.each_with_index do |digit, index|
25
+ next unless digit == @secret_code[index]
26
+
27
+ result << EXACT_MATCH_SYMBOL
28
+ @guess_code[index] = nil
29
+ @secret_code[index] = nil
30
+ end
31
+ result
32
+ end
33
+
34
+ def digit_match
35
+ result = ''
36
+ @guess_code.compact.each do |digit|
37
+ found_index = @secret_code.index(digit)
38
+
39
+ next unless found_index
40
+
41
+ result << DIGIT_MATCH_SYMBOL
42
+ @secret_code[found_index] = nil
43
+ end
44
+ result
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cb-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmytro Yakupov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fasterer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec_junit_formatter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.21'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.21'
111
+ description: Contains a core logic.
112
+ email:
113
+ - yakupov.dmitrij@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".circleci/config.yml"
119
+ - ".fasterer.yml"
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".rubocop.yml"
123
+ - ".ruby-version"
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - bin/console
130
+ - bin/setup
131
+ - codebreaker.gemspec
132
+ - lib/codebreaker.rb
133
+ - lib/codebreaker/difficulties/base.rb
134
+ - lib/codebreaker/difficulties/easy.rb
135
+ - lib/codebreaker/difficulties/hell.rb
136
+ - lib/codebreaker/difficulties/medium.rb
137
+ - lib/codebreaker/difficulty_factory.rb
138
+ - lib/codebreaker/difficulty_type.rb
139
+ - lib/codebreaker/errors/game_over_error.rb
140
+ - lib/codebreaker/errors/invalid_difficulty_error.rb
141
+ - lib/codebreaker/errors/no_hints_error.rb
142
+ - lib/codebreaker/errors/validation_length_error.rb
143
+ - lib/codebreaker/game.rb
144
+ - lib/codebreaker/hint.rb
145
+ - lib/codebreaker/mixins/validatable.rb
146
+ - lib/codebreaker/player.rb
147
+ - lib/codebreaker/secret_code_generator.rb
148
+ - lib/codebreaker/secret_code_matcher.rb
149
+ - lib/codebreaker/version.rb
150
+ homepage: https://github.com/YakupovDima/rubygarage-codebreaker-core
151
+ licenses:
152
+ - MIT
153
+ metadata:
154
+ homepage_uri: https://github.com/YakupovDima/rubygarage-codebreaker-core
155
+ source_code_uri: https://github.com/YakupovDima/rubygarage-codebreaker-core
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.7.0
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubygems_version: 3.1.4
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Core
175
+ test_files: []