codebreaker-yeroshek 0.1.1

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
+ SHA1:
3
+ metadata.gz: '033181db21196b32742be36e1d59d00e85138500'
4
+ data.tar.gz: 844353e50606c043ebd8972aa8c738b21d0cf3a5
5
+ SHA512:
6
+ metadata.gz: 36c341e9c52d68d08b057520e0f9aa22ca492512858e47e006b4c1ccc08f0b9ad67eaaf79fc99e088e0ed8aee01847230db5b92fc649ca5d37c4d2b31bf4c6b2
7
+ data.tar.gz: 466630f29fb74e10abac87bfe7939fc91fd6d9229c0068e9e8872064f8ecfccd0bca9f1adab82f8d386d4e3b417a550f99067554e580f640f4e085249c98b726
@@ -0,0 +1,48 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: circleci/ruby:2.5.3-node-browsers
6
+ enviroment:
7
+ RAILS_ENV: test
8
+
9
+ working_directory: ~/repo
10
+
11
+ steps:
12
+ - checkout
13
+
14
+ # Download and cache dependencies
15
+ - restore_cache:
16
+ keys:
17
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
18
+ # fallback to using the latest cache if no exact match is found
19
+ - v1-dependencies-
20
+
21
+ - run:
22
+ name: install dependencies
23
+ command: |
24
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
25
+
26
+ - save_cache:
27
+ paths:
28
+ - ./vendor/bundle
29
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
30
+
31
+ # run tests!
32
+ - run:
33
+ name: run tests
34
+ command: |
35
+ mkdir /tmp/test-results
36
+
37
+ bundle exec rspec --format progress \
38
+ --format RspecJunitFormatter \
39
+ --out /tmp/test-results/rspec.xml \
40
+ --format progress \
41
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
42
+
43
+ # collect reports
44
+ - store_test_results:
45
+ path: /tmp/test-results
46
+ - store_artifacts:
47
+ path: /tmp/test-results
48
+ destination: test-results
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
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
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ /coverage
2
+ /database/data.yml
3
+ /database/data_test.yml
4
+ /pkg/
5
+
6
+ # rspec failure tracking
7
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ Style/Encoding:
4
+ Enabled: false
5
+
6
+ Metrics/LineLength:
7
+ Max: 120
8
+
9
+ Metrics/MethodLength:
10
+ Max: 15
11
+
12
+ Metrics/BlockLength:
13
+ ExcludedMethods: ['describe']
14
+ Max: 30
15
+
16
+ Documentation:
17
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rg-codebreaker
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.3p105
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.16.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
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 codebreaker.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ colorize (0.8.1)
12
+ concurrent-ruby (1.1.3)
13
+ diff-lcs (1.3)
14
+ docile (1.3.1)
15
+ fasterer (0.4.1)
16
+ colorize (~> 0.7)
17
+ ruby_parser (~> 3.11.0)
18
+ i18n (1.1.1)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.1)
21
+ json (2.1.0)
22
+ method_source (0.9.2)
23
+ parallel (1.12.1)
24
+ parser (2.5.3.0)
25
+ ast (~> 2.4.0)
26
+ powerpack (0.1.2)
27
+ pry (0.12.2)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.9.0)
30
+ rainbow (3.0.0)
31
+ rake (10.5.0)
32
+ rspec (3.8.0)
33
+ rspec-core (~> 3.8.0)
34
+ rspec-expectations (~> 3.8.0)
35
+ rspec-mocks (~> 3.8.0)
36
+ rspec-core (3.8.0)
37
+ rspec-support (~> 3.8.0)
38
+ rspec-expectations (3.8.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-mocks (3.8.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-support (3.8.0)
45
+ rspec_junit_formatter (0.4.1)
46
+ rspec-core (>= 2, < 4, != 2.12.0)
47
+ rubocop (0.61.1)
48
+ jaro_winkler (~> 1.5.1)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.5, != 2.5.1.1)
51
+ powerpack (~> 0.1)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (~> 1.4.0)
55
+ rubocop-rspec (1.30.1)
56
+ rubocop (>= 0.60.0)
57
+ ruby-progressbar (1.10.0)
58
+ ruby_parser (3.11.0)
59
+ sexp_processor (~> 4.9)
60
+ sexp_processor (4.11.0)
61
+ simplecov (0.16.1)
62
+ docile (~> 1.1)
63
+ json (>= 1.8, < 3)
64
+ simplecov-html (~> 0.10.0)
65
+ simplecov-html (0.10.2)
66
+ unicode-display_width (1.4.0)
67
+
68
+ PLATFORMS
69
+ ruby
70
+ x64-mingw32
71
+
72
+ DEPENDENCIES
73
+ bundler (~> 1.16)
74
+ codebreaker!
75
+ fasterer
76
+ i18n
77
+ pry
78
+ rake (~> 10.0)
79
+ rspec (~> 3.0)
80
+ rspec_junit_formatter
81
+ rubocop
82
+ rubocop-rspec
83
+ simplecov
84
+
85
+ BUNDLED WITH
86
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
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,42 @@
1
+ # Codebreaker
2
+ 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.
3
+
4
+ TODO: Delete this and the text above, and describe your gem
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'codebreaker'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install codebreaker
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ 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.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ 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 [Contributor Covenant](http://contributor-covenant.org) code of conduct.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
39
+
40
+ ## Code of Conduct
41
+
42
+ 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/mmrshk/codebreaker/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/autoload.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require 'i18n'
5
+ require 'yaml'
6
+ require_relative 'lib/app/i18n_config'
7
+ require_relative 'lib/app/modules/validator'
8
+ require_relative 'lib/app/entities/game'
9
+ require_relative 'lib/app/entities/game'
10
+ require_relative 'lib/app/entities/data_storage'
11
+ require_relative 'lib/app/entities/processor'
12
+ require_relative 'lib/app/entities/menu'
13
+ require_relative 'lib/app/entities/renderer'
14
+ require_relative 'lib/app/entities/statistics'
15
+ require_relative 'lib/codebreaker/version'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "codebreaker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,45 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codebreaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'codebreaker-yeroshek'
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ['Emma Yeroshek']
10
+ spec.email = ['emma.yeroshek@gmail.com']
11
+
12
+ spec.summary = 'Codebreaker app'
13
+ spec.description = 'Logic game'
14
+ spec.homepage = 'https://github.com/mmrshk/rg-codebreaker'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = 'exe'
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ['lib']
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.16'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ spec.add_development_dependency 'fasterer'
39
+ spec.add_development_dependency 'i18n'
40
+ spec.add_development_dependency 'pry'
41
+ spec.add_development_dependency 'rubocop'
42
+ spec.add_development_dependency 'rubocop-rspec'
43
+ spec.add_development_dependency 'simplecov'
44
+ spec.add_development_dependency 'rspec_junit_formatter'
45
+ end
@@ -0,0 +1,4 @@
1
+ # Ignore everything in this directory
2
+ *
3
+ # Except this file
4
+ !.gitignore
data/index.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'autoload.rb'
4
+
5
+ Menu.new.game_menu
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DataStorage
4
+ FILE_NAME = 'database/data.yml'
5
+
6
+ def create
7
+ File.new(FILE_NAME, 'w')
8
+ File.write(FILE_NAME, [].to_yaml)
9
+ end
10
+
11
+ def load
12
+ YAML.load(File.open(FILE_NAME), [Menu]) if storage_exist?
13
+ end
14
+
15
+ def save(object)
16
+ File.open(FILE_NAME, 'w') { |file| file.write(YAML.dump(object)) }
17
+ end
18
+
19
+ def storage_exist?
20
+ File.exist?(FILE_NAME)
21
+ end
22
+
23
+ def save_game_result(object)
24
+ create unless storage_exist?
25
+ save(load.push(object))
26
+ end
27
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Game
4
+ DIGITS_COUNT = 4
5
+ DIFFICULTIES = {
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
+ }.freeze
19
+ RANGE = (1..6).freeze
20
+
21
+ attr_reader :attempts, :hints, :code
22
+
23
+ def initialize
24
+ @process = Processor.new
25
+ end
26
+
27
+ def generate(difficulty)
28
+ @difficulty = difficulty
29
+ @code = generate_secret_code
30
+ @hints = @code.sample(difficulty[:hints])
31
+ @attempts = difficulty[:attempts]
32
+ end
33
+
34
+ def start_process(command)
35
+ @process.secret_code_proc(code.join, command)
36
+ end
37
+
38
+ def win?(guess)
39
+ code.join == guess
40
+ end
41
+
42
+ def decrease_attempts!
43
+ @attempts -= 1
44
+ end
45
+
46
+ def to_h(name)
47
+ {
48
+ name: name,
49
+ difficulty: DIFFICULTIES.key(@difficulty),
50
+ all_attempts: @difficulty[:attempts],
51
+ all_hints: @difficulty[:hints],
52
+ attempts_used: @difficulty[:attempts] - @attempts,
53
+ hints_used: @difficulty[:hints] - @hints.length
54
+ }
55
+ end
56
+
57
+ def hints_spent?
58
+ hints.empty?
59
+ end
60
+
61
+ def take_a_hint!
62
+ hints.pop
63
+ end
64
+
65
+ private
66
+
67
+ def generate_secret_code
68
+ Array.new(DIGITS_COUNT) { rand(RANGE) }
69
+ end
70
+ end
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Menu
4
+ include Validator
5
+ attr_reader :storage, :renderer, :game, :guess
6
+
7
+ COMMANDS = {
8
+ start: 'start',
9
+ exit: 'exit',
10
+ rules: 'rules',
11
+ stats: 'stats'
12
+ }.freeze
13
+ CHOOSE_COMMANDS = {
14
+ yes: 'yes'
15
+ }.freeze
16
+ HINT_COMMAND = 'hint'
17
+ MIN_SIZE_VALUE = 3
18
+ MAX_SIZE_VALUE = 20
19
+
20
+ def initialize
21
+ @storage = DataStorage.new
22
+ @renderer = Renderer.new
23
+ @game = Game.new
24
+ @statistics = Statistics.new
25
+ end
26
+
27
+ def game_menu
28
+ renderer.start_message
29
+ choice_menu_process(ask(:choice_options, commands: COMMANDS.keys.join(' | ')))
30
+ end
31
+
32
+ private
33
+
34
+ def rules
35
+ renderer.rules
36
+ game_menu
37
+ end
38
+
39
+ def start
40
+ @name = registrate_user
41
+ level_choice
42
+ game_process
43
+ end
44
+
45
+ def stats
46
+ scores = storage.load
47
+ render_stats(@statistics.stats(scores)) if scores
48
+ game_menu
49
+ end
50
+
51
+ def ask(phrase_key = nil, options = {})
52
+ renderer.message(phrase_key, options) if phrase_key
53
+ gets.chomp
54
+ end
55
+
56
+ def save_result
57
+ storage.save_game_result(game.to_h(@name)) if ask(:save_results_message) == CHOOSE_COMMANDS[:yes]
58
+ end
59
+
60
+ def registrate_user
61
+ loop do
62
+ name = ask(:registration)
63
+
64
+ return name if name_valid?(name)
65
+
66
+ renderer.registration_name_length_error
67
+ end
68
+ end
69
+
70
+ def name_valid?(name)
71
+ !check_emptyness(name) && check_length(name, MIN_SIZE_VALUE, MAX_SIZE_VALUE)
72
+ end
73
+
74
+ def level_choice
75
+ loop do
76
+ level = ask(:hard_level, levels: Game::DIFFICULTIES.keys.join(' | '))
77
+
78
+ return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
79
+ return game_menu if level == COMMANDS[:exit]
80
+
81
+ renderer.command_error
82
+ end
83
+ end
84
+
85
+ def generate_game(difficulty)
86
+ game.generate(difficulty)
87
+ renderer.message(:difficulty,
88
+ hints: difficulty[:hints],
89
+ attempts: difficulty[:attempts])
90
+ end
91
+
92
+ def game_process
93
+ while game.attempts.positive?
94
+ @guess = ask
95
+ return handle_win if game.win?(guess)
96
+
97
+ choice_code_process
98
+ end
99
+ handle_lose
100
+ end
101
+
102
+ def choice_code_process
103
+ case guess
104
+ when HINT_COMMAND then hint_process
105
+ when COMMANDS[:exit] then game_menu
106
+ else handle_command
107
+ end
108
+ end
109
+
110
+ def handle_command
111
+ return renderer.command_error unless check_command_range(guess)
112
+
113
+ p game.start_process(guess)
114
+ renderer.round_message
115
+ game.decrease_attempts!
116
+ end
117
+
118
+ def handle_win
119
+ renderer.win_game_message
120
+ save_result
121
+ game_menu
122
+ end
123
+
124
+ def handle_lose
125
+ renderer.lost_game_message(game.code)
126
+ game_menu
127
+ end
128
+
129
+ def hint_process
130
+ return renderer.no_hints_message? if game.hints_spent?
131
+
132
+ renderer.print_hint_number(game.take_a_hint!)
133
+ end
134
+
135
+ def exit_from_game
136
+ renderer.goodbye_message
137
+ exit
138
+ end
139
+
140
+ def choice_menu_process(command_name)
141
+ case command_name
142
+ when COMMANDS[:start] then start
143
+ when COMMANDS[:exit] then exit_from_game
144
+ when COMMANDS[:rules] then rules
145
+ when COMMANDS[:stats] then stats
146
+ else
147
+ renderer.command_error
148
+ game_menu
149
+ end
150
+ end
151
+
152
+ def render_stats(list)
153
+ list.each_with_index do |key, index|
154
+ puts "#{index + 1}: "
155
+ key.each { |param, value| puts "#{param}:#{value}" }
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Processor
4
+ MATCHED_DIGIT_CHAR = '+'
5
+ UNMATCHED_DIGIT_CHAR = '-'
6
+
7
+ attr_reader :guess, :code, :result
8
+
9
+ def secret_code_proc(code, guess)
10
+ @code = code.split('')
11
+ @guess = guess.split('')
12
+ handle_matched_digits.join + handle_matched_digits_with_wrong_position.join
13
+ end
14
+
15
+ private
16
+
17
+ def handle_matched_digits
18
+ code.map.with_index do |_, index|
19
+ next unless code[index] == guess[index]
20
+
21
+ @guess[index], @code[index] = nil
22
+ MATCHED_DIGIT_CHAR
23
+ end
24
+ end
25
+
26
+ def handle_matched_digits_with_wrong_position
27
+ guess.compact.map do |number|
28
+ next unless @code.include?(number)
29
+
30
+ @code.delete_at(code.index(number))
31
+ UNMATCHED_DIGIT_CHAR
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Renderer
4
+ def message(msg_name, hashee = {})
5
+ puts I18n.t(msg_name, hashee)
6
+ end
7
+
8
+ def start_message
9
+ message(:start_message)
10
+ end
11
+
12
+ def rules
13
+ message(:rules)
14
+ end
15
+
16
+ def goodbye_message
17
+ message(:goodbye_message)
18
+ end
19
+
20
+ def save_results_message
21
+ message(:save_results_message)
22
+ end
23
+
24
+ def win_game_message
25
+ message(:win_game_message)
26
+ end
27
+
28
+ def round_message
29
+ message(:round_message)
30
+ end
31
+
32
+ def lost_game_message(code)
33
+ message(:lost_game_message, code: code)
34
+ end
35
+
36
+ def no_hints_message?
37
+ message(:have_no_hints_message)
38
+ end
39
+
40
+ def print_hint_number(code)
41
+ message(:print_hint_number, code: code)
42
+ end
43
+
44
+ def registration_name_emptyness_error
45
+ message(:registration_name_emptyness_error)
46
+ end
47
+
48
+ def registration_name_length_error
49
+ message(:registration_name_length_error)
50
+ end
51
+
52
+ def command_error
53
+ message(:command_error)
54
+ end
55
+
56
+ def command_length_error
57
+ message(:command_length_error)
58
+ end
59
+
60
+ def command_int_error
61
+ message(:command_int_error)
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Statistics
4
+ def stats(list)
5
+ difficulties = list.group_by { |score| score[:difficulty] }
6
+ %w[hell medium easy].reduce([]) do |sorted_difficulties, difficulty_name|
7
+ if difficulties[difficulty_name]
8
+ sorted_difficulties + stats_sort(difficulties[difficulty_name])
9
+ else
10
+ sorted_difficulties
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def stats_sort(scores)
18
+ scores.sort_by! { |score| [score[:attempts_used], score[:hints_used]] }.reverse
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ I18n.load_path << Dir[File.expand_path('lib/app/locales/') + '/*.yml']
4
+ I18n.config.available_locales = :en
@@ -0,0 +1,39 @@
1
+ en:
2
+ start_message: "Welcome to CodeBreaker"
3
+ choice_options: "Choose option:
4
+ %{commands}"
5
+ hard_level: "Which level you do you want to play?
6
+ %{levels}"
7
+ goodbye_message: "We are waiting for you. Come back!"
8
+ command_error: "You have passed unexpected command. Please choose one from listed commands"
9
+ rules: "Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
10
+ The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
11
+
12
+ A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code and in the same position. For example:
13
+ Secret number - 1234
14
+ Input number - 6264
15
+ Number of pluses - 2 (second and fourth position)
16
+
17
+ A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code but in a different position. For example:
18
+ Secret number - 1234
19
+ Input number - 6462
20
+ Number of minuses - 2 (second and fourth position)
21
+
22
+ An empty space indicates that there is not a current digit in a secret number.
23
+
24
+ If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
25
+
26
+ Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint - he receives back a separate digit of the secret code.
27
+ "
28
+ registration: "Before start, enter your name, please "
29
+ difficulty: "You had to %{hints} hints and %{attempts} attempts"
30
+ round_message: "1. Enter your secret code
31
+ 2. hint
32
+ 3. exit "
33
+ guess: "Opps, secret code must to be from four digits in range from 1 to 6"
34
+ lost_game_message: "Oh, your attempts ended... Your code was: %{code}"
35
+ win_game_message: "Yayy, u won the game! Congrats!"
36
+ save_results_message: "Do you want to save result? 1. yes 2. no"
37
+ have_no_hints_message: "Oh, your hints ended"
38
+ print_hint_number: "Hint number: %{code}"
39
+ registration_name_length_error: "It must be more than 3 and less than 20 symbols."
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validator
4
+ VALUE_FORMAT = /^[1-6]{4}$/.freeze
5
+
6
+ def check_emptyness(value)
7
+ value.empty?
8
+ end
9
+
10
+ def check_length(value, min_size, max_size)
11
+ value.size.between?(min_size, max_size)
12
+ end
13
+
14
+ def check_command_range(command)
15
+ command =~ VALUE_FORMAT
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "codebreaker/version"
4
+
5
+ module Codebreaker
6
+ end
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker-yeroshek
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Emma Yeroshek
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-30 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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fasterer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec_junit_formatter
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Logic game
154
+ email:
155
+ - emma.yeroshek@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".circleci/config.yml"
161
+ - ".fasterer.yml"
162
+ - ".gitignore"
163
+ - ".rspec"
164
+ - ".rubocop.yml"
165
+ - ".ruby-gemset"
166
+ - ".ruby-version"
167
+ - ".travis.yml"
168
+ - CODE_OF_CONDUCT.md
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - autoload.rb
175
+ - bin/console
176
+ - bin/setup
177
+ - codebreaker.gemspec
178
+ - database/.gitignore
179
+ - index.rb
180
+ - lib/app/entities/data_storage.rb
181
+ - lib/app/entities/game.rb
182
+ - lib/app/entities/menu.rb
183
+ - lib/app/entities/processor.rb
184
+ - lib/app/entities/renderer.rb
185
+ - lib/app/entities/statistics.rb
186
+ - lib/app/i18n_config.rb
187
+ - lib/app/locales/en.yml
188
+ - lib/app/modules/validator.rb
189
+ - lib/codebreaker.rb
190
+ - lib/codebreaker/version.rb
191
+ homepage: https://github.com/mmrshk/rg-codebreaker
192
+ licenses:
193
+ - MIT
194
+ metadata:
195
+ allowed_push_host: https://rubygems.org
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.5.2.3
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: Codebreaker app
216
+ test_files: []