codebreakergem 0.1.12 → 0.1.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +14 -13
- data/.fasterer.yml +19 -19
- data/.gitignore +11 -11
- data/.rspec +3 -3
- data/.rubocop.yml +13 -8
- data/.ruby-version +1 -1
- data/.travis.yml +7 -7
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +12 -11
- data/Gemfile.lock +75 -70
- data/LICENSE.txt +21 -21
- data/README.md +43 -43
- data/Rakefile +8 -8
- data/bin/console +15 -15
- data/bin/setup +8 -8
- data/codebreakergem.gemspec +39 -43
- data/lib/classes/difficulties/difficulty.rb +12 -12
- data/lib/classes/difficulties/easy.rb +12 -12
- data/lib/classes/difficulties/hell.rb +12 -12
- data/lib/classes/difficulties/medium.rb +12 -12
- data/lib/classes/file_worker.rb +29 -25
- data/lib/classes/game.rb +103 -116
- data/lib/classes/information_presenter.rb +32 -0
- data/lib/classes/user.rb +17 -17
- data/lib/codebreakergem.rb +19 -19
- data/lib/codebreakergem/version.rb +5 -5
- data/lib/config.rb +5 -5
- data/lib/locales/en.yml +53 -50
- data/lib/modules/check_module.rb +11 -11
- metadata +5 -5
- data/lib/classes/statistic.rb +0 -17
- data/lib/locales/ru.yml +0 -2
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2019 Sasha
|
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.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Sasha
|
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
CHANGED
@@ -1,43 +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/codebreakergem`. 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 'codebreakergem'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install codebreakergem
|
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]/codebreakergem. 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]/codebreakergem/blob/master/CODE_OF_CONDUCT.md).
|
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/codebreakergem`. 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 'codebreakergem'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install codebreakergem
|
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]/codebreakergem. 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]/codebreakergem/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,8 +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
|
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
|
data/bin/console
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'codebreakergem'
|
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__)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'codebreakergem'
|
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
CHANGED
@@ -1,8 +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
|
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
|
data/codebreakergem.gemspec
CHANGED
@@ -1,43 +1,39 @@
|
|
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 'codebreakergem/version'
|
6
|
-
|
7
|
-
Gem::Specification.new do |spec|
|
8
|
-
spec.name = 'codebreakergem'
|
9
|
-
spec.version = Codebreakergem::VERSION
|
10
|
-
spec.authors = ['Sasha']
|
11
|
-
spec.email = ['sasha']
|
12
|
-
|
13
|
-
spec.summary = 'Codebreaker gem'
|
14
|
-
spec.description = 'A logic game where you need to guess number'
|
15
|
-
spec.homepage = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
|
16
|
-
spec.license = 'MIT'
|
17
|
-
|
18
|
-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
-
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
spec.
|
34
|
-
|
35
|
-
|
36
|
-
spec.
|
37
|
-
spec.
|
38
|
-
spec.
|
39
|
-
|
40
|
-
spec.add_development_dependency 'bundler', '~> 1.17'
|
41
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
42
|
-
spec.add_development_dependency 'rspec', '~> 3.0'
|
43
|
-
end
|
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 'codebreakergem/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'codebreakergem'
|
9
|
+
spec.version = Codebreakergem::VERSION
|
10
|
+
spec.authors = ['Sasha']
|
11
|
+
spec.email = ['sasha']
|
12
|
+
|
13
|
+
spec.summary = 'Codebreaker gem'
|
14
|
+
spec.description = 'A logic game where you need to guess number'
|
15
|
+
spec.homepage = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
|
21
|
+
|
22
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
23
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
24
|
+
spec.metadata['source_code_uri'] = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
|
25
|
+
spec.metadata['changelog_uri'] = 'https://github.com/SashaZhuravskiy/CodebreakerGemRepo/'
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = 'exe'
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ['lib']
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
39
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class Difficulty
|
5
|
-
attr_reader :title, :order
|
6
|
-
attr_accessor :attempts, :hints
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
raise NotImplementedError
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Difficulty
|
5
|
+
attr_reader :title, :order
|
6
|
+
attr_accessor :attempts, :hints
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class Easy < Difficulty
|
5
|
-
def initialize
|
6
|
-
@title = 'Easy'
|
7
|
-
@attempts = 15
|
8
|
-
@hints = 2
|
9
|
-
@order = 3
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Easy < Difficulty
|
5
|
+
def initialize
|
6
|
+
@title = 'Easy'
|
7
|
+
@attempts = 15
|
8
|
+
@hints = 2
|
9
|
+
@order = 3
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class Hell < Difficulty
|
5
|
-
def initialize
|
6
|
-
@title = 'Hell'
|
7
|
-
@attempts = 5
|
8
|
-
@hints = 1
|
9
|
-
@order = 1
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Hell < Difficulty
|
5
|
+
def initialize
|
6
|
+
@title = 'Hell'
|
7
|
+
@attempts = 5
|
8
|
+
@hints = 1
|
9
|
+
@order = 1
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class Medium < Difficulty
|
5
|
-
def initialize
|
6
|
-
@title = 'Medium'
|
7
|
-
@attempts = 10
|
8
|
-
@hints = 1
|
9
|
-
@order = 2
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Medium < Difficulty
|
5
|
+
def initialize
|
6
|
+
@title = 'Medium'
|
7
|
+
@attempts = 10
|
8
|
+
@hints = 1
|
9
|
+
@order = 2
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/classes/file_worker.rb
CHANGED
@@ -1,25 +1,29 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class FileWorker
|
5
|
-
PERMITTED_CLASSES = [
|
6
|
-
|
7
|
-
class << self
|
8
|
-
def read_from_file(filename)
|
9
|
-
return
|
10
|
-
|
11
|
-
YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_to_file(filename, data)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class FileWorker
|
5
|
+
PERMITTED_CLASSES = [Symbol].freeze
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def read_from_file(filename)
|
9
|
+
return unless File.file?(Game::FILE)
|
10
|
+
|
11
|
+
YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_to_file(filename, data)
|
15
|
+
if File.file?(filename)
|
16
|
+
data_in_file = YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
|
17
|
+
data_in_file.is_a?(Array) ? data_in_file << data : data_in_file = [data_in_file, data]
|
18
|
+
write_to_file(filename, data_in_file)
|
19
|
+
else
|
20
|
+
write_to_file(filename, data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def write_to_file(filename, data)
|
25
|
+
File.open(filename, 'w') { |file| file.write(data.to_yaml) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/classes/game.rb
CHANGED
@@ -1,116 +1,103 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Codebreakergem
|
4
|
-
class Game
|
5
|
-
include Checker
|
6
|
-
|
7
|
-
DIGIT_NUMBER = 4
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
difficulty.hints
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
difficulty.attempts
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
end
|
105
|
-
end
|
106
|
-
result
|
107
|
-
end
|
108
|
-
|
109
|
-
def make_guess(guess)
|
110
|
-
result = ''
|
111
|
-
code_copy = secret_code.dup
|
112
|
-
result += find_place(guess, code_copy)
|
113
|
-
result + find_presence(guess, code_copy)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreakergem
|
4
|
+
class Game
|
5
|
+
include Checker
|
6
|
+
|
7
|
+
DIGIT_NUMBER = 4
|
8
|
+
CORRECT_PLACE_SYMBOL = '+'
|
9
|
+
INCORRECT_PLACE_SYMBOL = '-'
|
10
|
+
FILE = File.expand_path(__dir__ + '/../data/data.yml')
|
11
|
+
|
12
|
+
attr_reader :secret_code, :user
|
13
|
+
attr_accessor :difficulty, :statistics
|
14
|
+
|
15
|
+
def initialize(user, difficulty_title)
|
16
|
+
@user = user
|
17
|
+
@difficulty = difficulty_factory(difficulty_title)
|
18
|
+
@secret_code = generate_code
|
19
|
+
end
|
20
|
+
|
21
|
+
def show_hint
|
22
|
+
return I18n.t(:no_hints) unless difficulty.hints.positive?
|
23
|
+
|
24
|
+
difficulty.hints -= 1
|
25
|
+
secret_code[difficulty.hints]
|
26
|
+
end
|
27
|
+
|
28
|
+
def try_guess(guess)
|
29
|
+
if guess == secret_code
|
30
|
+
save
|
31
|
+
difficulty.attempts = 0
|
32
|
+
return I18n.t(:victory_message)
|
33
|
+
end
|
34
|
+
|
35
|
+
difficulty.attempts -= 1
|
36
|
+
return I18n.t(:out_of_attempts) unless difficulty.attempts.positive?
|
37
|
+
|
38
|
+
make_guess(guess)
|
39
|
+
end
|
40
|
+
|
41
|
+
def save
|
42
|
+
source = difficulty_factory(difficulty.title)
|
43
|
+
FileWorker.add_to_file(FILE, to_h(source, source.attempts - difficulty.attempts, source.hints - difficulty.hints))
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_h(source_difficulty, attempts_used, hints_used)
|
47
|
+
{ name: user.name,
|
48
|
+
difficulty: difficulty.title,
|
49
|
+
order: difficulty.order,
|
50
|
+
attempts_total: source_difficulty.attempts,
|
51
|
+
attempts_used: attempts_used,
|
52
|
+
hints_total: source_difficulty.hints,
|
53
|
+
hints_used: hints_used }
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def used_calculation(initial, actual)
|
59
|
+
initial - actual
|
60
|
+
end
|
61
|
+
|
62
|
+
def generate_code
|
63
|
+
DIGIT_NUMBER.times.map { rand(1..6) }.join
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_place(guess, code)
|
67
|
+
result = ''
|
68
|
+
(0...guess.length).each do |index|
|
69
|
+
next unless guess[index] == code[index]
|
70
|
+
|
71
|
+
result += CORRECT_PLACE_SYMBOL
|
72
|
+
code[index] = ' '
|
73
|
+
guess[index] = '_'
|
74
|
+
end
|
75
|
+
result
|
76
|
+
end
|
77
|
+
|
78
|
+
def find_presence(guess, code)
|
79
|
+
result = ''
|
80
|
+
(0...guess.length).each do |index|
|
81
|
+
position = code.index(guess[index])
|
82
|
+
if position
|
83
|
+
result += INCORRECT_PLACE_SYMBOL
|
84
|
+
code[position] = ' '
|
85
|
+
end
|
86
|
+
end
|
87
|
+
result
|
88
|
+
end
|
89
|
+
|
90
|
+
def make_guess(guess)
|
91
|
+
code_copy = secret_code.dup
|
92
|
+
find_place(guess, code_copy) + find_presence(guess, code_copy)
|
93
|
+
end
|
94
|
+
|
95
|
+
def difficulty_factory(difficulty_title)
|
96
|
+
case difficulty_title
|
97
|
+
when 'Easy' then Easy.new
|
98
|
+
when 'Medium' then Medium.new
|
99
|
+
when 'Hell' then Hell.new
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|