codebreaker-d3n 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 +19 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/codebreaker.gemspec +36 -0
- data/config/locales/en.yml +31 -0
- data/lib/autoload.rb +13 -0
- data/lib/codebreaker/console.rb +121 -0
- data/lib/codebreaker/database.rb +25 -0
- data/lib/codebreaker/game.rb +121 -0
- data/lib/codebreaker/user.rb +25 -0
- data/lib/codebreaker/validations.rb +13 -0
- data/lib/codebreaker/version.rb +5 -0
- data/lib/codebreaker.rb +6 -0
- metadata +205 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 520e12c5185312f3b8a9ede23726c7633227f7c5d99122d89ed79bbb4e644f5f
|
4
|
+
data.tar.gz: 4952453b93bbbb4594443242ef445d7d90bbfbe3a692ab8fd08f82ebae702283
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d167c78f1be950dfb54314528042af4967f32cd17038080f7884aa24831ff8bf91b633a659ed43d13f6e705a158f38eccf86923d94e90f8e0ee92a94f809747
|
7
|
+
data.tar.gz: 2dbb8980183107746a75ad1ec66ffcf1484487b30234ae23bf5f01f0e12f216433db4d35fa60d4dc9a5af2cae8ed31f51b10a42027d806f4e15d43fe64a0309d
|
data/.fasterer.yml
ADDED
@@ -0,0 +1,19 @@
|
|
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
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
Metrics/LineLength:
|
4
|
+
Max: 120
|
5
|
+
|
6
|
+
Metrics/BlockLength:
|
7
|
+
ExcludedMethods: ['describe']
|
8
|
+
|
9
|
+
Style/Documentation:
|
10
|
+
Description: 'Document classes and non-namespace modules.'
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
RSpec/MessageSpies:
|
14
|
+
EnforcedStyle: receive
|
15
|
+
|
16
|
+
RSpec/MessageChain:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
AllCops:
|
20
|
+
Exclude:
|
21
|
+
- 'bin/console'
|
22
|
+
- 'Rakefile'
|
23
|
+
- 'Gemfile'
|
24
|
+
- 'codebreaker.gemspec'
|
25
|
+
TargetRubyVersion: 2.5.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
codebreaker (0.1.0)
|
5
|
+
i18n
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.0)
|
11
|
+
coderay (1.1.2)
|
12
|
+
colorize (0.8.1)
|
13
|
+
concurrent-ruby (1.1.4)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
docile (1.3.1)
|
16
|
+
fasterer (0.4.1)
|
17
|
+
colorize (~> 0.7)
|
18
|
+
ruby_parser (~> 3.11.0)
|
19
|
+
i18n (1.2.0)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
jaro_winkler (1.5.1)
|
22
|
+
json (2.1.0)
|
23
|
+
method_source (0.9.2)
|
24
|
+
parallel (1.12.1)
|
25
|
+
parser (2.5.3.0)
|
26
|
+
ast (~> 2.4.0)
|
27
|
+
powerpack (0.1.2)
|
28
|
+
pry (0.12.2)
|
29
|
+
coderay (~> 1.1.0)
|
30
|
+
method_source (~> 0.9.0)
|
31
|
+
rainbow (3.0.0)
|
32
|
+
rake (10.5.0)
|
33
|
+
rspec (3.8.0)
|
34
|
+
rspec-core (~> 3.8.0)
|
35
|
+
rspec-expectations (~> 3.8.0)
|
36
|
+
rspec-mocks (~> 3.8.0)
|
37
|
+
rspec-core (3.8.0)
|
38
|
+
rspec-support (~> 3.8.0)
|
39
|
+
rspec-expectations (3.8.2)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.8.0)
|
42
|
+
rspec-mocks (3.8.0)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.8.0)
|
45
|
+
rspec-support (3.8.0)
|
46
|
+
rubocop (0.60.0)
|
47
|
+
jaro_winkler (~> 1.5.1)
|
48
|
+
parallel (~> 1.10)
|
49
|
+
parser (>= 2.5, != 2.5.1.1)
|
50
|
+
powerpack (~> 0.1)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (~> 1.4.0)
|
54
|
+
rubocop-rspec (1.30.1)
|
55
|
+
rubocop (>= 0.60.0)
|
56
|
+
ruby-progressbar (1.10.0)
|
57
|
+
ruby_parser (3.11.0)
|
58
|
+
sexp_processor (~> 4.9)
|
59
|
+
sexp_processor (4.11.0)
|
60
|
+
simplecov (0.16.1)
|
61
|
+
docile (~> 1.1)
|
62
|
+
json (>= 1.8, < 3)
|
63
|
+
simplecov-html (~> 0.10.0)
|
64
|
+
simplecov-html (0.10.2)
|
65
|
+
table_print (1.5.6)
|
66
|
+
unicode-display_width (1.4.0)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
ruby
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
bundler (~> 1.17)
|
73
|
+
codebreaker!
|
74
|
+
fasterer
|
75
|
+
pry
|
76
|
+
rake (~> 10.0)
|
77
|
+
rspec (~> 3.0)
|
78
|
+
rubocop
|
79
|
+
rubocop-rspec
|
80
|
+
simplecov
|
81
|
+
table_print
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 D3N
|
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,37 @@
|
|
1
|
+
# Codebreaker
|
2
|
+
|
3
|
+
Start game with codebreaker.rb
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'codebreaker'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install codebreaker
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Install, play, enjoy
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
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.
|
28
|
+
|
29
|
+
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).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreaker.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
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,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "codebreaker/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "codebreaker-d3n"
|
8
|
+
spec.version = Codebreaker::VERSION
|
9
|
+
spec.authors = "Denny_Dee"
|
10
|
+
spec.email = "denys.khomiakov@gmail.com"
|
11
|
+
|
12
|
+
spec.summary = "super summary"
|
13
|
+
spec.description = "super game"
|
14
|
+
spec.homepage = "http://supergame.com.ua"
|
15
|
+
spec.license = "AFL-1.1"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
27
|
+
spec.add_development_dependency "fasterer", '~> 0'
|
28
|
+
spec.add_development_dependency "pry", '~> 0'
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
spec.add_development_dependency "rubocop", '~> 0'
|
32
|
+
spec.add_development_dependency "rubocop-rspec", '~> 0'
|
33
|
+
spec.add_development_dependency "simplecov", '~> 0'
|
34
|
+
spec.add_development_dependency "table_print", '~> 0'
|
35
|
+
spec.add_runtime_dependency "i18n", '~> 0'
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
en:
|
2
|
+
INTRO: "\nWELCOME TO CODEBREAKER!\n"
|
3
|
+
GOODBYE: "\n\nGOOD BYE!!!\n\n"
|
4
|
+
HINT_PROPOSAL: "Press [%{hint}] to use hint"
|
5
|
+
NO_HINTS_MESSAGE: "No hints left"
|
6
|
+
WIN_MESSAGE: "\n\nYOU WIN!!!"
|
7
|
+
GOODBYE: "\n\nGOOD BYE!!!\n\n"
|
8
|
+
MAIN_MENU: "\n\nPress [%{quit}] to quit game.\nPress one of the following, please:\n\n[%{start}] -> START\n[%{rules}] -> RULES\n[%{stats}] -> STATS\n"
|
9
|
+
NAME_PROPOSAL: "\n\nPress [%{quit}] to quit game.\nEnter your NAME, please:"
|
10
|
+
ENTER_GUESS: "\nEnter your GUESS, please:\nPress [%{quit}] to quit game."
|
11
|
+
CHOOSE_DIFFICULTY: "\nPress [%{quit}] to quit game.\nChoose your game skill, please:\n-- [%{low}]\n-- [%{middle}]\n-- [%{high}]\n"
|
12
|
+
SAVE_MESSAGE: "\n\nPress [%{quit}] to quit game.\nDo you want to save results [y/n]?"
|
13
|
+
LOSE_MESSAGE: "\n\nYOU LOSE!!!"
|
14
|
+
NAME_ERROR: "Name should be within range %{min} to %{max} symbols"
|
15
|
+
GUESS_ERROR: "Guess should be %{quan}-digit only, each one within range %{min} to %{max} symbols"
|
16
|
+
MENU_ERROR: "Unacceptable command"
|
17
|
+
CORRECT_CODE: "Secret number was - %{code}"
|
18
|
+
|
19
|
+
RULES: "1. Codebreaker is a logic game in which a code-breaker tries to break a secret code 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.
|
20
|
+
2. 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. The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
|
21
|
+
3. 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:
|
22
|
+
Secret number - 1234
|
23
|
+
Input number - 6264
|
24
|
+
Number of pluses - 2 (second and fourth position)
|
25
|
+
4. 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:
|
26
|
+
Secret number - 1234
|
27
|
+
Input number - 6462
|
28
|
+
Number of minuses - 2 (second and fourth position)
|
29
|
+
5. An empty space indicates that there is not a current digit in a secret number.
|
30
|
+
6. If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
|
31
|
+
7. 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/lib/autoload.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pry'
|
4
|
+
require 'table_print'
|
5
|
+
require 'I18n'
|
6
|
+
require 'yaml'
|
7
|
+
require_relative 'codebreaker/version'
|
8
|
+
require_relative 'codebreaker/validations'
|
9
|
+
require_relative 'codebreaker/database'
|
10
|
+
require_relative 'codebreaker/game'
|
11
|
+
require_relative 'codebreaker/user'
|
12
|
+
require_relative 'codebreaker/console'
|
13
|
+
I18n.load_path << Dir[File.expand_path('config/locales') + '/*.yml']
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class Console
|
5
|
+
COMMAND = { start: '1', rules: '2', stats: '3', quit: 'q', use_tip: 'h', yes: 'y', no: 'n' }.freeze
|
6
|
+
attr_reader :game, :user
|
7
|
+
|
8
|
+
def menu
|
9
|
+
show(:INTRO)
|
10
|
+
loop do
|
11
|
+
show(:MAIN_MENU, start: COMMAND[:start], rules: COMMAND[:rules], stats: COMMAND[:stats], quit: COMMAND[:quit])
|
12
|
+
case input
|
13
|
+
when COMMAND[:start] then pre_game_process
|
14
|
+
when COMMAND[:rules] then show(:RULES)
|
15
|
+
when COMMAND[:stats] then show_stats
|
16
|
+
else show(:MENU_ERROR)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def pre_game_process
|
24
|
+
registration
|
25
|
+
@game = Game.new
|
26
|
+
game_level_definer
|
27
|
+
play_game
|
28
|
+
end
|
29
|
+
|
30
|
+
def registration
|
31
|
+
loop do
|
32
|
+
show(:NAME_PROPOSAL, quit: COMMAND[:quit])
|
33
|
+
@user = User.new
|
34
|
+
@user.name = input
|
35
|
+
return if @user.errors.empty?
|
36
|
+
|
37
|
+
puts @user.errors.first
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# rubocop:disable Metrics/LineLength
|
42
|
+
|
43
|
+
def game_level_definer
|
44
|
+
loop do
|
45
|
+
show(:CHOOSE_DIFFICULTY, low: Game::LEVELS.keys[0].to_s, middle: Game::LEVELS.keys[1].to_s, high: Game::LEVELS.keys[2].to_s, quit: COMMAND[:quit])
|
46
|
+
return if @game.set(input)
|
47
|
+
|
48
|
+
show(:MENU_ERROR)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# rubocop:enable Metrics/LineLength
|
53
|
+
|
54
|
+
def play_game
|
55
|
+
loop do
|
56
|
+
before_round
|
57
|
+
in_round_input = input
|
58
|
+
next provide_hint if in_round_input == COMMAND[:use_tip]
|
59
|
+
|
60
|
+
round(in_round_input)
|
61
|
+
next if @game.errors.any?
|
62
|
+
|
63
|
+
puts @game.start
|
64
|
+
return the_end if game_over?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def provide_hint
|
69
|
+
@game.hints_left? ? (puts @game.hint) : show(:NO_HINTS_MESSAGE)
|
70
|
+
end
|
71
|
+
|
72
|
+
def before_round
|
73
|
+
show(:ENTER_GUESS, quit: COMMAND[:quit])
|
74
|
+
show(:HINT_PROPOSAL, hint: COMMAND[:use_tip]) if @game.hints_left?
|
75
|
+
end
|
76
|
+
|
77
|
+
def round(in_round_input)
|
78
|
+
@game.guess(in_round_input)
|
79
|
+
puts @game.errors.first
|
80
|
+
end
|
81
|
+
|
82
|
+
def game_over?
|
83
|
+
@game.win? || @game.lost?
|
84
|
+
end
|
85
|
+
|
86
|
+
def the_end
|
87
|
+
show(:CORRECT_CODE, code: @game.secret_code.join.to_s)
|
88
|
+
return show(:LOSE_MESSAGE) if @game.lost?
|
89
|
+
|
90
|
+
show(:WIN_MESSAGE)
|
91
|
+
save_game
|
92
|
+
end
|
93
|
+
|
94
|
+
def save_game
|
95
|
+
show(:SAVE_MESSAGE, quit: COMMAND[:quit])
|
96
|
+
return saving_process if input == COMMAND[:yes]
|
97
|
+
end
|
98
|
+
|
99
|
+
def saving_process
|
100
|
+
Database.save(@game.to_h(@user.name))
|
101
|
+
end
|
102
|
+
|
103
|
+
def input
|
104
|
+
data_in = gets.chomp
|
105
|
+
data_in == COMMAND[:quit] ? quit : data_in
|
106
|
+
end
|
107
|
+
|
108
|
+
def show(*message)
|
109
|
+
puts I18n.t(*message)
|
110
|
+
end
|
111
|
+
|
112
|
+
def show_stats
|
113
|
+
tp Database.load_stats, :difficulty, :name, :attempts_total, :attempts_used, :hints_total, :hints_used
|
114
|
+
end
|
115
|
+
|
116
|
+
def quit
|
117
|
+
show(:GOODBYE)
|
118
|
+
exit
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
DB_PATH = './database/database.yml'
|
5
|
+
class Database
|
6
|
+
class << self
|
7
|
+
def load_stats
|
8
|
+
load_db.sort_by! { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def save(results)
|
12
|
+
database = load_db << results
|
13
|
+
File.open(DB_PATH, 'w+') { |file| file.write(database.to_yaml) }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def load_db
|
19
|
+
return YAML.load_file(DB_PATH) if File.exist?(DB_PATH)
|
20
|
+
|
21
|
+
[]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class Game
|
5
|
+
include Validations
|
6
|
+
attr_reader :difficulty, :errors, :secret_code
|
7
|
+
|
8
|
+
CODE_DIGITS = (1..6).freeze
|
9
|
+
CODE_LENGTH = 4
|
10
|
+
MATCH = '+'
|
11
|
+
PRESENCE = '-'
|
12
|
+
LEVELS = {
|
13
|
+
silly: {
|
14
|
+
attempts: 15,
|
15
|
+
hints: 2,
|
16
|
+
command: 'silly'
|
17
|
+
},
|
18
|
+
moderate: {
|
19
|
+
attempts: 10,
|
20
|
+
hints: 1,
|
21
|
+
command: 'moderate'
|
22
|
+
},
|
23
|
+
clever: {
|
24
|
+
attempts: 5,
|
25
|
+
hints: 1,
|
26
|
+
command: 'clever'
|
27
|
+
}
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
@secret_code = Array.new(CODE_LENGTH) { rand(CODE_DIGITS) }
|
32
|
+
@errors = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def guess(input)
|
36
|
+
user_number = input.chars.map(&:to_i)
|
37
|
+
validate(user_number)
|
38
|
+
@user_number = user_number
|
39
|
+
end
|
40
|
+
|
41
|
+
def start
|
42
|
+
@code = @secret_code.dup
|
43
|
+
@attempts -= 1
|
44
|
+
matches_result = check_matches
|
45
|
+
presence_result = check_presence
|
46
|
+
@result = (matches_result + presence_result).join
|
47
|
+
end
|
48
|
+
|
49
|
+
def win?
|
50
|
+
@result == MATCH * CODE_LENGTH
|
51
|
+
end
|
52
|
+
|
53
|
+
def lost?
|
54
|
+
@attempts.zero?
|
55
|
+
end
|
56
|
+
|
57
|
+
def hint
|
58
|
+
@hints.pop
|
59
|
+
end
|
60
|
+
|
61
|
+
def hints_left?
|
62
|
+
@hints.any?
|
63
|
+
end
|
64
|
+
|
65
|
+
def set(input)
|
66
|
+
return unless LEVELS.key?(input.to_sym)
|
67
|
+
|
68
|
+
@difficulty = LEVELS.dig(input.to_sym, :command)
|
69
|
+
@hints = @secret_code.sample(LEVELS.dig(@difficulty.to_sym, :hints))
|
70
|
+
@attempts = LEVELS.dig(@difficulty.to_sym, :attempts)
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_h(name)
|
74
|
+
{
|
75
|
+
name: name,
|
76
|
+
difficulty: @difficulty,
|
77
|
+
attempts_total: LEVELS.dig(@difficulty.to_sym, :attempts),
|
78
|
+
attempts_used: (LEVELS.dig(@difficulty.to_sym, :attempts) - @attempts),
|
79
|
+
hints_total: LEVELS.dig(@difficulty.to_sym, :hints),
|
80
|
+
hints_used: (LEVELS.dig(@difficulty.to_sym, :hints) - @hints.size)
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def check_matches
|
87
|
+
output = []
|
88
|
+
@code.each_with_index do |num, index|
|
89
|
+
next unless num == @user_number[index]
|
90
|
+
|
91
|
+
output << MATCH
|
92
|
+
@user_number[index] = nil
|
93
|
+
@code[index] = nil
|
94
|
+
end
|
95
|
+
output
|
96
|
+
end
|
97
|
+
|
98
|
+
def check_presence
|
99
|
+
output = []
|
100
|
+
@code.compact!
|
101
|
+
@user_number.compact.each do |num|
|
102
|
+
next unless @code.include? num
|
103
|
+
|
104
|
+
output << PRESENCE
|
105
|
+
@code[@code.index(num)] = nil
|
106
|
+
end
|
107
|
+
output
|
108
|
+
end
|
109
|
+
|
110
|
+
# rubocop:disable Metrics/LineLength
|
111
|
+
|
112
|
+
def validate(number)
|
113
|
+
@errors = []
|
114
|
+
@errors << I18n.t(:GUESS_ERROR, quan: CODE_LENGTH, min: CODE_DIGITS.min, max: CODE_DIGITS.max) unless match_checker(number.size, CODE_LENGTH)
|
115
|
+
|
116
|
+
number.each { |digit| @errors << I18n.t(:GUESS_ERROR, quan: CODE_LENGTH, min: CODE_DIGITS.min, max: CODE_DIGITS.max) unless range_checker(digit, CODE_DIGITS) }
|
117
|
+
end
|
118
|
+
|
119
|
+
# rubocop:enable Metrics/LineLength
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class User
|
5
|
+
include Validations
|
6
|
+
attr_reader :name, :errors
|
7
|
+
|
8
|
+
NAME_SIZE = (3..20).freeze
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@errors = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def name=(input)
|
15
|
+
validate(input)
|
16
|
+
@name = input
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def validate(name)
|
22
|
+
@errors << I18n.t(:NAME_ERROR, min: NAME_SIZE.min, max: NAME_SIZE.max) unless range_checker(name.size, NAME_SIZE)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/codebreaker.rb
ADDED
metadata
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker-d3n
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denny_Dee
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-12 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: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
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: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !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
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: table_print
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: i18n
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: super game
|
154
|
+
email: denys.khomiakov@gmail.com
|
155
|
+
executables: []
|
156
|
+
extensions: []
|
157
|
+
extra_rdoc_files: []
|
158
|
+
files:
|
159
|
+
- ".fasterer.yml"
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".travis.yml"
|
164
|
+
- Gemfile
|
165
|
+
- Gemfile.lock
|
166
|
+
- LICENSE.txt
|
167
|
+
- README.md
|
168
|
+
- Rakefile
|
169
|
+
- bin/console
|
170
|
+
- bin/setup
|
171
|
+
- codebreaker.gemspec
|
172
|
+
- config/locales/en.yml
|
173
|
+
- lib/autoload.rb
|
174
|
+
- lib/codebreaker.rb
|
175
|
+
- lib/codebreaker/console.rb
|
176
|
+
- lib/codebreaker/database.rb
|
177
|
+
- lib/codebreaker/game.rb
|
178
|
+
- lib/codebreaker/user.rb
|
179
|
+
- lib/codebreaker/validations.rb
|
180
|
+
- lib/codebreaker/version.rb
|
181
|
+
homepage: http://supergame.com.ua
|
182
|
+
licenses:
|
183
|
+
- AFL-1.1
|
184
|
+
metadata: {}
|
185
|
+
post_install_message:
|
186
|
+
rdoc_options: []
|
187
|
+
require_paths:
|
188
|
+
- lib
|
189
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
|
+
requirements:
|
196
|
+
- - ">="
|
197
|
+
- !ruby/object:Gem::Version
|
198
|
+
version: '0'
|
199
|
+
requirements: []
|
200
|
+
rubyforge_project:
|
201
|
+
rubygems_version: 2.7.6
|
202
|
+
signing_key:
|
203
|
+
specification_version: 4
|
204
|
+
summary: super summary
|
205
|
+
test_files: []
|