codebreaker-mikhail 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ae855fc9138d9fcaad1c52d2699a384618b67ffa25ef37e3f39cefb60a45fec
4
+ data.tar.gz: 2a1363abbd28057e78633c4c0f03673e798da27eced6606088211158baa86d58
5
+ SHA512:
6
+ metadata.gz: 3e587f7e5c1e708485ab6589ef722d7a584668715f3b2002c1a3167cfb93cc87027cb862a199ec419e11c4b3d68f7851932542618673e81d904bf075707c0098
7
+ data.tar.gz: 6b8d8c0fdbd3c977cd7d2ebdc138f0793f5b62372b2a1012ba3c326eddc5cdadfe274271a81d34c7c3570fb08f8b062412a3619e6b755083e37436b64394dda5
@@ -0,0 +1,66 @@
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:3.0.0
9
+ environment:
10
+ BUNDLER_VERSION: 2.2.3
11
+
12
+ cashes:
13
+ - &bundle_caches_v1 v2-repo-{{ checksum "Gemfile.lock" }}
14
+ - &bundle_cache v2-repo-
15
+
16
+ commands:
17
+ defaults:
18
+ steps:
19
+ - checkout
20
+ - restore_cache:
21
+ keys:
22
+ - *bundle_caches_v1
23
+ - *bundle_cache
24
+ - run: bundle install --path vendor/bundle
25
+ - save_cache:
26
+ key: *bundle_cache
27
+ paths:
28
+ - vendor/bundle
29
+ run_linters:
30
+ description: command to start linters
31
+ steps:
32
+ - run:
33
+ name: rubucop
34
+ command: bundle exec rubocop
35
+ - run:
36
+ name: fasterer
37
+ command: bundle exec fasterer
38
+ run_specs:
39
+ steps:
40
+ - run:
41
+ name: run specs
42
+ command: bundle exec rspec
43
+
44
+ jobs:
45
+ lintering:
46
+ executor: default
47
+ steps:
48
+ - defaults
49
+ - run_linters
50
+ run_specs:
51
+ executor: default
52
+ steps:
53
+ - defaults
54
+ - run_specs
55
+
56
+ workflows:
57
+ version: 2.1
58
+ build:
59
+ jobs:
60
+ - lintering
61
+ - run_specs:
62
+ filters:
63
+ branches:
64
+ only: develop
65
+ requires:
66
+ - lintering
data/.fasterer.yml ADDED
@@ -0,0 +1,23 @@
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'
23
+ - 'db/schema.rb'
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ Style/FrozenStringLiteralComment:
4
+ Enabled: false
5
+ Style/Documentation:
6
+ Enabled: false
7
+ Metrics/ParameterLists:
8
+ Max: 5
9
+ MaxOptionalParameters: 5
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+ Metrics/LineLength:
13
+ Max: 120
14
+ Lint/MissingSuper:
15
+ Enabled: false
data/.rubocop.yml ADDED
@@ -0,0 +1,43 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ AllCops:
13
+ NewCops: enable
14
+ Exclude:
15
+ - vendor/bundle/**/*
16
+
17
+ Style/FrozenStringLiteralComment:
18
+ Enabled: false
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ Metrics/ParameterLists:
24
+ Max: 5
25
+ MaxOptionalParameters: 5
26
+
27
+ Metrics/AbcSize:
28
+ Enabled: false
29
+
30
+ Layout/LineLength:
31
+ Max: 120
32
+
33
+ Metrics/BlockLength:
34
+ Exclude:
35
+ - 'spec/**/*'
36
+ - 'codebreaker.gemspec'
37
+
38
+ Lint/MissingSuper:
39
+ Enabled: false
40
+
41
+ RSpec/ScatteredSetup:
42
+ Enabled: false
43
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
@@ -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 booozyby@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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in codebraker.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker-mikhail (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ colorize (0.8.1)
11
+ diff-lcs (1.5.0)
12
+ docile (1.4.0)
13
+ fasterer (0.10.0)
14
+ colorize (~> 0.7)
15
+ ruby_parser (>= 3.19.1)
16
+ json (2.6.2)
17
+ parallel (1.22.1)
18
+ parser (3.1.2.1)
19
+ ast (~> 2.4.1)
20
+ rainbow (3.1.1)
21
+ rake (10.5.0)
22
+ regexp_parser (2.5.0)
23
+ rexml (3.2.5)
24
+ rspec (3.11.0)
25
+ rspec-core (~> 3.11.0)
26
+ rspec-expectations (~> 3.11.0)
27
+ rspec-mocks (~> 3.11.0)
28
+ rspec-core (3.11.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-expectations (3.11.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-mocks (3.11.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-support (3.11.0)
37
+ rubocop (1.35.1)
38
+ json (~> 2.3)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.1.2.1)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8, < 3.0)
43
+ rexml (>= 3.2.5, < 4.0)
44
+ rubocop-ast (>= 1.20.1, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 3.0)
47
+ rubocop-ast (1.21.0)
48
+ parser (>= 3.1.1.0)
49
+ rubocop-rspec (2.5.0)
50
+ rubocop (~> 1.19)
51
+ ruby-progressbar (1.11.0)
52
+ ruby_parser (3.19.1)
53
+ sexp_processor (~> 4.16)
54
+ sexp_processor (4.16.1)
55
+ simplecov (0.21.2)
56
+ docile (~> 1.1)
57
+ simplecov-html (~> 0.11)
58
+ simplecov_json_formatter (~> 0.1)
59
+ simplecov-html (0.12.3)
60
+ simplecov_json_formatter (0.1.4)
61
+ unicode-display_width (2.2.0)
62
+
63
+ PLATFORMS
64
+ x86_64-linux
65
+
66
+ DEPENDENCIES
67
+ bundler (~> 2.2.3)
68
+ codebreaker-mikhail!
69
+ fasterer
70
+ rake (~> 10.0)
71
+ rspec (~> 3.11)
72
+ rubocop (~> 1.30)
73
+ rubocop-rspec (~> 2.5.0)
74
+ simplecov
75
+
76
+ BUNDLED WITH
77
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 MikhailBogun
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. 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/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
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
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/codebreaker/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "codebreaker-mikhail"
7
+ spec.version = Codebreaker::VERSION
8
+ spec.authors = ["Mikhail Bogun"]
9
+ spec.email = ["mikebogun@gmail.com"]
10
+ spec.summary = "Codebreaker game"
11
+ spec.description = "Game for console"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
14
+ spec.metadata["homepage_uri"] = "https://github.com/MikhailBogun/Codebreaker"
15
+ spec.metadata["source_code_uri"] = "https://github.com/MikhailBogun/Codebreaker"
16
+ spec.metadata["changelog_uri"] = "https://github.com/MikhailBogun/Codebreaker"
17
+ spec.metadata["rubygems_mfa_required"] = "false"
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.2.3"
27
+ spec.add_development_dependency "fasterer"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.11"
30
+ spec.add_development_dependency "rubocop", "~> 1.30"
31
+ spec.add_development_dependency("rubocop-rspec", "~> 2.5.0")
32
+ spec.add_development_dependency "simplecov"
33
+ spec.metadata["rubygems_mfa_required"] = "true"
34
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "game_const"
2
+ require_relative "matrix"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SecretCodeGenerator
4
+ def self.call
5
+ (1..4).map { rand(1..6) }
6
+ end
7
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ module FileStore
5
+ FILE_DIRECTORY = "statistics"
6
+ FILE_NAME = "statistics.yml"
7
+
8
+ def save_file(game)
9
+ validate_state(game.state, :win)
10
+ score = load_file
11
+ score << fill_game_data(game)
12
+ File.write(storage_path, YAML.dump({ codebrakers: score }))
13
+ end
14
+
15
+ def load_file
16
+ create_storage unless storage_exists?
17
+ (YAML.load_file(storage_path) || {})[:codebrakers] || []
18
+ end
19
+
20
+ def statistics
21
+ load_file.each.sort_by { |game| [game[:attempts], game[:used_hints], game[:used_attempts]] }
22
+ end
23
+
24
+ private
25
+
26
+ def create_storage
27
+ FileUtils.mkdir_p(FILE_DIRECTORY)
28
+ File.open(storage_path, "w") unless File.exist?(storage_path)
29
+ end
30
+
31
+ def storage_path
32
+ File.join(FILE_DIRECTORY, FILE_NAME)
33
+ end
34
+
35
+ def storage_exists?
36
+ Dir.exist?(FILE_DIRECTORY) && File.exist?(storage_path)
37
+ end
38
+
39
+ def fill_game_data(game)
40
+ game_statistic = {}
41
+ game_statistic[:name] = game.user.name
42
+ game_statistic[:difficulty] = game.user.difficulty
43
+ game_statistic[:attempts] = Constants::DIFFICULTIES[game.user.difficulty][:attempts]
44
+ game_statistic[:hints] = Constants::DIFFICULTIES[game.user.difficulty][:hints]
45
+ game_statistic.merge(user_data(game))
46
+ end
47
+
48
+ def user_data(game)
49
+ user = {}
50
+ user[:used_attempts] = used_attempts(game)
51
+ user[:used_hints] = used_hints(game)
52
+ user
53
+ end
54
+
55
+ def used_attempts(game)
56
+ Constants::DIFFICULTIES[game.user.difficulty][:attempts] - game.user.attempts
57
+ end
58
+
59
+ def used_hints(game)
60
+ Constants::DIFFICULTIES[game.user.difficulty][:hints] - game.user.hints
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Game
5
+ include Validation
6
+
7
+ attr_reader :hints, :user, :state, :secret_code
8
+
9
+ def initialize(user)
10
+ @user = user
11
+ @state = Constants::START_GAME
12
+ @secret_code = SecretCodeGenerator.call
13
+ end
14
+
15
+ def start
16
+ validate_state(@state, Constants::START_GAME)
17
+ @copied_code = @secret_code.dup
18
+ @state = Constants::IN_GAME
19
+ end
20
+
21
+ def generate_matrix(input)
22
+ validate_state(@state, Constants::IN_GAME)
23
+ validate_guess(input)
24
+ user.decrement_attemts
25
+ MatrixGenerator.new(input, @secret_code).call
26
+ end
27
+
28
+ def hints?
29
+ (user.hints <= Constants::DIFFICULTIES[@user.difficulty][:hints]) && user.hints.positive?
30
+ end
31
+
32
+ def attempts?
33
+ (user.attempts <= Constants::DIFFICULTIES[@user.difficulty][:attempts]) && user.attempts.positive?
34
+ end
35
+
36
+ def win?(result)
37
+ return false unless result == @secret_code
38
+
39
+ @state = Constants::WIN
40
+ true
41
+ end
42
+
43
+ def lose?
44
+ return unless user.attempts.zero?
45
+
46
+ @state = Constants::LOSE
47
+ true
48
+ end
49
+
50
+ def show_difficulties
51
+ Constants::DIFFICULTIES
52
+ end
53
+
54
+ def use_hint
55
+ return if @copied_code.empty?
56
+
57
+ hint = @copied_code.sample
58
+ @copied_code.join.sub!(hint.to_s, "")
59
+ user.decrement_hints
60
+ hint
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ module Codebreaker
2
+ module Constants
3
+ DIFFICULTIES = {
4
+ easy: { attempts: 15, hints: 2 },
5
+ middle: { attempts: 10, hints: 1 },
6
+ hard: { attempts: 5, hints: 1 }
7
+ }.freeze
8
+ NAME_LENGTH = (3..20)
9
+ START_GAME = :start
10
+ IN_GAME = :game
11
+ WIN = :win
12
+ LOSE = :lose
13
+ end
14
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class MatrixGenerator
5
+ def initialize(input, secret_code)
6
+ @input = input
7
+ @secret_code = secret_code
8
+ end
9
+
10
+ def call
11
+ input, text_code, extra_char = check_in_same_position(@input)
12
+ check_in_different_position(input, text_code, extra_char)
13
+ end
14
+
15
+ private
16
+
17
+ def select_code(input)
18
+ (0...4).select { |index| input[index] == @secret_code[index].to_s }
19
+ end
20
+
21
+ def check_in_same_position(input)
22
+ text_code = ""
23
+ extra_char = ""
24
+ select_code(input).reverse_each do |index|
25
+ text_code += "+"
26
+ extra_char += input.slice(index)
27
+ end
28
+ [input, text_code, extra_char]
29
+ end
30
+
31
+ def check_in_different_position(input, text_code = "", extra_char = "")
32
+ input.each_char do |item|
33
+ if include_number?(item, extra_char)
34
+ text_code += "-"
35
+ extra_char += item
36
+ end
37
+ end
38
+ text_code
39
+ end
40
+
41
+ def include_number?(number, char)
42
+ @secret_code.include?(number.to_i) && !char.include?(number)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class User
5
+ include Validation
6
+
7
+ attr_reader :name, :attempts, :hints, :difficulty
8
+
9
+ def initialize(name)
10
+ validate_name(name)
11
+ @name = name
12
+ end
13
+
14
+ def assign_user_config(difficulty)
15
+ @difficulty = difficulty
16
+ @attempts = Constants::DIFFICULTIES[@difficulty][:attempts]
17
+ @hints = Constants::DIFFICULTIES[@difficulty][:hints]
18
+ end
19
+
20
+ def decrement_attemts
21
+ @attempts -= 1
22
+ end
23
+
24
+ def decrement_hints
25
+ @hints -= 1
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class InputError < StandardError
4
+ def initialize
5
+ super("Incorrect state of the game")
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LengthError < StandardError
4
+ def initialize
5
+ super("Incorrect length")
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StateError < StandardError
4
+ def initialize
5
+ super("Incorrect state of the game")
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors/input_error"
4
+ require_relative "errors/length_error"
5
+ require_relative "errors/state_error"
6
+
7
+ module Codebreaker
8
+ module Validation
9
+ NUMBERS = /^[1-6]{4}$/
10
+
11
+ def validate_name(name)
12
+ raise LengthError unless Constants::NAME_LENGTH.cover?(name.length)
13
+ end
14
+
15
+ def validate_guess(code)
16
+ raise InputError unless NUMBERS.match?(code)
17
+ end
18
+
19
+ def validate_state(state, current_state)
20
+ raise StateError unless state == current_state
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ VERSION = "0.1.2"
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ require_relative "codebreaker/version"
6
+ require_relative "codebreaker/game_const"
7
+ require_relative "codebreaker/autoloader"
8
+ require_relative "codebreaker/validation/validation"
9
+ require_relative "codebreaker/code_generator"
10
+ require_relative "codebreaker/user"
11
+ require_relative "codebreaker/file_store"
12
+ require_relative "codebreaker/game"
13
+
14
+ module Codebreaker
15
+ class Error < StandardError; end
16
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker-mikhail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Bogun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-06 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.2.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.3
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.30'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.30'
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.5.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.5.0
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'
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
+ description: Game for console
112
+ email:
113
+ - mikebogun@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 copy.yml"
123
+ - ".rubocop.yml"
124
+ - ".ruby-version"
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - Gemfile.lock
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - bin/console
132
+ - bin/setup
133
+ - codebreaker.gemspec
134
+ - lib/codebreaker.rb
135
+ - lib/codebreaker/autoloader.rb
136
+ - lib/codebreaker/code_generator.rb
137
+ - lib/codebreaker/file_store.rb
138
+ - lib/codebreaker/game.rb
139
+ - lib/codebreaker/game_const.rb
140
+ - lib/codebreaker/matrix.rb
141
+ - lib/codebreaker/user.rb
142
+ - lib/codebreaker/validation/errors/input_error.rb
143
+ - lib/codebreaker/validation/errors/length_error.rb
144
+ - lib/codebreaker/validation/errors/state_error.rb
145
+ - lib/codebreaker/validation/validation.rb
146
+ - lib/codebreaker/version.rb
147
+ homepage:
148
+ licenses:
149
+ - MIT
150
+ metadata:
151
+ homepage_uri: https://github.com/MikhailBogun/Codebreaker
152
+ source_code_uri: https://github.com/MikhailBogun/Codebreaker
153
+ changelog_uri: https://github.com/MikhailBogun/Codebreaker
154
+ rubygems_mfa_required: 'true'
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: 3.0.0
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.2.3
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Codebreaker game
174
+ test_files: []