codebreaker_dmitriev 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.fasterer.yml +22 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +84 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/autoload.rb +2 -0
- data/autoload_console.rb +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codebreaker.gemspec +42 -0
- data/config/i18n.rb +1 -0
- data/config/locales/en.yml +28 -0
- data/lib/codebreaker/game.rb +77 -0
- data/lib/codebreaker/validation.rb +19 -0
- data/lib/codebreaker/version.rb +3 -0
- data/lib/codebreaker.rb +2 -0
- data/lib/console_app/console.rb +121 -0
- data/lib/console_app/database.rb +39 -0
- data/lib/console_app/difficult.rb +33 -0
- data/lib/console_app/errors/errors.rb +7 -0
- data/lib/console_app/output.rb +98 -0
- data/main.rb +3 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b7953790376d20c4c0fdeea17496564434f344fa0f3db29ad7a875eadfb1bba1
|
4
|
+
data.tar.gz: 0dbcab1cde0637f14b8a6b452e97f6b5cbc1be34ee0232458c8fce1ae77bbede
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 39324fb095949394bf296666f1259df7beb9312aac005836ece4cad63cb115546b2a6b0ae3683aa82b6f45ad78d7aa948dae0870bdc8a0f143f9a2aa79f4e1c5
|
7
|
+
data.tar.gz: 2ab28acfefe83253fa745bcc20c9dc04601f1f00d3c9262630d49c9e040270fa598ff5fdbbf9ae6f6133ddf6c96fcbc6af2908b4bb3e2cc5913805c34145192a
|
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: false
|
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
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
Metrics/LineLength:
|
4
|
+
Max: 120
|
5
|
+
|
6
|
+
Style/Documentation:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/FrozenStringLiteralComment:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Metrics/BlockLength:
|
13
|
+
ExcludedMethods: ['describe']
|
14
|
+
|
15
|
+
Metrics/AbcSize:
|
16
|
+
Max: 30
|
17
|
+
|
18
|
+
Metrics/MethodLength:
|
19
|
+
Max: 15
|
20
|
+
|
21
|
+
RSpec/MessageSpies:
|
22
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.6.3
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 legendado70@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
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
codebreaker_dmitriev (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
byebug (11.0.1)
|
11
|
+
coderay (1.1.2)
|
12
|
+
colorize (0.8.1)
|
13
|
+
concurrent-ruby (1.1.5)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
docile (1.3.1)
|
16
|
+
fasterer (0.5.1)
|
17
|
+
colorize (~> 0.7)
|
18
|
+
ruby_parser (>= 3.13.0)
|
19
|
+
i18n (1.6.0)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
jaro_winkler (1.5.2)
|
22
|
+
json (2.2.0)
|
23
|
+
method_source (0.9.2)
|
24
|
+
parallel (1.17.0)
|
25
|
+
parser (2.6.3.0)
|
26
|
+
ast (~> 2.4.0)
|
27
|
+
pry (0.12.2)
|
28
|
+
coderay (~> 1.1.0)
|
29
|
+
method_source (~> 0.9.0)
|
30
|
+
pry-byebug (3.7.0)
|
31
|
+
byebug (~> 11.0)
|
32
|
+
pry (~> 0.10)
|
33
|
+
rainbow (3.0.0)
|
34
|
+
rake (10.5.0)
|
35
|
+
rspec (3.8.0)
|
36
|
+
rspec-core (~> 3.8.0)
|
37
|
+
rspec-expectations (~> 3.8.0)
|
38
|
+
rspec-mocks (~> 3.8.0)
|
39
|
+
rspec-core (3.8.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-expectations (3.8.3)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-mocks (3.8.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.8.0)
|
47
|
+
rspec-support (3.8.0)
|
48
|
+
rubocop (0.71.0)
|
49
|
+
jaro_winkler (~> 1.5.1)
|
50
|
+
parallel (~> 1.10)
|
51
|
+
parser (>= 2.6)
|
52
|
+
rainbow (>= 2.2.2, < 4.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
55
|
+
rubocop-rspec (1.33.0)
|
56
|
+
rubocop (>= 0.60.0)
|
57
|
+
ruby-progressbar (1.10.1)
|
58
|
+
ruby_parser (3.13.1)
|
59
|
+
sexp_processor (~> 4.9)
|
60
|
+
sexp_processor (4.12.1)
|
61
|
+
simplecov (0.16.1)
|
62
|
+
docile (~> 1.1)
|
63
|
+
json (>= 1.8, < 3)
|
64
|
+
simplecov-html (~> 0.10.0)
|
65
|
+
simplecov-html (0.10.2)
|
66
|
+
unicode-display_width (1.6.0)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
ruby
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
bundler (~> 2.0)
|
73
|
+
codebreaker_dmitriev!
|
74
|
+
fasterer (~> 0.5.1)
|
75
|
+
i18n (~> 1.6)
|
76
|
+
pry-byebug (~> 3.4)
|
77
|
+
rake (~> 10.0)
|
78
|
+
rspec (~> 3.0)
|
79
|
+
rubocop (~> 0.71.0)
|
80
|
+
rubocop-rspec (~> 1.33)
|
81
|
+
simplecov (~> 0.16.1)
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
2.0.1
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
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
|
+
## Code of Conduct
|
38
|
+
|
39
|
+
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
data/autoload.rb
ADDED
data/autoload_console.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'codebreaker'
|
2
|
+
require 'yaml'
|
3
|
+
require 'i18n'
|
4
|
+
require 'pry'
|
5
|
+
|
6
|
+
require_relative './config/i18n'
|
7
|
+
require_relative './lib/console_app/output'
|
8
|
+
require_relative './lib/console_app/database'
|
9
|
+
require_relative './lib/console_app/difficult'
|
10
|
+
require_relative './lib/console_app/console'
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'codebreaker'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/codebreaker.gemspec
ADDED
@@ -0,0 +1,42 @@
|
|
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_dmitriev'
|
7
|
+
spec.version = Codebreaker::VERSION
|
8
|
+
spec.authors = ['Alexey Dmitriev']
|
9
|
+
spec.email = ['legendado70@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Codebreaker'
|
12
|
+
spec.description = 'Game'
|
13
|
+
spec.homepage = 'https://github.com/legendado/codebreaker'
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
21
|
+
'public gem pushes.'
|
22
|
+
end
|
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-byebug', '~> 3.4'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
+
spec.add_development_dependency 'rubocop', '~> 0.71.0'
|
40
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.33'
|
41
|
+
spec.add_development_dependency 'simplecov', '~> 0.16.1'
|
42
|
+
end
|
data/config/i18n.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
I18n.load_path << Dir[File.expand_path('config/locales') + '/*.yml']
|
@@ -0,0 +1,28 @@
|
|
1
|
+
en:
|
2
|
+
greeting: "Welcome to codebreaker!"
|
3
|
+
goodbye: "Goodbye!"
|
4
|
+
rules: "You need to guess a number that the computer generated.\nYou have some attempts and some hints.\nGood luck :)"
|
5
|
+
menu: "Please, choose one:\n* start\n* rules\n* stats\n* exit"
|
6
|
+
difficult: "* Easy - 15 attempts, 2 hints\n* Medium - 10 attempts, 1 hint\n* Hell - 5 attempts, 1 hint"
|
7
|
+
set_name: "Choose name (min 3 characters):"
|
8
|
+
set_difficult: "Choose level:"
|
9
|
+
unknown_command: "Wrong command!"
|
10
|
+
name_error: "Name must be between 3 and 20 characters!"
|
11
|
+
difficult_error: "Wrong difficult!"
|
12
|
+
hint_error: "Sorry, but you don`t have a hints :("
|
13
|
+
win: "Congratz. You win!"
|
14
|
+
lose: "Nice try, but you lose :("
|
15
|
+
game_help: "Your number or command:"
|
16
|
+
game_commands: "\n* Puts your number\n* Hints\n* Exit"
|
17
|
+
game_hints: "You have %{hints} hints."
|
18
|
+
game_attempts: "%{attempts} attempts left."
|
19
|
+
start_again: "Do you wanna start a new game?(y/n):"
|
20
|
+
save_stats: "Do you wanna save your stats?(y/n):"
|
21
|
+
show_stats: "Rating: %{rating};
|
22
|
+
Name: %{name};
|
23
|
+
Difficult: %{difficult};
|
24
|
+
Attempts total: %{a};
|
25
|
+
Attempts used: %{a_used};
|
26
|
+
Hints total: %{h};
|
27
|
+
Hints used: %{h_used};"
|
28
|
+
stats_error: "Statistics is empty."
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Codebreaker
|
2
|
+
include Validation
|
3
|
+
|
4
|
+
class Game
|
5
|
+
PLUS = '+'.freeze
|
6
|
+
MINUS = '-'.freeze
|
7
|
+
|
8
|
+
attr_reader :name, :difficult, :secret, :attempts, :hints, :hints_used, :attempts_used
|
9
|
+
|
10
|
+
def initialize(name, difficult)
|
11
|
+
@name = name
|
12
|
+
@difficult = difficult
|
13
|
+
@attempts = get_attempts(difficult)
|
14
|
+
@hints = get_hints(difficult)
|
15
|
+
@attempts_used = 0
|
16
|
+
@hints_used = 0
|
17
|
+
|
18
|
+
generate_secret
|
19
|
+
@secret_chars = @secret.chars.shuffle
|
20
|
+
end
|
21
|
+
|
22
|
+
def decrement_attempts
|
23
|
+
return false if @attempts.zero?
|
24
|
+
|
25
|
+
@attempts_used += 1
|
26
|
+
@attempts -= 1
|
27
|
+
end
|
28
|
+
|
29
|
+
def use_hint
|
30
|
+
return false if @hints.zero?
|
31
|
+
|
32
|
+
@hints_used += 1
|
33
|
+
@hints -= 1
|
34
|
+
@secret_chars.pop
|
35
|
+
end
|
36
|
+
|
37
|
+
def win?(matches)
|
38
|
+
matches == '++++'
|
39
|
+
end
|
40
|
+
|
41
|
+
def check_number(guess, secret_code = @secret)
|
42
|
+
secret = secret_code.chars
|
43
|
+
input = guess.chars
|
44
|
+
|
45
|
+
grouped = secret.zip(input).group_by { |values| values.first == values.last }
|
46
|
+
grouped.default = []
|
47
|
+
|
48
|
+
plus = grouped[true].size
|
49
|
+
return PLUS * plus if plus == 4
|
50
|
+
|
51
|
+
secret, input = grouped[false].transpose
|
52
|
+
minus = (secret & input).inject(0) { |count, number| count + [secret.count(number), input.count(number)].min }
|
53
|
+
PLUS * plus << MINUS * minus
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def generate_secret
|
59
|
+
@secret = (1..4).map { rand(1..6) }.join
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_attempts(difficult)
|
63
|
+
case difficult
|
64
|
+
when 'easy' then 15
|
65
|
+
when 'medium' then 10
|
66
|
+
when 'hell' then 5
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_hints(difficult)
|
71
|
+
case difficult
|
72
|
+
when 'easy' then 2
|
73
|
+
else 1
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Validation
|
2
|
+
DIFFICULTS = {
|
3
|
+
easy: 'easy',
|
4
|
+
medium: 'medium',
|
5
|
+
hell: 'hell'
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
def name_valid?(name)
|
9
|
+
name.length.between? 3, 20
|
10
|
+
end
|
11
|
+
|
12
|
+
def guess_valid?(guess)
|
13
|
+
/^[1-6]{4}$/.match? guess
|
14
|
+
end
|
15
|
+
|
16
|
+
def difficult_valid?(difficult)
|
17
|
+
DIFFICULTS.values.include? difficult.downcase
|
18
|
+
end
|
19
|
+
end
|
data/lib/codebreaker.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
class Console
|
2
|
+
include Codebreaker
|
3
|
+
include Output
|
4
|
+
include Database
|
5
|
+
|
6
|
+
COMMANDS = {
|
7
|
+
start: 'start',
|
8
|
+
rules: 'rules',
|
9
|
+
stats: 'stats',
|
10
|
+
exit: 'exit',
|
11
|
+
hints: 'hints'
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
YES = 'y'.freeze
|
15
|
+
|
16
|
+
def run
|
17
|
+
Messages.show_greeting
|
18
|
+
|
19
|
+
loop do
|
20
|
+
print '~> '
|
21
|
+
case gets.chomp.downcase
|
22
|
+
when COMMANDS[:start] then return register
|
23
|
+
when COMMANDS[:rules] then Messages.show_rules
|
24
|
+
when COMMANDS[:stats] then Statistics.show
|
25
|
+
when COMMANDS[:exit] then close
|
26
|
+
else Messages.unknown_command
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def to_h(game)
|
34
|
+
{
|
35
|
+
name: game.name,
|
36
|
+
difficult: @difficult,
|
37
|
+
attempts: game.attempts,
|
38
|
+
attempts_used: game.attempts_used,
|
39
|
+
hints: game.hints,
|
40
|
+
hints_used: game.hints_used
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def register
|
45
|
+
name = set_name
|
46
|
+
@difficult = set_difficult
|
47
|
+
game = Game.new(name, @difficult.level)
|
48
|
+
start_game(game)
|
49
|
+
end
|
50
|
+
|
51
|
+
def start_game(game)
|
52
|
+
Messages.game_commands
|
53
|
+
|
54
|
+
loop do
|
55
|
+
break Messages.lose if game.attempts.zero?
|
56
|
+
|
57
|
+
Messages.show_game_info game
|
58
|
+
guess = user_input
|
59
|
+
|
60
|
+
next Messages.get_hint(game) if guess == COMMANDS[:hints]
|
61
|
+
next Messages.unknown_command unless guess_valid? guess
|
62
|
+
|
63
|
+
result = get_result(game, guess)
|
64
|
+
break win_game(game) if result
|
65
|
+
end
|
66
|
+
|
67
|
+
start_new_game
|
68
|
+
end
|
69
|
+
|
70
|
+
def start_new_game
|
71
|
+
Messages.start_again
|
72
|
+
start = gets.chomp.downcase
|
73
|
+
return run if start == YES
|
74
|
+
|
75
|
+
close
|
76
|
+
end
|
77
|
+
|
78
|
+
def win_game(game)
|
79
|
+
Messages.win
|
80
|
+
Messages.save_stats
|
81
|
+
save = gets.chomp.downcase
|
82
|
+
Statistics.save(to_h(game)) if save == YES
|
83
|
+
end
|
84
|
+
|
85
|
+
def get_result(game, guess)
|
86
|
+
result = game.check_number(guess)
|
87
|
+
puts "[#{result}]"
|
88
|
+
game.decrement_attempts
|
89
|
+
game.win? result
|
90
|
+
end
|
91
|
+
|
92
|
+
def set_name
|
93
|
+
Messages.show_name
|
94
|
+
loop do
|
95
|
+
name = user_input
|
96
|
+
return name if name_valid? name
|
97
|
+
|
98
|
+
Messages.name_error
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def set_difficult
|
103
|
+
Messages.show_difficult
|
104
|
+
loop do
|
105
|
+
difficult = user_input
|
106
|
+
return Difficult.new(difficult) if difficult_valid? difficult
|
107
|
+
|
108
|
+
Messages.difficult_error
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def user_input
|
113
|
+
input = gets.chomp.downcase
|
114
|
+
input == COMMANDS[:exit] ? close : input
|
115
|
+
end
|
116
|
+
|
117
|
+
def close
|
118
|
+
Messages.show_bye
|
119
|
+
exit
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Database
|
2
|
+
class Statistics
|
3
|
+
DB_FILE = 'seed.yaml'.freeze
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def save(game_result)
|
7
|
+
@stats = open
|
8
|
+
@stats << game_result
|
9
|
+
File.write(DB_FILE, @stats.to_yaml)
|
10
|
+
end
|
11
|
+
|
12
|
+
def open
|
13
|
+
File.exist?(DB_FILE) ? load : []
|
14
|
+
end
|
15
|
+
|
16
|
+
def load
|
17
|
+
Psych.safe_load(
|
18
|
+
File.read(DB_FILE),
|
19
|
+
[Symbol, Difficult],
|
20
|
+
[],
|
21
|
+
true
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def sort(stats)
|
26
|
+
stats
|
27
|
+
.sort_by { |stat| [stat[:difficult].to_i, stat[:attempts_used], stat[:hints_used]] }
|
28
|
+
.each.with_index { |stat, index| stat[:rating] = index + 1 }
|
29
|
+
end
|
30
|
+
|
31
|
+
def show
|
32
|
+
@stats = open
|
33
|
+
return Output::Messages.stats_error if @stats.empty?
|
34
|
+
|
35
|
+
sort(@stats).each { |stat| Output::Messages.show_stats(stat) }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative './errors/errors'
|
2
|
+
|
3
|
+
class Difficult
|
4
|
+
include Errors
|
5
|
+
|
6
|
+
DIFFICULTS = {
|
7
|
+
easy: 'easy',
|
8
|
+
medium: 'medium',
|
9
|
+
hell: 'hell'
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
attr_reader :level
|
13
|
+
|
14
|
+
def initialize(level)
|
15
|
+
raise WrongDifficultError unless valid? level
|
16
|
+
|
17
|
+
@level = level.downcase
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_i
|
21
|
+
case @level.downcase
|
22
|
+
when DIFFICULTS[:hell] then 1
|
23
|
+
when DIFFICULTS[:medium] then 2
|
24
|
+
when DIFFICULTS[:easy] then 3
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def valid?(level)
|
31
|
+
level.is_a?(String) && DIFFICULTS.values.include?(level.downcase)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module Output
|
2
|
+
class Messages
|
3
|
+
class << self
|
4
|
+
ARROW = '~> '.freeze
|
5
|
+
|
6
|
+
def clear
|
7
|
+
system('clear')
|
8
|
+
end
|
9
|
+
|
10
|
+
def stats_error
|
11
|
+
puts I18n.t :stats_error
|
12
|
+
end
|
13
|
+
|
14
|
+
def show_stats(stat)
|
15
|
+
puts I18n.t(:show_stats, rating: stat[:rating], name: stat[:name], difficult: stat[:difficult].level,
|
16
|
+
a: (stat[:attempts] + stat[:attempts_used]), a_used: stat[:attempts_used],
|
17
|
+
h: (stat[:hints] + stat[:hints_used]), h_used: stat[:hints_used])
|
18
|
+
end
|
19
|
+
|
20
|
+
def show_greeting
|
21
|
+
clear
|
22
|
+
puts I18n.t :greeting
|
23
|
+
puts I18n.t :menu
|
24
|
+
end
|
25
|
+
|
26
|
+
def show_rules
|
27
|
+
puts I18n.t :rules
|
28
|
+
end
|
29
|
+
|
30
|
+
def show_bye
|
31
|
+
puts I18n.t :goodbye
|
32
|
+
end
|
33
|
+
|
34
|
+
def show_game_info(game)
|
35
|
+
puts '==================='
|
36
|
+
puts I18n.t(:game_hints, hints: game.hints)
|
37
|
+
puts I18n.t(:game_attempts, attempts: game.attempts)
|
38
|
+
puts I18n.t :game_help
|
39
|
+
print ARROW
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_hint(game)
|
43
|
+
return Messages.hint_error if game.hints.zero?
|
44
|
+
|
45
|
+
puts "[#{game.use_hint}] - one of the number"
|
46
|
+
end
|
47
|
+
|
48
|
+
def show_difficult
|
49
|
+
puts I18n.t :set_difficult
|
50
|
+
puts I18n.t :difficult
|
51
|
+
print ARROW
|
52
|
+
end
|
53
|
+
|
54
|
+
def show_name
|
55
|
+
puts I18n.t :set_name
|
56
|
+
print ARROW
|
57
|
+
end
|
58
|
+
|
59
|
+
def game_commands
|
60
|
+
puts I18n.t :game_commands
|
61
|
+
end
|
62
|
+
|
63
|
+
def start_again
|
64
|
+
puts I18n.t :start_again
|
65
|
+
print ARROW
|
66
|
+
end
|
67
|
+
|
68
|
+
def win
|
69
|
+
puts I18n.t :win
|
70
|
+
end
|
71
|
+
|
72
|
+
def save_stats
|
73
|
+
puts I18n.t :save_stats
|
74
|
+
print ARROW
|
75
|
+
end
|
76
|
+
|
77
|
+
def name_error
|
78
|
+
puts I18n.t :name_error
|
79
|
+
end
|
80
|
+
|
81
|
+
def lose
|
82
|
+
puts I18n.t :lose
|
83
|
+
end
|
84
|
+
|
85
|
+
def difficult_error
|
86
|
+
puts I18n.t :difficult_error
|
87
|
+
end
|
88
|
+
|
89
|
+
def hint_error
|
90
|
+
puts I18n.t :hint_error
|
91
|
+
end
|
92
|
+
|
93
|
+
def unknown_command
|
94
|
+
puts I18n.t :unknown_command
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/main.rb
ADDED
metadata
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker_dmitriev
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Dmitriev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-18 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-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
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.71.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.71.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: '1.33'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.33'
|
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.16.1
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.16.1
|
139
|
+
description: Game
|
140
|
+
email:
|
141
|
+
- legendado70@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".fasterer.yml"
|
147
|
+
- ".gitignore"
|
148
|
+
- ".rspec"
|
149
|
+
- ".rubocop.yml"
|
150
|
+
- ".ruby-version"
|
151
|
+
- ".travis.yml"
|
152
|
+
- CODE_OF_CONDUCT.md
|
153
|
+
- Gemfile
|
154
|
+
- Gemfile.lock
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- autoload.rb
|
158
|
+
- autoload_console.rb
|
159
|
+
- bin/console
|
160
|
+
- bin/setup
|
161
|
+
- codebreaker.gemspec
|
162
|
+
- config/i18n.rb
|
163
|
+
- config/locales/en.yml
|
164
|
+
- lib/codebreaker.rb
|
165
|
+
- lib/codebreaker/game.rb
|
166
|
+
- lib/codebreaker/validation.rb
|
167
|
+
- lib/codebreaker/version.rb
|
168
|
+
- lib/console_app/console.rb
|
169
|
+
- lib/console_app/database.rb
|
170
|
+
- lib/console_app/difficult.rb
|
171
|
+
- lib/console_app/errors/errors.rb
|
172
|
+
- lib/console_app/output.rb
|
173
|
+
- main.rb
|
174
|
+
homepage: https://github.com/legendado/codebreaker
|
175
|
+
licenses: []
|
176
|
+
metadata:
|
177
|
+
allowed_push_host: https://rubygems.org
|
178
|
+
post_install_message:
|
179
|
+
rdoc_options: []
|
180
|
+
require_paths:
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - ">="
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
requirements: []
|
193
|
+
rubygems_version: 3.0.3
|
194
|
+
signing_key:
|
195
|
+
specification_version: 4
|
196
|
+
summary: Codebreaker
|
197
|
+
test_files: []
|