codebreaker_gem_iagodka 0.1.0

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: eb1ae76715864a3fd5c14cc1fef645ff414999fd25cf9659162f7bb17467543f
4
+ data.tar.gz: 9ea33cd131d4a5d62facdb1a53b35bcd4cdc63a17f822cd902fd06424350b8a0
5
+ SHA512:
6
+ metadata.gz: e61b22b85fe4d80235e105f97def5b7226982b3a42b7badd97e76cb31ab6c6ea0c9266436bfc87b590c363507a26cd67650604b35bda798e1b34542b2d6d3ac4
7
+ data.tar.gz: 5d962c256998e3f8a96d1d667577ad3579364702610f037cba2c36b440731e6d55c918f25ba323b9dda3482f5b3a825ece56922e17006450e9187126f7e782d8
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,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /spec/reports/
7
+ /tmp/
8
+
9
+ # rspec failure tracking
10
+ .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,10 @@
1
+ require: rubocop-rspec
2
+
3
+ Metrics/LineLength:
4
+ Max: 120
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ Metrics/BlockLength:
10
+ ExcludedMethods: ['describe']
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ codebreaker
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.1
@@ -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 andrei.iagodka@gmail.com. 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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in codebreaker_gem.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker_gem (0.1.0)
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.4)
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.5.1)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.2)
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
+ rubocop (0.62.0)
46
+ jaro_winkler (~> 1.5.1)
47
+ parallel (~> 1.10)
48
+ parser (>= 2.5, != 2.5.1.1)
49
+ powerpack (~> 0.1)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (~> 1.4.0)
53
+ rubocop-rspec (1.31.0)
54
+ rubocop (>= 0.60.0)
55
+ ruby-progressbar (1.10.0)
56
+ ruby_parser (3.11.0)
57
+ sexp_processor (~> 4.9)
58
+ sexp_processor (4.11.0)
59
+ simplecov (0.16.1)
60
+ docile (~> 1.1)
61
+ json (>= 1.8, < 3)
62
+ simplecov-html (~> 0.10.0)
63
+ simplecov-html (0.10.2)
64
+ terminal-table (1.8.0)
65
+ unicode-display_width (~> 1.1, >= 1.1.1)
66
+ unicode-display_width (1.4.1)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 1.17)
73
+ codebreaker_gem!
74
+ fasterer
75
+ i18n
76
+ pry
77
+ rake (~> 10.0)
78
+ rspec (~> 3.0)
79
+ rubocop
80
+ rubocop-rspec
81
+ simplecov
82
+ terminal-table
83
+
84
+ BUNDLED WITH
85
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 andreiiagodka
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
+ # CodebreakerGem
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_gem`. 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_gem'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codebreaker_gem
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]/codebreaker_gem. 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.
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 CodebreakerGem project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker_gem/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,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pry'
4
+ require 'yaml'
5
+ require 'i18n'
6
+ require 'terminal-table'
7
+
8
+ require_relative 'config/i18n'
9
+
10
+ require_relative 'lib/app/helpers/output_helper'
11
+ require_relative 'lib/app/helpers/output'
12
+ require_relative 'lib/app/helpers/failing'
13
+
14
+ require_relative 'lib/app/modules/console_user_interaction'
15
+
16
+ require_relative 'lib/app/entities/console'
17
+ require_relative 'lib/app/entities/validated_entity'
18
+ require_relative 'lib/app/entities/player'
19
+ require_relative 'lib/app/entities/difficulty'
20
+ require_relative 'lib/app/entities/guess'
21
+ require_relative 'lib/app/entities/game'
22
+ require_relative 'lib/app/entities/statistic'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "codebreaker_gem"
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,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "codebreaker_gem/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codebreaker_gem_iagodka"
8
+ spec.version = CodebreakerGem::VERSION
9
+ spec.authors = ["andreiiagodka"]
10
+ spec.email = ["andrei.iagodka@gmail.com"]
11
+
12
+ spec.summary = 'CodebreakerAndreiiagodka'
13
+ spec.description = 'Logic game'
14
+ spec.homepage = 'https://github.com/andreiiagodka/codebreaker_gem.git'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.17"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "i18n"
28
+ spec.add_development_dependency "fasterer"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "rubocop"
31
+ spec.add_development_dependency "rubocop-rspec"
32
+ spec.add_development_dependency "simplecov"
33
+ spec.add_development_dependency "terminal-table"
34
+ end
data/config/i18n.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ I18n.load_path << Dir[File.expand_path('database') + '/' + 'locales.yml']
@@ -0,0 +1,30 @@
1
+ en:
2
+ message:
3
+ introduction: Welcome to Codebreaker!
4
+ rules: Codebreaker rules
5
+ exit: Goodbye!
6
+ registration_heading: Codebreaker Registration
7
+ difficulty_heading: Choose difficulty
8
+ game_start_heading: The games has started! Try to guess secret code.
9
+ input_secret_code: Input secret code
10
+ player_name_registration: Input your name
11
+ statistics: Used attempts - %{used_attempts}/%{total_attempts}. Used hints - %{used_hints}/%{total_hints}
12
+ used_attempts: Used attempts
13
+ used_hints: Used hints
14
+ win: Congratulations! You have guessed it!
15
+ save_result: Would you like to save result of the game? [yes/no]
16
+ start_new_game: Would you like to start a new game? [yes/no]
17
+
18
+ error:
19
+ unexpected_option: You have passed unexpected option. Please choose one from listed options.
20
+ unexpected_command: You have passed unexpected command. Please choose one from listed options.
21
+ unexpected_difficulty: You have passed unexpected difficulty. Please choose one from listed difficulties.
22
+ player_name_length: Player name length min - %{min_length}, max - %{max_length}
23
+ secret_code_length: Required %{code_length} elements!
24
+ secret_code_digits_range: Each digit has to be in range %{min_value}-%{max_value}!
25
+ hints_limit: You are out of hints!
26
+ attempts_limit: You are out of attempts!
27
+
28
+ table:
29
+ title: Rating of Codebreakers
30
+ headings: [Rating, Name, Difficulty, Total Attempts, Used Attempts, Total Hints, Used Hints]
@@ -0,0 +1,49 @@
1
+ ---
2
+ :name: aaaaaaaaaaaaaaaaaaaa
3
+ :difficulty: medium
4
+ :total_attempts: 10
5
+ :used_attempts: 0
6
+ :total_hints: 1
7
+ :used_hints: 0
8
+ ---
9
+ :name: aaaaaaaaaaaaaaaaaaaa
10
+ :difficulty: easy
11
+ :total_attempts: 15
12
+ :used_attempts: 0
13
+ :total_hints: 3
14
+ :used_hints: 0
15
+ ---
16
+ :name: aaaaaaaaaaaaaaaaaaaa
17
+ :difficulty: medium
18
+ :total_attempts: 10
19
+ :used_attempts: 0
20
+ :total_hints: 1
21
+ :used_hints: 0
22
+ ---
23
+ :name: aaaaaaaaaaaaaaaaaaaa
24
+ :difficulty: easy
25
+ :total_attempts: 15
26
+ :used_attempts: 0
27
+ :total_hints: 3
28
+ :used_hints: 0
29
+ ---
30
+ :name: aaaaaaaaaaaaaaaaaaaa
31
+ :difficulty: easy
32
+ :total_attempts: 15
33
+ :used_attempts: 0
34
+ :total_hints: 3
35
+ :used_hints: 0
36
+ ---
37
+ :name: aaaaaaaaaaaaaaaaaaaa
38
+ :difficulty: easy
39
+ :total_attempts: 15
40
+ :used_attempts: 0
41
+ :total_hints: 3
42
+ :used_hints: 0
43
+ ---
44
+ :name: aaaaaaaaaaaaaaaaaaaa
45
+ :difficulty: easy
46
+ :total_attempts: 15
47
+ :used_attempts: 0
48
+ :total_hints: 3
49
+ :used_hints: 0
data/index.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'autoload.rb'
4
+
5
+ CodebreakerGem::Console.new.menu
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Console
5
+ include ConsoleUserInteraction
6
+
7
+ COMMANDS = {
8
+ start: 'start',
9
+ rules: 'rules',
10
+ stats: 'stats',
11
+ exit: 'exit'
12
+ }.freeze
13
+
14
+ KEYWORDS = {
15
+ yes: 'yes',
16
+ no: 'no'
17
+ }.freeze
18
+
19
+ def menu
20
+ output.introduction
21
+ loop do
22
+ output.display(options_list)
23
+ select_option
24
+ end
25
+ end
26
+
27
+ def select_option
28
+ case user_input
29
+ when COMMANDS[:start] then navigation
30
+ when COMMANDS[:rules] then output.rules
31
+ when COMMANDS[:stats] then output.display(statistic.rating_table)
32
+ else failing.unexpected_option
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def navigation
39
+ @player = create_entity(Player)
40
+ @difficulty = create_entity(Difficulty)
41
+ game_process
42
+ end
43
+
44
+ def game_process
45
+ output.game_start_heading
46
+ @game = Game.new(@difficulty.level)
47
+ make_guess
48
+ end
49
+
50
+ def make_guess
51
+ loop do
52
+ @guess = create_entity(Guess)
53
+ @guess.hint? ? output_hint : guess_result
54
+ end
55
+ end
56
+
57
+ def output_hint
58
+ @game.hints_available? ? output.display(failing.hints_limit) : output.display(@game.use_hint)
59
+ end
60
+
61
+ def guess_result
62
+ @game.increment_used_attempts
63
+ return win if @game.win?(@guess.guess_code)
64
+ return loss if @game.loss?
65
+
66
+ marked_guess = @game.mark_guess(@guess.guess_code)
67
+ output.display(marked_guess)
68
+ end
69
+
70
+ def win
71
+ output.win
72
+ save_result
73
+ start_new_game
74
+ end
75
+
76
+ def loss
77
+ output.display(failing.attempts_limit)
78
+ start_new_game
79
+ end
80
+
81
+ def save_result
82
+ output.save_result
83
+ loop do
84
+ case user_input
85
+ when KEYWORDS[:yes] then return statistic.save(@player, @game)
86
+ when KEYWORDS[:no] then return
87
+ else failing.unexpected_command
88
+ end
89
+ end
90
+ end
91
+
92
+ def start_new_game
93
+ output.start_new_game
94
+ loop do
95
+ case user_input
96
+ when KEYWORDS[:yes] then menu
97
+ when KEYWORDS[:no] then exit_from_console
98
+ else failing.unexpected_command
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Difficulty < ValidatedEntity
5
+ attr_reader :level, :errors
6
+
7
+ DIFFICULTIES = {
8
+ easy: {
9
+ level: 'easy',
10
+ attempts: 15,
11
+ hints: 3
12
+ },
13
+
14
+ medium: {
15
+ level: 'medium',
16
+ attempts: 10,
17
+ hints: 1
18
+ },
19
+
20
+ hard: {
21
+ level: 'hard',
22
+ attempts: 5,
23
+ hints: 1
24
+ }
25
+ }.freeze
26
+
27
+ def initialize(difficulty)
28
+ super()
29
+ @level = DIFFICULTIES[difficulty.to_sym]
30
+ end
31
+
32
+ def validate
33
+ @errors << failing.unexpected_difficulty if check_difficulty
34
+ end
35
+
36
+ def self.list
37
+ DIFFICULTIES.keys.map(&:to_s).map(&:capitalize)
38
+ end
39
+
40
+ private
41
+
42
+ def check_difficulty
43
+ @level.nil?
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Game
5
+ attr_reader :difficulty, :total_attempts, :used_attempts, :total_hints, :used_hints, :secret_code
6
+
7
+ SECRET_CODE_LENGTH = 4
8
+ ELEMENT_VALUE_RANGE = (1..6).freeze
9
+
10
+ DELIMITER = ''
11
+ EXACT_MATCH = '+'
12
+ NUMBER_MATCH = '-'
13
+
14
+ def initialize(difficulty)
15
+ @difficulty = difficulty[:level]
16
+ @total_attempts = difficulty[:attempts]
17
+ @used_attempts = 0
18
+ @total_hints = difficulty[:hints]
19
+ @used_hints = 0
20
+ @secret_code = generate
21
+ @shuffled_code = @secret_code.shuffle
22
+ end
23
+
24
+ def use_hint
25
+ increment_used_hints
26
+ @shuffled_code.shift
27
+ end
28
+
29
+ def hints_available?
30
+ @used_hints >= @total_hints
31
+ end
32
+
33
+ def win?(guess_code)
34
+ guess_code == convert_to_string(@secret_code)
35
+ end
36
+
37
+ def loss?
38
+ @used_attempts == @total_attempts
39
+ end
40
+
41
+ def increment_used_attempts
42
+ @used_attempts += 1
43
+ end
44
+
45
+ def mark_guess(guess_code)
46
+ @converted_input = convert_to_digit_array(guess_code)
47
+ @cloned_code = @secret_code.clone
48
+ convert_to_string(exact_match.compact + number_match.compact)
49
+ end
50
+
51
+ private
52
+
53
+ def generate
54
+ Array.new(SECRET_CODE_LENGTH) { rand(ELEMENT_VALUE_RANGE) }
55
+ end
56
+
57
+ def increment_used_hints
58
+ @used_hints += 1
59
+ end
60
+
61
+ def convert_to_string(argument)
62
+ argument.join
63
+ end
64
+
65
+ def convert_to_digit_array(guess_code)
66
+ guess_code.split(DELIMITER).map(&:to_i)
67
+ end
68
+
69
+ def exact_match
70
+ @converted_input.map.with_index do |digit, index|
71
+ next unless @cloned_code[index] == digit
72
+
73
+ @converted_input[index] = nil
74
+ @cloned_code[index] = nil
75
+ EXACT_MATCH
76
+ end
77
+ end
78
+
79
+ def number_match
80
+ @converted_input.compact.map do |digit|
81
+ next unless @cloned_code.include?(digit)
82
+
83
+ @cloned_code.delete_at(@cloned_code.index(digit))
84
+ NUMBER_MATCH
85
+ end
86
+ end
87
+
88
+ def convert_marked_guess
89
+ convert_to_string(@cloned_code.grep(String).sort)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Guess < ValidatedEntity
5
+ attr_reader :guess_code, :errors
6
+
7
+ ELEMENT_VALUE_RANGE = (1..6).freeze
8
+
9
+ HINT = 'hint'
10
+
11
+ def initialize(guess_code)
12
+ super()
13
+ @guess_code = guess_code
14
+ end
15
+
16
+ def validate
17
+ return if hint?
18
+
19
+ @errors << failing.secret_code_length unless check_length
20
+ @errors << failing.secret_code_digits_range unless check_digits_range
21
+ end
22
+
23
+ def hint?
24
+ @guess_code == HINT
25
+ end
26
+
27
+ private
28
+
29
+ def check_length
30
+ @guess_code.length == Game::SECRET_CODE_LENGTH
31
+ end
32
+
33
+ def check_digits_range
34
+ @guess_code.each_char { |digit| break unless ELEMENT_VALUE_RANGE.include?(digit.to_i) }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Player < ValidatedEntity
5
+ attr_reader :name, :errors
6
+
7
+ NAME_LENGTH_RANGE = (3..20).freeze
8
+
9
+ def initialize(name)
10
+ super()
11
+ @name = name
12
+ end
13
+
14
+ def validate
15
+ @errors << failing.player_name_length unless check_name_length
16
+ end
17
+
18
+ private
19
+
20
+ def check_name_length
21
+ NAME_LENGTH_RANGE.include?(@name.length)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Statistic
5
+ DB_DIR = 'database'
6
+ STATISTIC_FILE_NAME = 'statistic'
7
+ YML_FORMAT = '.yml'
8
+
9
+ STATISTIC_YML = DB_DIR + '/' + STATISTIC_FILE_NAME + YML_FORMAT
10
+
11
+ def initialize
12
+ @table = I18n.t(:table)
13
+ end
14
+
15
+ def save(player, score)
16
+ new_record = record(player, score)
17
+ save_to_file(new_record)
18
+ end
19
+
20
+ def rating_table
21
+ table = Terminal::Table.new
22
+ table.title = @table[:title]
23
+ table.headings = @table[:headings]
24
+ table.rows = table_rows
25
+ table
26
+ end
27
+
28
+ private
29
+
30
+ def record(player, score)
31
+ {
32
+ name: player.name,
33
+ difficulty: score.difficulty,
34
+ total_attempts: score.total_attempts,
35
+ used_attempts: score.used_attempts,
36
+ total_hints: score.total_hints,
37
+ used_hints: score.used_hints
38
+ }
39
+ end
40
+
41
+ def save_to_file(record)
42
+ File.open(STATISTIC_YML, 'a') { |file| file.write record.to_yaml }
43
+ end
44
+
45
+ def table_rows
46
+ File.exist?(STATISTIC_YML) ? load.each_with_index.map { |record, index| rows(record, index) } : []
47
+ end
48
+
49
+ def load
50
+ YAML.load_stream(File.read(STATISTIC_YML)).sort_by { |statistic| statistic[:difficulty] }
51
+ end
52
+
53
+ def rows(record, index)
54
+ [
55
+ index + 1,
56
+ record[:name],
57
+ record[:difficulty],
58
+ record[:total_attempts],
59
+ record[:used_attempts],
60
+ record[:total_hints],
61
+ record[:used_hints]
62
+ ]
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class ValidatedEntity
5
+ def initialize
6
+ @errors = []
7
+ end
8
+
9
+ def validate
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def valid?
14
+ validate
15
+ @errors.empty?
16
+ end
17
+
18
+ def failing
19
+ @failing ||= Failing.new
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Failing < OutputHelper
5
+ ERROR = 'error'
6
+
7
+ def unexpected_difficulty
8
+ error(:unexpected_difficulty)
9
+ end
10
+
11
+ def attempts_limit
12
+ error(:attempts_limit)
13
+ end
14
+
15
+ def hints_limit
16
+ error(:hints_limit)
17
+ end
18
+
19
+ def player_name_length
20
+ error(:player_name_length,
21
+ min_length: Player::NAME_LENGTH_RANGE.min,
22
+ max_length: Player::NAME_LENGTH_RANGE.max)
23
+ end
24
+
25
+ def secret_code_length
26
+ error(:secret_code_length,
27
+ code_length: Game::SECRET_CODE_LENGTH)
28
+ end
29
+
30
+ def secret_code_digits_range
31
+ error(:secret_code_digits_range,
32
+ min_value: Guess::ELEMENT_VALUE_RANGE.min,
33
+ max_value: Guess::ELEMENT_VALUE_RANGE.max)
34
+ end
35
+
36
+ def unexpected_option
37
+ display(error(:unexpected_option))
38
+ end
39
+
40
+ def unexpected_command
41
+ display(error(:unexpected_command))
42
+ end
43
+
44
+ private
45
+
46
+ def error(error, *parameters)
47
+ get(ERROR, error, *parameters)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class Output < OutputHelper
5
+ MESSAGE = 'message'
6
+
7
+ def introduction
8
+ message(:introduction)
9
+ end
10
+
11
+ def rules
12
+ message(:rules)
13
+ end
14
+
15
+ def registration
16
+ message(:registration_heading)
17
+ message(:player_name_registration)
18
+ end
19
+
20
+ def game_start_heading
21
+ message(:game_start_heading)
22
+ end
23
+
24
+ def difficulty_heading
25
+ message(:difficulty_heading)
26
+ display(Difficulty.list)
27
+ end
28
+
29
+ def win
30
+ message(:win)
31
+ end
32
+
33
+ def exit
34
+ message(:exit)
35
+ end
36
+
37
+ def statistics(game)
38
+ message(:statistics,
39
+ used_attempts: game.used_attempts,
40
+ total_attempts: game.total_attempts,
41
+ used_hints: game.used_hints,
42
+ total_hints: game.total_hints)
43
+ message(:input_secret_code)
44
+ end
45
+
46
+ def save_result
47
+ message(:save_result)
48
+ end
49
+
50
+ def start_new_game
51
+ message(:start_new_game)
52
+ end
53
+
54
+ private
55
+
56
+ def message(message, *parameters)
57
+ display(get(MESSAGE, message, *parameters))
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ class OutputHelper
5
+ def get(section, message, *parameters)
6
+ I18n.t(translation_access(section, message), *parameters)
7
+ end
8
+
9
+ def display(argument)
10
+ puts argument
11
+ end
12
+
13
+ private
14
+
15
+ def translation_access(section, message)
16
+ "#{section}.#{convert_to_string(message)}"
17
+ end
18
+
19
+ def convert_to_string(argument)
20
+ argument.to_s
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodebreakerGem
4
+ module ConsoleUserInteraction
5
+ ENTITIES = {
6
+ player: 'Player',
7
+ difficulty: 'Difficulty',
8
+ guess: 'Guess'
9
+ }.freeze
10
+
11
+ def options_list
12
+ Console::COMMANDS.values.map(&:capitalize)
13
+ end
14
+
15
+ def user_input
16
+ input_value = gets.chomp.downcase
17
+ exit?(input_value) ? exit_from_console : input_value
18
+ end
19
+
20
+ def exit_from_console
21
+ output.exit
22
+ exit
23
+ end
24
+
25
+ def create_entity(klass)
26
+ loop do
27
+ class_heading(klass)
28
+ entity = klass.new(user_input)
29
+ return entity if entity.valid?
30
+
31
+ output.display(entity.errors)
32
+ end
33
+ end
34
+
35
+ def statistic
36
+ @statistic ||= Statistic.new
37
+ end
38
+
39
+ def output
40
+ @output ||= Output.new
41
+ end
42
+
43
+ def failing
44
+ @failing ||= Failing.new
45
+ end
46
+
47
+ private
48
+
49
+ def class_heading(klass)
50
+ case klass.to_s
51
+ when ENTITIES[:player] then output.registration
52
+ when ENTITIES[:difficulty] then output.difficulty_heading
53
+ when ENTITIES[:guess] then output.statistics(@game)
54
+ end
55
+ end
56
+
57
+ def exit?(input_value)
58
+ input_value == Console::COMMANDS[:exit]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,2 @@
1
+ require "codebreaker_gem/version"
2
+ require_relative '../autoload'
@@ -0,0 +1,3 @@
1
+ module CodebreakerGem
2
+ VERSION = "0.1.0"
3
+ end
Binary file
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_gem_iagodka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - andreiiagodka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-14 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: i18n
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: fasterer
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: terminal-table
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
+ - andrei.iagodka@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".fasterer.yml"
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - ".rubocop.yml"
164
+ - ".ruby-gemset"
165
+ - ".ruby-version"
166
+ - ".travis.yml"
167
+ - CODE_OF_CONDUCT.md
168
+ - Gemfile
169
+ - Gemfile.lock
170
+ - LICENSE.txt
171
+ - README.md
172
+ - Rakefile
173
+ - autoload.rb
174
+ - bin/console
175
+ - bin/setup
176
+ - codebreaker_gem.gemspec
177
+ - config/i18n.rb
178
+ - database/locales.yml
179
+ - database/statistic.yml
180
+ - index.rb
181
+ - lib/app/entities/console.rb
182
+ - lib/app/entities/difficulty.rb
183
+ - lib/app/entities/game.rb
184
+ - lib/app/entities/guess.rb
185
+ - lib/app/entities/player.rb
186
+ - lib/app/entities/statistic.rb
187
+ - lib/app/entities/validated_entity.rb
188
+ - lib/app/helpers/failing.rb
189
+ - lib/app/helpers/output.rb
190
+ - lib/app/helpers/output_helper.rb
191
+ - lib/app/modules/console_user_interaction.rb
192
+ - lib/codebreaker_gem.rb
193
+ - lib/codebreaker_gem/version.rb
194
+ - pkg/codebreaker_gem-0.1.0.gem
195
+ homepage: https://github.com/andreiiagodka/codebreaker_gem.git
196
+ licenses:
197
+ - MIT
198
+ metadata: {}
199
+ post_install_message:
200
+ rdoc_options: []
201
+ require_paths:
202
+ - lib
203
+ required_ruby_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ required_rubygems_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ requirements: []
214
+ rubyforge_project:
215
+ rubygems_version: 2.7.6
216
+ signing_key:
217
+ specification_version: 4
218
+ summary: CodebreakerAndreiiagodka
219
+ test_files: []