codebreaker_artuomka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eaea4703cab30a526c86cea603a3f0585d023cc5deff505cc8442d36c41742a2
4
+ data.tar.gz: fd3c2ae32e413c3e2305b50400e96e9e8e80369be291fba7f5da1583dc30e6e0
5
+ SHA512:
6
+ metadata.gz: c05ec1703be1ec07bd2add90e935e1d7edbe09366af1d461874ccffd6b4102bd4c6d3952db301fa0034df5ca9bd965680d2787bd4ede8ae89914f4b7147d4633
7
+ data.tar.gz: a148060732dc51c709fd8ad636f2ef420be681cb945529a54908ed96114b685c227cac86b6954b8dbf328b0e5539f759ea3666699dd06da043970ec2d237898f
@@ -0,0 +1,48 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ branches:
5
+ only:
6
+ - master
7
+ - development
8
+ docker:
9
+ - image: circleci/ruby:2.5.3-node-browsers
10
+ enviroment:
11
+ RAILS_ENV: test
12
+
13
+ working_directory: ~/repo
14
+
15
+ steps:
16
+ - checkout
17
+
18
+ # Download and cache dependencies
19
+ - restore_cache:
20
+ keys:
21
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
22
+ # fallback to using the latest cache if no exact match is found
23
+ - v1-dependencies-
24
+
25
+ - run:
26
+ name: install dependencies
27
+ command: |
28
+ gem update --system
29
+ gem install bundler
30
+ bundle install
31
+ - save_cache:
32
+ paths:
33
+ - ./vendor/bundle
34
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
35
+
36
+ # run tests!
37
+ - run:
38
+ name: run tests
39
+ command: |
40
+ mkdir /tmp/test-results
41
+ bundle exec rspec --format progress \
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
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea
10
+ .rubocop.yml
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 2.0.2
@@ -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 r.tem_negreyev@meta.ua. 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/
@@ -0,0 +1,39 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'codebreaker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'codebreaker_artuomka'
7
+ spec.version = Codebreaker::VERSION
8
+ spec.authors = ['Artuomka']
9
+ spec.email = ['r.tem_negreyev@meta.ua']
10
+
11
+ spec.summary = 'codebreaker application'
12
+ spec.description = 'Some logic game'
13
+ spec.homepage = 'https://github.com/Artuomka/codebreaker_gem'
14
+ spec.license = 'MIT'
15
+
16
+ if spec.respond_to?(:metadata)
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+ else
19
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
20
+ 'public gem pushes.'
21
+ end
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 2.0'
33
+ spec.add_development_dependency 'fasterer'
34
+ spec.add_development_dependency 'i18n'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'rubocop'
38
+ spec.add_development_dependency 'rubocop-rspec'
39
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'simplecov', require: false, group: :test
3
+ # Specify your gem's dependencies in codebreaker.gemspec
4
+ gemspec
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker_artuomka (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ colorize (0.8.1)
11
+ concurrent-ruby (1.1.5)
12
+ diff-lcs (1.3)
13
+ docile (1.3.2)
14
+ fasterer (0.8.0)
15
+ colorize (~> 0.7)
16
+ ruby_parser (>= 3.14.1)
17
+ i18n (1.7.0)
18
+ concurrent-ruby (~> 1.0)
19
+ jaro_winkler (1.5.4)
20
+ json (2.2.0)
21
+ parallel (1.19.1)
22
+ parser (2.6.5.0)
23
+ ast (~> 2.4.0)
24
+ rainbow (3.0.0)
25
+ rake (10.5.0)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.0)
31
+ rspec-support (~> 3.9.0)
32
+ rspec-expectations (3.9.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.0)
39
+ rubocop (0.76.0)
40
+ jaro_winkler (~> 1.5.1)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.6)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 1.4.0, < 1.7)
46
+ rubocop-rspec (1.36.0)
47
+ rubocop (>= 0.68.1)
48
+ ruby-progressbar (1.10.1)
49
+ ruby_parser (3.14.1)
50
+ sexp_processor (~> 4.9)
51
+ sexp_processor (4.13.0)
52
+ simplecov (0.17.1)
53
+ docile (~> 1.1)
54
+ json (>= 1.8, < 3)
55
+ simplecov-html (~> 0.10.0)
56
+ simplecov-html (0.10.2)
57
+ unicode-display_width (1.6.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler (~> 2.0)
64
+ codebreaker_artuomka!
65
+ fasterer
66
+ i18n
67
+ rake (~> 10.0)
68
+ rspec (~> 3.0)
69
+ rubocop
70
+ rubocop-rspec
71
+ simplecov
72
+
73
+ BUNDLED WITH
74
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Artuomka
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.
@@ -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'
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).
@@ -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
@@ -0,0 +1,11 @@
1
+ require 'i18n'
2
+ require 'yaml'
3
+ require_relative 'lib/codebreaker/i18n_config'
4
+ require_relative 'lib/codebreaker/helpers/validator'
5
+ require_relative 'lib/codebreaker/entities/game'
6
+ require_relative 'lib/codebreaker/entities/storage'
7
+ require_relative 'lib/codebreaker/entities/controller'
8
+ require_relative 'lib/codebreaker/entities/menu'
9
+ require_relative 'lib/codebreaker/entities/viewer'
10
+ require_relative 'lib/codebreaker/entities/statistics'
11
+ require_relative 'lib/codebreaker/version'
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'codebreaker'
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+ # require "pry"
10
+ # Pry.start
11
+
12
+ require 'irb'
13
+ IRB.start(__FILE__)
@@ -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,15 @@
1
+ require 'codebreaker/version'
2
+ require 'yaml'
3
+ require_relative '../autoload'
4
+
5
+ module Codebreaker
6
+ require_relative 'codebreaker/i18n_config'
7
+ require_relative 'codebreaker/helpers/validator'
8
+ require_relative 'codebreaker/entities/game'
9
+ require_relative 'codebreaker/entities/storage'
10
+ require_relative 'codebreaker/entities/controller'
11
+ require_relative 'codebreaker/entities/menu'
12
+ require_relative 'codebreaker/entities/viewer'
13
+ require_relative 'codebreaker/entities/statistics'
14
+ require_relative 'codebreaker/version'
15
+ end
@@ -0,0 +1,36 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Controller
4
+ GUESSED_NUMBER_SYMBOL = '+'.freeze
5
+ UNEXPECTED_NUMBER_SYMBOL = '-'.freeze
6
+
7
+ attr_reader :guess, :code, :result
8
+
9
+ def secret_code_handler(code, guess)
10
+ @code = code.split('')
11
+ @guess = guess.split('')
12
+ guessed_numbers.join + guessed_numbers_error_posit.join
13
+ end
14
+
15
+ private
16
+
17
+ def guessed_numbers
18
+ code.map.with_index do |_key, index|
19
+ next unless code[index] == guess[index]
20
+
21
+ @guess[index], @code[index] = nil
22
+ GUESSED_NUMBER_SYMBOL
23
+ end
24
+ end
25
+
26
+ def guessed_numbers_error_posit
27
+ guess.compact.map do |number|
28
+ next unless @code.include?(number)
29
+
30
+ @code.delete_at(code.index(number))
31
+ UNEXPECTED_NUMBER_SYMBOL
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,79 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Game
4
+ COUNT_NUMBERS = 4
5
+ CODE_RANGE = (1..6).freeze
6
+ DIFFICULTIES =
7
+ {
8
+ easy:
9
+ {
10
+ attempts: 15,
11
+ hints: 2
12
+ },
13
+ medium:
14
+ {
15
+ attempts: 10,
16
+ hints: 1
17
+ },
18
+ hard:
19
+ {
20
+ attempts: 5,
21
+ hints: 1
22
+ }
23
+ }.freeze
24
+
25
+ attr_reader :attempts, :hints, :code
26
+
27
+ def initialize
28
+ @controller = Controller.new
29
+ end
30
+
31
+ def init_game(difficulty)
32
+ @difficulty = difficulty
33
+ @code = generate_code
34
+ @hints = @code.sample(difficulty[:hints])
35
+ @attempts = difficulty[:attempts]
36
+ end
37
+
38
+ def start(guess)
39
+ @controller.secret_code_handler(code.join, guess)
40
+ end
41
+
42
+ def check_win?(answer)
43
+ code.join == answer
44
+ end
45
+
46
+ def hints_not_remain?
47
+ hints.empty?
48
+ end
49
+
50
+ def attempt_wasted
51
+ @attempts -= 1
52
+ end
53
+
54
+ def hint_take
55
+ hints.pop
56
+ end
57
+
58
+ def to_h(name)
59
+ {
60
+ name: name,
61
+ difficulty: DIFFICULTIES.key(@difficulty),
62
+ all_attempts: @difficulty[:attempts],
63
+ all_hints: @difficulty[:hints],
64
+ attempts_wasted: @difficulty[:attempts] - @attempts,
65
+ hints_wasted: @difficulty[:hints] - @hints.length,
66
+ date: Time.now
67
+ }
68
+ end
69
+
70
+ private
71
+
72
+ def generate_code
73
+ Array.new(COUNT_NUMBERS) do
74
+ rand CODE_RANGE
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,162 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Menu
4
+ include Helpers::Validator
5
+
6
+ attr_reader :storage, :viewer, :game, :guess
7
+
8
+ COMMANDS_LIST = {
9
+ start: 'start',
10
+ exit: 'exit',
11
+ rules: 'rules',
12
+ stats: 'stats'
13
+ }.freeze
14
+
15
+ CONFIRM_COMMAND = {
16
+ yes: 'yes'
17
+ }.freeze
18
+
19
+ HINT_COMMAND = 'hint'.freeze
20
+ MIN_SIZE = 3
21
+ MAX_SIZE = 25
22
+
23
+ def initialize
24
+ @storage = Storage.new
25
+ @viewer = Viewer.new
26
+ @game = Game.new
27
+ @statistics = Statistics.new
28
+ end
29
+
30
+ def game_menu
31
+ viewer.start_message
32
+
33
+ choice_menu_process(ask(:choice_options, commands: COMMANDS_LIST.keys.join(' | ')))
34
+ end
35
+
36
+ def rules
37
+ viewer.rules
38
+ game_menu
39
+ end
40
+
41
+ def start
42
+ @name = user_registration
43
+ level_choice
44
+ game_process
45
+ end
46
+
47
+ def stats
48
+ scores = storage.load
49
+ render_stats(@statistics.stats(scores)) if scores
50
+ game_menu
51
+ end
52
+
53
+ def ask(phrase_key = nil, options = {})
54
+ viewer.message(phrase_key, options) if phrase_key
55
+ gets.chomp
56
+ end
57
+
58
+ def save_result
59
+ storage.save_game_results(game.to_h(@name)) if ask(:save_results_message) == CONFIRM_COMMAND[:yes]
60
+ end
61
+
62
+ def name_valid?(name)
63
+ !empty_check?(name) && size_check(name, MIN_SIZE, MAX_SIZE)
64
+ end
65
+
66
+ def level_choice
67
+ loop do
68
+ level = ask(:difficulty_level, levels: Game::DIFFICULTIES.keys.join(' | '))
69
+
70
+ return generate_game(Game::DIFFICULTIES[level.to_sym]) if Game::DIFFICULTIES[level.to_sym]
71
+ return game_menu if level == COMMANDS_LIST[:exit]
72
+
73
+ viewer.command_error
74
+ end
75
+ end
76
+
77
+ def generate_game(difficulty)
78
+ game.init_game(difficulty)
79
+ viewer.message(:difficulty,
80
+ hints: difficulty[:hints],
81
+ attempts: difficulty[:attempts])
82
+ end
83
+
84
+ def game_process
85
+ while game.attempts.positive?
86
+ @guess = ask
87
+ return handle_win if game.check_win?(guess)
88
+
89
+ choice_code_process
90
+ end
91
+ handle_lose
92
+ end
93
+
94
+ def choice_code_process
95
+ case guess
96
+ when HINT_COMMAND then hint_process
97
+ when COMMANDS_LIST[:exit] then game_menu
98
+ else handle_command
99
+ end
100
+ end
101
+
102
+ def handle_command
103
+ return viewer.command_error unless format_check?(guess)
104
+
105
+ p game.start(guess)
106
+ viewer.round_message
107
+ game.attempt_wasted
108
+ end
109
+
110
+ def handle_win
111
+ viewer.win_game_message
112
+ save_result
113
+ game_menu
114
+ end
115
+
116
+ def handle_lose
117
+ viewer.lost_game_message(game.code)
118
+ game_menu
119
+ end
120
+
121
+ def hint_process
122
+ return viewer.no_hints_message if game.hints_not_remain?
123
+
124
+ viewer.print_hint(game.hint_take)
125
+ end
126
+
127
+ def exit_from_game
128
+ viewer.goodbye_message
129
+ exit
130
+ end
131
+
132
+ def choice_menu_process(command)
133
+ case command
134
+ when COMMANDS_LIST[:start] then start
135
+ when COMMANDS_LIST[:exit] then exit_from_game
136
+ when COMMANDS_LIST[:rules] then rules
137
+ when COMMANDS_LIST[:stats] then stats
138
+ else
139
+ viewer.command_error
140
+ game_menu
141
+ end
142
+ end
143
+
144
+ def user_registration
145
+ loop do
146
+ name = ask(:registration)
147
+
148
+ return name if name_valid?(name)
149
+
150
+ viewer.registration_name_length_error
151
+ end
152
+ end
153
+
154
+ def render_stats(list)
155
+ list.each_with_index do |key, index|
156
+ puts "#{index + 1}: "
157
+ key.each { |param, value| puts "#{param}:#{value}" }
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,22 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Statistics
4
+ def stats(list)
5
+ difficulties = list.group_by { |score| score[:difficulty] }
6
+ %i[hard 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_wasted], score[:hints_wasted]] }.reverse
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Storage
4
+ FILE_NAME = 'data.yml'.freeze
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 file_exists?
13
+ end
14
+
15
+ def save(object)
16
+ File.open(FILE_NAME, 'w') { |file| file.write(YAML.dump(object)) }
17
+ end
18
+
19
+ def save_game_results(object)
20
+ create unless file_exists?
21
+ save(load.push(object))
22
+ end
23
+
24
+ private
25
+
26
+ def file_exists?
27
+ File.exist?(FILE_NAME)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ module Codebreaker
2
+ module Entities
3
+ class Viewer
4
+ def message(message, hash = {})
5
+ puts I18n.t(message, hash)
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 win_game_message
21
+ message(:win_game_message)
22
+ end
23
+
24
+ def lost_game_message(code)
25
+ message(:lost_game_message, code: code)
26
+ end
27
+
28
+ def save_results_message
29
+ message(:save_results_message)
30
+ end
31
+
32
+ def round_message
33
+ message(:round_message)
34
+ end
35
+
36
+ def no_hints_message
37
+ message(:have_no_hints_message)
38
+ end
39
+
40
+ def print_hint(hint)
41
+ message(:print_hint_number, code: hint)
42
+ end
43
+
44
+ def registration_name_length_error
45
+ message(:registration_name_length_error)
46
+ end
47
+
48
+ def command_error
49
+ message(:command_error)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ module Codebreaker
2
+ module Helpers
3
+ module Validator
4
+ FORMAT_VALUES = /^[1-6]{4}$/.freeze
5
+
6
+ def empty_check?(value)
7
+ value.chomp.empty?
8
+ end
9
+
10
+ def format_check?(value)
11
+ value =~ FORMAT_VALUES
12
+ end
13
+
14
+ def size_check(value, min_size, max_size)
15
+ value.chomp.size.between?(min_size, max_size)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ I18n.load_path << Dir[File.expand_path('lib/codebreaker/locales/') + '/*.yml']
2
+ I18n.config.available_locales = :en
@@ -0,0 +1,44 @@
1
+ en:
2
+ start_message: "Welcome to The Game"
3
+ choice_options: "Choose option:
4
+ %{commands}"
5
+ difficulty_level: "What difficulty do you want to play?
6
+ %{levels}"
7
+ goodbye_message: "Good luck and good bye!"
8
+ command_error: "Unexpected command. Please choose one from listed commands"
9
+
10
+ rules: "codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker.
11
+ The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers
12
+ between 1 and 6.
13
+ The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the
14
+ codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with up to 4 signs - + or - or empty
15
+ spaces.
16
+ A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code
17
+ and in the same position. For example:
18
+ Secret number - 1234
19
+ Input number - 6264
20
+ Number of pluses - 2 (second and fourth position)
21
+ A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code
22
+ but in a different position. For example:
23
+ Secret number - 1234
24
+ Input number - 6462
25
+ Number of minuses - 2 (second and fourth position)
26
+ An empty space indicates that there is not a current digit in a secret number.
27
+ If codebreaker inputs the exact number as a secret number - codebreaker wins the game.
28
+ If all attempts are spent - codebreaker loses.
29
+ codebreaker also has some number of hints(depends on chosen difficulty).
30
+ If a user takes a hint - he receives back a separate digit of the secret code.
31
+ "
32
+ registration: "Introduce yourself, please "
33
+ difficulty: "You had to %{hints} hints and %{attempts} attempts"
34
+ round_message: "1. Enter your secret code
35
+ 2. hint
36
+ 3. exit "
37
+ guess: "Incorrect code! Secret code must to be from four digits in range from 1 to 6"
38
+ lost_game_message: "You spent all attempts and lose a game... Your code was: %{code}"
39
+ win_game_message: "Congratulations! You won the game"
40
+ save_results_message: "Do you want to save result? 1. YES 2. NO"
41
+ have_no_hints_message: "You have no more hints"
42
+ print_hint_number: "Hint number: %{code}"
43
+ registration_name_length_error: "Yor name is incorrect.
44
+ It must be more than 3 and less than 20 characters."
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/main.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'autoload.rb'
2
+
3
+ Codebreaker::Entities::Menu.new.game_menu
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_artuomka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artuomka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-15 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.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
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-rspec
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: Some logic game
112
+ email:
113
+ - r.tem_negreyev@meta.ua
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".circleci/config.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Codebreaker.gemspec
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE.txt
127
+ - README.md
128
+ - Rakefile
129
+ - autoload.rb
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/codebreaker.rb
133
+ - lib/codebreaker/entities/controller.rb
134
+ - lib/codebreaker/entities/game.rb
135
+ - lib/codebreaker/entities/menu.rb
136
+ - lib/codebreaker/entities/statistics.rb
137
+ - lib/codebreaker/entities/storage.rb
138
+ - lib/codebreaker/entities/viewer.rb
139
+ - lib/codebreaker/helpers/validator.rb
140
+ - lib/codebreaker/i18n_config.rb
141
+ - lib/codebreaker/locales/en.yml
142
+ - lib/codebreaker/version.rb
143
+ - main.rb
144
+ homepage: https://github.com/Artuomka/codebreaker_gem
145
+ licenses:
146
+ - MIT
147
+ metadata:
148
+ allowed_push_host: https://rubygems.org
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.7.6
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: codebreaker application
169
+ test_files: []