codebreaker_rg_game 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: 5e77bf5909283e226abc241bc4b3bfaec0d93fa8eae3637b43143a1d7c6edb99
4
+ data.tar.gz: c8d2c5729c800830470abbf7fdc57916c850ee376a77d0d9893248868de06c6f
5
+ SHA512:
6
+ metadata.gz: ede2c78bcb5b6b8f5677158ee05534dd547ade83705678e7104d9bd88de6cdaaf0f9afc4e5f1d0ae97eef4bdcec3a66a60ee21ad2000df8981a75706ca729e61
7
+ data.tar.gz: 88c9628a7a2789486b9e595e3951aa1d71671cbf49678fefa60a215ee174cbe58d54a7201ea75a78dbdfd18b85eaac1f5c81a95e98253cbe938bfbcceadfa566
data/.fasterer.yml ADDED
@@ -0,0 +1,22 @@
1
+ speedups:
2
+ rescue_vs_respond_to: true
3
+ module_eval: true
4
+ shuffle_first_vs_sample: true
5
+ for_loop_vs_each: true
6
+ each_with_index_vs_while: false
7
+ map_flatten_vs_flat_map: true
8
+ reverse_each_vs_reverse_each: true
9
+ select_first_vs_detect: true
10
+ sort_vs_sort_by: true
11
+ fetch_with_argument_vs_block: true
12
+ keys_each_vs_each_key: true
13
+ hash_merge_bang_vs_hash_brackets: true
14
+ block_vs_symbol_to_proc: true
15
+ proc_call_vs_yield: true
16
+ gsub_vs_tr: true
17
+ select_last_vs_reverse_detect: true
18
+ getter_vs_attr_reader: true
19
+ setter_vs_attr_writer: true
20
+
21
+ exclude_paths:
22
+ - 'spec/**/*.rb'
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /database/*.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Documentation:
4
+ Enabled: false
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ - 'Gemfile'
8
+ - 'spec/**/*.rb'
9
+
data/CHANGELOG.mb ADDED
File without changes
@@ -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 zahoruiko.yaroslav@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,9 @@
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
+ gemspec
8
+
9
+
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker_rg_game (3.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.5)
13
+ diff-lcs (1.3)
14
+ docile (1.3.2)
15
+ fasterer (0.5.1)
16
+ colorize (~> 0.7)
17
+ ruby_parser (>= 3.13.0)
18
+ i18n (1.6.0)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.3)
21
+ json (2.2.0)
22
+ method_source (0.9.2)
23
+ parallel (1.17.0)
24
+ parser (2.6.3.0)
25
+ ast (~> 2.4.0)
26
+ pry (0.12.2)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ rainbow (3.0.0)
30
+ rake (10.5.0)
31
+ rspec (3.8.0)
32
+ rspec-core (~> 3.8.0)
33
+ rspec-expectations (~> 3.8.0)
34
+ rspec-mocks (~> 3.8.0)
35
+ rspec-core (3.8.2)
36
+ rspec-support (~> 3.8.0)
37
+ rspec-expectations (3.8.4)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-mocks (3.8.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-support (3.8.2)
44
+ rubocop (0.69.0)
45
+ jaro_winkler (~> 1.5.1)
46
+ parallel (~> 1.10)
47
+ parser (>= 2.6)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 1.7)
51
+ ruby-progressbar (1.10.1)
52
+ ruby_parser (3.13.1)
53
+ sexp_processor (~> 4.9)
54
+ sexp_processor (4.12.1)
55
+ simplecov (0.16.1)
56
+ docile (~> 1.1)
57
+ json (>= 1.8, < 3)
58
+ simplecov-html (~> 0.10.0)
59
+ simplecov-html (0.10.2)
60
+ unicode-display_width (1.6.0)
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ bundler (~> 2.0)
67
+ codebreaker_rg_game!
68
+ fasterer (~> 0.5.1)
69
+ i18n (~> 1.6)
70
+ pry (~> 0.12.2)
71
+ rake (~> 10.0)
72
+ rspec (~> 3.8)
73
+ rubocop (~> 0.69.0)
74
+ simplecov (~> 0.16.1)
75
+
76
+ BUNDLED WITH
77
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Yaroslav Zahoruiko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Codebreaker
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codebreaker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'codebreaker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codebreaker
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and 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. 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 Codebreaker project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'codebreaker'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'codebreaker/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'codebreaker_rg_game'
9
+ spec.version = Codebreaker::VERSION
10
+ spec.authors = ['Yaroslav Zahoruiko']
11
+ spec.email = ['zahoruiko.yaroslav@gmail.com']
12
+
13
+ spec.summary = 'Codebreaker'
14
+ spec.description = 'game'
15
+ spec.homepage = 'https://github.com/YaroslavZahoruiko/Codebreaker'
16
+ spec.license = 'MIT'
17
+
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = 'https://github.com/YaroslavZahoruiko/Codebreaker'
22
+ spec.metadata['changelog_uri'] = 'https://github.com/YaroslavZahoruiko/Codebreaker/tree/devCHANGELOG.md'
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler', '~> 2.0'
34
+ spec.add_development_dependency 'fasterer', '~> 0.5.1'
35
+ spec.add_development_dependency 'i18n', '~> 1.6'
36
+ spec.add_development_dependency 'pry', '~> 0.12.2'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.8'
39
+ spec.add_development_dependency 'rubocop', '~> 0.69.0'
40
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
41
+ end
data/database/.keep ADDED
File without changes
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Game
5
+ STATE = {
6
+ continue: 'continue',
7
+ win: 'win',
8
+ lose: 'lose'
9
+ }.freeze
10
+ VALID_NUMBERS = (1..6).freeze
11
+ LENGTH_CODE = 4
12
+ CORRECT_SYMBOL = '+'
13
+ INCORRECT_SYMBOL = '-'
14
+ attr_reader :hints, :attempts, :state, :number
15
+ def initialize(player, difficulties)
16
+ @player = player
17
+ @difficulties = difficulties
18
+ @attempts = @difficulties.attempts
19
+ @hints = @difficulties.hints
20
+ @number = generator_secret_code
21
+ @available_hints = @number.clone
22
+ @state = STATE[:continue]
23
+ end
24
+
25
+ def hint
26
+ @hints -= 1
27
+ @available_hints.delete_at(rand(@available_hints.size))
28
+ end
29
+
30
+ def check_code(code)
31
+ return nil unless valid_code?(code)
32
+
33
+ @attempts -= 1
34
+ temp_code = code.chars
35
+ guess_number = Array.new(LENGTH_CODE) { 0 }
36
+ calc_and_mark_correct(temp_code, guess_number) + calc_and_mark_incorrect(temp_code, guess_number)
37
+ end
38
+
39
+ def to_h
40
+ {
41
+ player: @player.name,
42
+ difficulties: @difficulties.level,
43
+ attempts_total: @difficulties.attempts,
44
+ hints_total: @difficulties.hints,
45
+ attempts_used: @difficulties.attempts - @attempts,
46
+ hints_used: @difficulties.hints - @hints,
47
+ state: @state
48
+ }
49
+ end
50
+
51
+ def win?(code)
52
+ return false unless code == @number.join
53
+
54
+ @state = STATE[:win]
55
+ true
56
+ end
57
+
58
+ def hints?
59
+ !@hints.zero?
60
+ end
61
+
62
+ def lose?
63
+ return false unless @attempts.zero?
64
+
65
+ @state = STATE[:lose]
66
+ true
67
+ end
68
+
69
+ private
70
+
71
+ def calc_and_mark_correct(code, used)
72
+ result = ''
73
+ code.each_index do |index|
74
+ next unless number[index] == code[index]
75
+
76
+ result += CORRECT_SYMBOL
77
+ code[index], used[index] = nil
78
+ end
79
+ result
80
+ end
81
+
82
+ def calc_and_mark_incorrect(code, used)
83
+ result = ''
84
+ number.each_index do |number_index|
85
+ code_index = code.index(number[number_index])
86
+ next if code_index.nil? || used[number_index].nil?
87
+
88
+ result += INCORRECT_SYMBOL
89
+ code[code_index], used[number_index] = nil
90
+ end
91
+ result
92
+ end
93
+
94
+ def generator_secret_code
95
+ Array.new(4) { rand(VALID_NUMBERS).to_s }
96
+ end
97
+
98
+ def valid_code?(code)
99
+ Verifier.verify_valid_code(code)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Console
4
+ include Codebreaker
5
+ COMMANDS = {
6
+ start: 'start',
7
+ rules: 'rules',
8
+ stats: 'stats',
9
+ exit: 'exit',
10
+ hint: 'hint',
11
+ yes: 'y',
12
+ no: 'n',
13
+ win: 'win'
14
+ }.freeze
15
+
16
+ def initialize
17
+ @state = :choice_main
18
+ end
19
+
20
+ def run
21
+ loop do
22
+ send(@state)
23
+ break if @state == :exit
24
+ end
25
+ end
26
+
27
+ def welcome
28
+ show :welcome
29
+ end
30
+
31
+ def goodbye
32
+ show :goodbye
33
+ end
34
+
35
+ private
36
+
37
+ def choice_main
38
+ show(:menu)
39
+ case input
40
+ when COMMANDS[:start] then @state = :register_name
41
+ when COMMANDS[:rules] then @state = :rules
42
+ when COMMANDS[:stats] then @state = :output_stats
43
+ when COMMANDS[:exit] then @state = :exit
44
+ else show(:invalid_command)
45
+ end
46
+ end
47
+
48
+ def register_name
49
+ show(:name_request)
50
+
51
+ name = input
52
+
53
+ @player = Player.new(name)
54
+ @player.validate ? @state = :register_difficulty : show(:invalid_name)
55
+ end
56
+
57
+ def register_difficulty
58
+ show(:difficulty_request)
59
+
60
+ difficult = input.downcase
61
+
62
+ if Difficulties.valid_difficult(difficult)
63
+ @difficult = Difficulties.new(difficult)
64
+ @state = :create_game
65
+ else
66
+ show(:invalid_difficulty)
67
+ end
68
+ end
69
+
70
+ def create_game
71
+ @codebreaker = Game.new(@player, @difficult)
72
+ @state = :game_process
73
+ end
74
+
75
+ def game_process
76
+ return @state = :lose if @codebreaker.lose?
77
+
78
+ show(:game_process)
79
+ code = input
80
+ return @state = :hint if hint?(code)
81
+
82
+ return @state = :win if @codebreaker.win?(code)
83
+
84
+ result = @codebreaker.check_code(code)
85
+ return show(:invalid_code) if result.nil?
86
+
87
+ puts result
88
+ end
89
+
90
+ def win
91
+ show(:win_game_message)
92
+ @state = :save_stats
93
+ end
94
+
95
+ def lose
96
+ show(:lost_game_message, code: @codebreaker.number.join)
97
+ @state = :choice_main
98
+ end
99
+
100
+ def hint
101
+ @codebreaker.hints? ? show(:hint_message, hint: @codebreaker.hint) : show(:no_hints_message)
102
+ @state = :game_process
103
+ end
104
+
105
+ def rules
106
+ show(:rules)
107
+ @state = :choice_main
108
+ end
109
+
110
+ def save_stats
111
+ show(:save_result)
112
+ if input == COMMANDS[:yes]
113
+ Statistics.save(@codebreaker.to_h)
114
+ @state = :choice_main
115
+ elsif input == COMMANDS[:no]
116
+ @state = :choice_main
117
+ end
118
+ end
119
+
120
+ def output_stats
121
+ puts Statistics.output
122
+ @state = :choice_main
123
+ end
124
+
125
+ def hint?(code)
126
+ code == COMMANDS[:hint]
127
+ end
128
+
129
+ def show(message, optional = {})
130
+ puts I18n.t(message, optional)
131
+ end
132
+
133
+ def input
134
+ user_input = gets.chomp
135
+ user_input.downcase == COMMANDS[:exit] ? @state = :exit : user_input
136
+ end
137
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Difficulties
5
+ LEVELS = {
6
+ easy: {
7
+ attempts: 15,
8
+ hints: 2
9
+ },
10
+ medium: {
11
+ attempts: 10,
12
+ hints: 2
13
+ },
14
+ hell: {
15
+ attempts: 5,
16
+ hints: 1
17
+ }
18
+ }.freeze
19
+ attr_reader :level, :attempts, :hints
20
+
21
+ def initialize(difficulties)
22
+ @level = difficulties
23
+ @attempts = LEVELS[difficulties.to_sym][:attempts]
24
+ @hints = LEVELS[difficulties.to_sym][:hints]
25
+ end
26
+
27
+ class << self
28
+ def valid_difficult(difficulties)
29
+ Verifier.verify_valid_difficult(difficulties)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Player
5
+ MAX_LENGTH_NAME = 20
6
+ MIN_LENGTH_NAME = 3
7
+ attr_reader :name
8
+
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def validate
14
+ Verifier.verify_valid_name(@name)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Statistics
5
+ class << self
6
+ def save(stats)
7
+ YamlMapper.save(stats)
8
+ end
9
+
10
+ def output
11
+ stats = load
12
+ sort(stats)
13
+ hash_stats = {}
14
+ stats.each_index do |index|
15
+ hash_stats[index] = stats[index].to_s
16
+ end
17
+ hash_stats.to_a
18
+ end
19
+
20
+ private
21
+
22
+ def sort(array_stats)
23
+ array_stats.sort_by! { |stats| [stats[:attempts_used], stats[:hints_used]] }.reverse
24
+ end
25
+
26
+ def load
27
+ YamlMapper.load
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Verifier
5
+ COMMANDS = {
6
+ easy: 'easy',
7
+ medium: 'medium',
8
+ hell: 'hell'
9
+ }.freeze
10
+
11
+ class << self
12
+ def verify_valid_name(name)
13
+ name.is_a?(String) && verify_size?(name, min_size: Player::MIN_LENGTH_NAME, max_size: Player::MAX_LENGTH_NAME)
14
+ end
15
+
16
+ def verify_valid_difficult(difficult)
17
+ COMMANDS.values.include?(difficult)
18
+ end
19
+
20
+ def verify_valid_code(code)
21
+ return false unless code.is_a?(String)
22
+
23
+ return false unless verify_size?(code, min_size: Game::LENGTH_CODE, max_size: Game::LENGTH_CODE)
24
+
25
+ verify_include?(code)
26
+ end
27
+
28
+ private
29
+
30
+ def verify_include?(code)
31
+ code.chars.all? { |char| Game::VALID_NUMBERS.include?(char.to_i) }
32
+ end
33
+
34
+ def verify_size?(value, min_size:, max_size:)
35
+ value.size.between?(min_size, max_size)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class YamlMapper
5
+ PATH = 'database/codebreaker_db.yml'
6
+ class << self
7
+ def save(object)
8
+ stats = load
9
+ stats << object
10
+ File.open(PATH, 'w') do |file|
11
+ file.write(stats.to_yaml)
12
+ end
13
+ end
14
+
15
+ def load
16
+ File.file?(PATH) ? YAML.load(File.read(PATH)) : []
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ VERSION = '1.0'
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'codebreaker/version'
4
+ require_relative 'dependency'
5
+ module Codebreaker
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ I18n.config.load_path << Dir[File.expand_path('lib/locales') + '/*.yml']
4
+ I18n.config.available_locales = :en
5
+ I18n.default_locale = :en
data/lib/dependency.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require 'pry'
5
+ require_relative 'codebreaker/utils/verifier'
6
+ require_relative 'codebreaker/entities/console'
7
+ require_relative 'codebreaker/entities/player'
8
+ require_relative 'codebreaker/codebreaker'
9
+ require_relative 'codebreaker/utils/yaml_mapper'
10
+ require_relative 'config/i18n_config'
11
+ require_relative 'codebreaker/entities/difficulties'
12
+ require_relative 'codebreaker/entities/statistics'
@@ -0,0 +1,49 @@
1
+ en:
2
+ welcome: "Welcome to codebreaker game."
3
+
4
+ goodbye: "\nGoodbye!"
5
+
6
+ lost_game_message: "Oh, your attempts ended... Your code was: %{code}"
7
+
8
+ no_hints_message: "Oh, your hints ended"
9
+
10
+ win_game_message: "Yayy, u won the game! Congrats!"
11
+
12
+ menu: "\nStart\nStats\nRules\nExit\n"
13
+
14
+ hint_message: "\n hint: %{hint}"
15
+
16
+ name_request: "\nType your name"
17
+
18
+ difficulty_request: "\nChoose difficulty\nEasy\nMedium\nHell"
19
+
20
+ save_result: "\n\nAre you want to save your score? (Y/n)"
21
+
22
+ game_process: "\nChoose one from option below or type your guess
23
+ \nType 'hint'
24
+ \nType 'exit'
25
+ \nYour guess\n"
26
+
27
+ invalid_name: "\n Name must be between 3 and 20 characters long!"
28
+
29
+ invalid_difficulty: "\n Invalid difficulty!"
30
+
31
+ invalid_command: "\n You have passed unexpected command. Please choose one from listed commands"
32
+
33
+ invalid_code: "\n code must be number, required, length - 4 digits, each digit is a number in the range 1-6"
34
+
35
+ rules: "codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker.
36
+ \n 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.
37
+ \n 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.
38
+ \n The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
39
+ \n 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:
40
+ \n Secret number - 1234
41
+ \n Input number - 6264
42
+ \n Number of pluses - 2 (second and fourth position)
43
+ \n 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:
44
+ \n Secret number - 1234
45
+ \n Input number - 6462
46
+ \n Number of minuses - 2 (second and fourth position)
47
+ \n An empty space indicates that there is not a current digit in a secret number.
48
+ \n If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
49
+ \n 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."
data/main.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/dependency'
4
+ console = Console.new
5
+ console.welcome
6
+ console.run
7
+ console.goodbye
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_rg_game
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Zahoruiko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-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.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.12.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.8'
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.69.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.69.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.16.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.16.1
125
+ description: game
126
+ email:
127
+ - zahoruiko.yaroslav@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".fasterer.yml"
133
+ - ".gitignore"
134
+ - ".rubocop.yml"
135
+ - CHANGELOG.mb
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - codebreaker.gemspec
145
+ - database/.keep
146
+ - lib/codebreaker.rb
147
+ - lib/codebreaker/codebreaker.rb
148
+ - lib/codebreaker/entities/console.rb
149
+ - lib/codebreaker/entities/difficulties.rb
150
+ - lib/codebreaker/entities/player.rb
151
+ - lib/codebreaker/entities/statistics.rb
152
+ - lib/codebreaker/utils/verifier.rb
153
+ - lib/codebreaker/utils/yaml_mapper.rb
154
+ - lib/codebreaker/version.rb
155
+ - lib/config/i18n_config.rb
156
+ - lib/dependency.rb
157
+ - lib/locales/en.yml
158
+ - main.rb
159
+ homepage: https://github.com/YaroslavZahoruiko/Codebreaker
160
+ licenses:
161
+ - MIT
162
+ metadata:
163
+ allowed_push_host: https://rubygems.org
164
+ homepage_uri: https://github.com/YaroslavZahoruiko/Codebreaker
165
+ source_code_uri: https://github.com/YaroslavZahoruiko/Codebreaker
166
+ changelog_uri: https://github.com/YaroslavZahoruiko/Codebreaker/tree/devCHANGELOG.md
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.7.6
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Codebreaker
187
+ test_files: []