codebreaker-Bulatkin 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 650cb47ef623f29bf16ac29db47c79152674e0ad0927cc985c441af3213f526e
4
+ data.tar.gz: 24ec2b0c64f5fa568cc6eabeaf318e290d08403258b782159fe8992eb90e824f
5
+ SHA512:
6
+ metadata.gz: 1c87325aac46da9c0815e4b0d24aba5245f4f5ffc834df069218c4f134f79e047bf2165c70b41563477b4d6cd89823f81deb02d95d8e368069f235843a54b374
7
+ data.tar.gz: 0b825164cf97721a3c34767ccc06c9ab39084c28ba980588d9596b5051cbe6a592c29fbffa197ee02d2f45531c291039c4dcd9e11719692175ffaf4121a3f0da
@@ -0,0 +1 @@
1
+ 2.6.3
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /db/
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,2 @@
1
+ Metrics/LineLength:
2
+ Enabled: 120
@@ -0,0 +1 @@
1
+ codebreaker
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
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 yevhenii@rubygarage.org. 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,7 @@
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
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker-Bulatkin (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.5)
13
+ diff-lcs (1.3)
14
+ docile (1.3.2)
15
+ fasterer (0.7.1)
16
+ colorize (~> 0.7)
17
+ ruby_parser (>= 3.13.0)
18
+ i18n (1.7.0)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.4)
21
+ json (2.2.0)
22
+ method_source (0.9.2)
23
+ parallel (1.18.0)
24
+ parser (2.6.5.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.9.0)
32
+ rspec-core (~> 3.9.0)
33
+ rspec-expectations (~> 3.9.0)
34
+ rspec-mocks (~> 3.9.0)
35
+ rspec-core (3.9.0)
36
+ rspec-support (~> 3.9.0)
37
+ rspec-expectations (3.9.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-mocks (3.9.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-support (3.9.0)
44
+ rubocop (0.76.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.14.1)
53
+ sexp_processor (~> 4.9)
54
+ sexp_processor (4.13.0)
55
+ simplecov (0.17.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-Bulatkin!
68
+ fasterer
69
+ i18n
70
+ pry
71
+ rake (~> 10.0)
72
+ rspec (~> 3.0)
73
+ rubocop
74
+ simplecov
75
+
76
+ BUNDLED WITH
77
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -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. 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. 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).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ #!/usr/bin/env ruby
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'bundler/setup'
5
+ require 'codebreaker'
@@ -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,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'fasterer'
4
+
5
+ lib = File.expand_path('lib', __dir__)
6
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7
+ require 'codebreaker/version'
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = 'codebreaker-Bulatkin'
11
+ spec.version = Codebreaker::VERSION
12
+ spec.authors = ['Yevhenii Bulatkin']
13
+ spec.email = ['yevhenii@rubygarage.org']
14
+
15
+ spec.summary = 'Codebreaker game'
16
+ spec.description = 'Crack the code'
17
+ spec.homepage = 'https://github.com/Bulatkin/codebreaker'
18
+ spec.license = 'MIT'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been
22
+ # added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler', '~> 2.0'
31
+ spec.add_development_dependency 'fasterer'
32
+ spec.add_development_dependency 'i18n'
33
+ spec.add_development_dependency 'pry'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'rubocop'
37
+ spec.add_development_dependency 'simplecov'
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './config.rb'
4
+
5
+ # Codebreaker::Console.new.welcome_message
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Console
5
+ include Storage
6
+ attr_reader :game, :player, :guess
7
+
8
+ def welcome_message
9
+ output(:welcome_message)
10
+ run
11
+ end
12
+
13
+ private
14
+
15
+ def run
16
+ loop do
17
+ @game = Game.new
18
+ output(:game_navigation)
19
+ case user_input
20
+ when I18n.t(:start) then start_game_preparations
21
+ when I18n.t(:rule) then output(:rules)
22
+ when I18n.t(:stats) then statistic(sort_player(load))
23
+ when I18n.t(:exit) then exit
24
+ else output(:wrong_command)
25
+ end
26
+ end
27
+ end
28
+
29
+ def start_game_preparations
30
+ return unless registration && difficulty_setup
31
+
32
+ game_cycle
33
+ end
34
+
35
+ def registration
36
+ @player = Player.new
37
+ loop do
38
+ output(:output_name)
39
+ player.name = user_input
40
+ return true if player.valid?
41
+
42
+ puts player.errors.first
43
+ end
44
+ end
45
+
46
+ def game_cycle
47
+ output(:mode_info, game.difficulty)
48
+ until game.lost?
49
+ round_menu
50
+ @guess = user_input
51
+ hint_menu if guess == I18n.t(:hint)
52
+ mark if game.validate_guess(guess)
53
+ won if game.won?(guess)
54
+ end
55
+ lost
56
+ end
57
+
58
+ def difficulty_setup
59
+ loop do
60
+ output(:output_difficulty)
61
+ return true if game.set(user_input)
62
+ end
63
+ end
64
+
65
+ def round_menu
66
+ output(:remained_attempts, attempts: game.attempts)
67
+ output(:hints_left, hints: game.hint_keeper.size)
68
+ output(:input_guess)
69
+ end
70
+
71
+ def hint_menu
72
+ output(:no_hints_left) if game.hint_keeper.empty?
73
+ output(:take_hint, take_hint: game.take_hint!)
74
+ end
75
+
76
+ def mark
77
+ game.attempt_used && (puts play(guess))
78
+ end
79
+
80
+ def won
81
+ output(:game_won, secret_code: game.secret_code.join(''))
82
+ save_result
83
+ play_again
84
+ end
85
+
86
+ def lost
87
+ output(:game_lost, secret_code: game.secret_code.join(''))
88
+ end
89
+
90
+ def save_result
91
+ output(:save)
92
+ Storage.save(game.to_yaml(player.name)) if user_input == 'yes'
93
+ end
94
+
95
+ def play_again
96
+ output(:play_again)
97
+ user_input == 'yes' ? run : exit
98
+ end
99
+
100
+ def play(guess)
101
+ guess = guess.chars.map(&:to_i)
102
+ game.guess(guess)
103
+ end
104
+
105
+ def statistic(array)
106
+ puts I18n.t(:statistic)
107
+ rating = 1
108
+
109
+ array.each do |e|
110
+ header_part2 = "|#{e[:try]}\t\t|#{e[:hints_total]}\t\t|#{e[:hints_used]}\t\t|"
111
+ header_part1 = "|#{rating}\t|#{e[:name]}\t|#{e[:difficulty]}\t\t|#{e[:attempts]}\t\t"
112
+ puts header_part1 + header_part2
113
+ rating += 1
114
+ end
115
+ end
116
+
117
+ def user_input
118
+ input = gets.chomp
119
+ input == I18n.t(:exit) ? exit : input
120
+ end
121
+
122
+ def output(*message)
123
+ puts I18n.t(*message)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: false
2
+
3
+ module Codebreaker
4
+ class Game
5
+ include Validation
6
+ include Storage
7
+ attr_reader :secret_code, :attempts, :hints, :errors, :difficulty, :play,
8
+ :result, :player, :hint_keeper, :name, :storage
9
+
10
+ DIFFICULTY = {
11
+ hell: { attempts: 5, hint: 1, mode: 'hell' },
12
+ medium: { attempts: 10, hint: 1, mode: 'medium' },
13
+ easy: { attempts: 15, hint: 2, mode: 'easy' }
14
+ }.freeze
15
+
16
+ def initialize
17
+ @secret_code = 4.times.map { Random.rand(1..6) }
18
+ @errors = []
19
+ end
20
+
21
+ def set(user_input)
22
+ return unless DIFFICULTY.key?(user_input.to_sym)
23
+
24
+ @difficulty = DIFFICULTY.dig(user_input.to_sym)
25
+ @hints = @difficulty.dig(:hint)
26
+ @attempts = @difficulty.dig(:attempts)
27
+ @hint_keeper = @secret_code.sample(hints)
28
+ end
29
+
30
+ def take_hint!
31
+ hint_keeper.pop
32
+ end
33
+
34
+ def won?(guess)
35
+ secret_code.join == guess
36
+ end
37
+
38
+ def lost?
39
+ attempts.zero?
40
+ end
41
+
42
+ def to_yaml(name)
43
+ {
44
+ name: name,
45
+ difficulty: difficulty[:mode],
46
+ attempts_total: difficulty[:attempts],
47
+ attempts_used: difficulty[:attempts] - attempts,
48
+ hints_total: difficulty[:hint],
49
+ hints_used: difficulty[:hint] - hint_keeper.size
50
+ }.to_yaml
51
+ end
52
+
53
+ def attempt_used
54
+ @attempts -= 1
55
+ end
56
+
57
+ def validate_guess(guess)
58
+ guess_valid?(guess)
59
+ end
60
+
61
+ def guess(guess)
62
+ @result = '+' * exact_match_count(guess) + '-' * number_match_count(guess)
63
+ end
64
+
65
+ private
66
+
67
+ def exact_match_count(guess)
68
+ (0..3).inject(0) do |count, index|
69
+ count + (exact_match?(guess, index) ? 1 : 0)
70
+ end
71
+ end
72
+
73
+ def number_match_count(guess)
74
+ total_match_count(guess) - exact_match_count(guess)
75
+ end
76
+
77
+ def total_match_count(guess)
78
+ secret = @secret_code.dup
79
+ guess.inject(0) do |count, num|
80
+ count + (delete_first(secret, num) ? 1 : 0)
81
+ end
82
+ end
83
+
84
+ def delete_first(code, num)
85
+ code.delete_at(code.index(num)) if code.index(num)
86
+ end
87
+
88
+ def exact_match?(guess, index)
89
+ guess[index] == @secret_code[index]
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Player
5
+ include Validation
6
+ attr_reader :errors
7
+ attr_accessor :name
8
+
9
+ def valid?
10
+ validate
11
+ errors.empty?
12
+ end
13
+
14
+ private
15
+
16
+ def validate
17
+ @errors = []
18
+ errors << 'Name cannot be blank' if name.empty?
19
+ errors << 'Should be between 3-20 chars' unless size_correct?(name)
20
+ errors << 'Should consists of letters' unless class_valid?(name, String)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'pry'
5
+ require 'yaml'
6
+ require 'i18n'
7
+ require_relative 'modules/validation.rb'
8
+ require_relative 'modules/storage.rb'
9
+ require_relative 'codebreaker/version.rb'
10
+ require_relative 'codebreaker/player.rb'
11
+ require_relative 'codebreaker/game.rb'
12
+ require_relative 'codebreaker/console.rb'
13
+ require_relative 'i18n_config.rb'
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ I18n.load_path << Dir[File.expand_path('./locales/') + '/*.yml']
4
+ I18n.config.available_locales = :en
@@ -0,0 +1,59 @@
1
+ en:
2
+ welcome_message: 'Welcome to the Codebraker!'
3
+ game_navigation: "
4
+ ----------------------\n
5
+ (s)tart\n
6
+ (r)ules\n
7
+ s(t)ats\n
8
+ Exit\n
9
+ ----------------------\n"
10
+ start: 's'
11
+ rule: 'r'
12
+ stats: 't'
13
+ exit: 'exit'
14
+ save: 'Do you want to save the score?'
15
+ play_again: 'Do you want to play one more game?'
16
+ bye: 'Farewell'
17
+ remained_attempts: 'Number of remained attemtps: %{attempts}'
18
+ input_guess: "Please, input your guess\n"
19
+ game_won: 'You have won the game. The right code was: %{secret_code}'
20
+ game_lost: 'Sorry, you have just lost the game. The right code was: %{secret_code}'
21
+ mode_info: "You have chosen:\n
22
+ mode: %{mode}\n
23
+ attempts: %{attempts}\n
24
+ hints: %{hint}\n"
25
+ hint: 'hint'
26
+ statistic: "|Rating\t|Name\t|Difficulty\t|Attempts Total\t|Attempts Used\t|Hints Total\t|Hints Used\t|"
27
+ take_hint: 'The secret includes number: %{take_hint}'
28
+ hints_left: 'You have %{hints} hints more'
29
+ no_hints_left: 'No hints left'
30
+ game_end: "Want to play one more game?\n"
31
+ errors: "Following errors have occured: \n"
32
+ wrong_command: "You have passed unexpected command. Please choose one from listed commands\n"
33
+ output_name: 'Write your name, please:'
34
+ output_difficulty: "\nChoose desired mode:\n-- [hell]\n-- [medium]\n-- [easy]\n"
35
+ rules: "\t________________________________________________________________\n
36
+ \t|\t\t\tGame Rules\t\t\t\t|\n
37
+ \t|_______________________________________________________________|\n\n
38
+ \tCodebreaker is a logic game in which a code-breaker tries to break a\nsecret code
39
+ created by a code-maker. The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.\n
40
+
41
+ \tThe codebreaker gets some number of chances to break the code (depends\n on chosen difficulty). In each turn, the codebreaker makes a guess of\n 4 numbers. The codemaker then marks the guess with up to 4 signs - + or -\n or empty spaces.\n
42
+
43
+ \tA + indicates an exact match: one of the numbers in the guess is the\nsame as one of the
44
+ numbers in the secret code and in the same position.\n For example:
45
+ Secret number - 1234\n
46
+ Input number - 6264\n
47
+ Number of pluses - 2 (second and fourth position)\n\n
48
+
49
+ \tA - indicates a number match: one of the numbers in the guess is the\nsame as one of the numbers in the secret code but in a different position.\n For example:\n
50
+ Secret number - 1234\n
51
+ Input number - 6462\n
52
+ Number of minuses - 2 (second and fourth position)\n
53
+
54
+ \tAn empty space indicates that there is not a current digit in a secret\n number.\n
55
+
56
+ \tIf codebreaker inputs the exact number as a secret number - codebreaker wins the
57
+ game. If all attempts are spent - codebreaker loses.\n
58
+
59
+ \tCodebreaker 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.\n"
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ PATH = './db'
5
+ FILE_NAME = 'data.yml'
6
+ module Storage
7
+ class << self
8
+ def save(results)
9
+ FileUtils.mkdir_p(PATH) unless Dir.exist?(PATH)
10
+ data = load << results
11
+ File.open(File.join(Dir.pwd, PATH, FILE_NAME), 'a+') { |file| file.write(data.to_yaml) }
12
+ end
13
+
14
+ def load
15
+ YAML.safe_load(File.join(Dir.pwd, PATH, FILE_NAME))
16
+ []
17
+ end
18
+
19
+ def sort_player
20
+ load_database.sort_by { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ module Validation
5
+ def size_correct?(value)
6
+ value.length.between?(3, 20)
7
+ end
8
+
9
+ def class_valid?(value, klass)
10
+ value.is_a?(klass)
11
+ end
12
+
13
+ def guess_valid?(value)
14
+ /^[1-6]{4}$/.match?(value)
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker-Bulatkin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yevhenii Bulatkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-04 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: pry
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: 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.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.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: simplecov
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
+ description: Crack the code
126
+ email:
127
+ - yevhenii@rubygarage.org
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".-ruby-version"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".ruby-gemset"
137
+ - ".travis.yml"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - bin/codebreaker
145
+ - bin/setup
146
+ - codebreaker.gemspec
147
+ - lib/codebreaker.rb
148
+ - lib/codebreaker/console.rb
149
+ - lib/codebreaker/game.rb
150
+ - lib/codebreaker/player.rb
151
+ - lib/codebreaker/version.rb
152
+ - lib/config.rb
153
+ - lib/i18n_config.rb
154
+ - lib/locales/en.yml
155
+ - lib/modules/storage.rb
156
+ - lib/modules/validation.rb
157
+ homepage: https://github.com/Bulatkin/codebreaker
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubygems_version: 3.0.3
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Codebreaker game
180
+ test_files: []