codebreaker2018 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +108 -0
- data/Rakefile +6 -0
- data/bin/console +6 -0
- data/bin/setup +8 -0
- data/codebreaker.gemspec +26 -0
- data/lib/codebreaker/console.rb +186 -0
- data/lib/codebreaker/game.rb +123 -0
- data/lib/codebreaker/locale/console/en-US.yml +21 -0
- data/lib/codebreaker/locale/console/ru-RU.yml +21 -0
- data/lib/codebreaker/locale/game/en-US.yml +10 -0
- data/lib/codebreaker/locale/game/ru-RU.yml +10 -0
- data/lib/codebreaker/localization.rb +48 -0
- data/lib/codebreaker/score.rb +7 -0
- data/lib/codebreaker/version.rb +3 -0
- data/lib/codebreaker.rb +5 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61facc7472210fb21a64ea7f511a8e33f7198fce4a3b1f69b53d51a92b7703b0
|
4
|
+
data.tar.gz: dca15d6a979479efd75a40ce46e555c7d1fbd58811ad181cc3ccbe7d017d1d34
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 786ed3fd86aab78cc50881570755f2e306471c5be91d2a6454f852b2571c8ff67b6a5009af8286ce6e7af3ef57f76dacf49fb0f6bda18f5b44301ed9226d5e23
|
7
|
+
data.tar.gz: 17aede226827b05e93bdc5f2930162cbe4051e2faf3ccd0a08c7c00a99751ba12c884687050dcc288373a0a9b84780e267447972a591c81d6b1b36c08aa70586
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Vladislav Trotsenko
|
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,108 @@
|
|
1
|
+
# Codebreaker #
|
2
|
+
|
3
|
+
Codebreaker is a logic game in which the code-breaker tries to break a secret code created by a code-maker. The code-maker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
|
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 codebreaker2018
|
20
|
+
|
21
|
+
## Usage ##
|
22
|
+
Best way to demonstrate Codebreaker is auto start demo game:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'codebreaker2018'
|
26
|
+
Codebreaker::Console.new
|
27
|
+
```
|
28
|
+
|
29
|
+
### Game features ###
|
30
|
+
- Configurator:
|
31
|
+
- Player's name
|
32
|
+
- Max attempts and hints: integers only
|
33
|
+
- Levels: :simple, :middle, :hard
|
34
|
+
- Languages: :en, :ru
|
35
|
+
|
36
|
+
### Console features ###
|
37
|
+
- Autoloading localization by game language
|
38
|
+
- Color interface
|
39
|
+
- Ability to save results
|
40
|
+
- Ability to play again
|
41
|
+
- Ability to erase all results
|
42
|
+
- Demo mode
|
43
|
+
|
44
|
+
### Localization features ###
|
45
|
+
- Autoload localizations from locale dir
|
46
|
+
- Default language
|
47
|
+
- Ability to change locale
|
48
|
+
|
49
|
+
### Score features ###
|
50
|
+
- Date
|
51
|
+
- Player's name
|
52
|
+
- Winner or not
|
53
|
+
- Level
|
54
|
+
- Score
|
55
|
+
|
56
|
+
### Detail sample of Codebreaker usage ###
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# Init Game instance with block
|
60
|
+
game = Codebreaker::Game.new do |config|
|
61
|
+
config.player_name = 'Mike'
|
62
|
+
config.max_attempts = 5
|
63
|
+
config.max_hints = 2
|
64
|
+
config.level = :middle
|
65
|
+
config.lang = :en
|
66
|
+
end
|
67
|
+
|
68
|
+
# Alternative init Game instance with args
|
69
|
+
game = Codebreaker::Game.new('Mike', 5, 2, :middle, :en)
|
70
|
+
|
71
|
+
# Init Console instance with your game
|
72
|
+
console = Codebreaker::Console.new(game)
|
73
|
+
|
74
|
+
# Also you can auto load demo game instance and auto start it
|
75
|
+
Codebreaker::Console.new
|
76
|
+
|
77
|
+
# Interactive methods
|
78
|
+
# Let's play!
|
79
|
+
console.start_game
|
80
|
+
|
81
|
+
# Erase all game statistic
|
82
|
+
console.erase_scores
|
83
|
+
|
84
|
+
# Static methods
|
85
|
+
# Able to view current game instance into console
|
86
|
+
console.game
|
87
|
+
|
88
|
+
# Able to view path to yml-file
|
89
|
+
console.storage_path
|
90
|
+
|
91
|
+
# Able to view all game statistic
|
92
|
+
console.scores
|
93
|
+
```
|
94
|
+
|
95
|
+
## Development
|
96
|
+
|
97
|
+
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.
|
98
|
+
|
99
|
+
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).
|
100
|
+
|
101
|
+
## Contributing
|
102
|
+
|
103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bestwebua/homework-04-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.
|
104
|
+
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/codebreaker.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'codebreaker/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'codebreaker2018'
|
7
|
+
spec.version = Codebreaker::VERSION
|
8
|
+
spec.authors = ['Vladislav Trotsenko']
|
9
|
+
spec.email = ['admin@bestweb.com.ua']
|
10
|
+
|
11
|
+
spec.summary = %q{Codebreaker}
|
12
|
+
spec.description = %q{New version of logic terminal game.}
|
13
|
+
spec.homepage = 'https://github.com/bestwebua/homework-04-codebreaker'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'colorize', '~> 0.8.1'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
end
|
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
require 'erb'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Codebreaker
|
6
|
+
class Console
|
7
|
+
DEMO = Game.new('Demo User', 5, 2, :middle, :en)
|
8
|
+
HINT = '-h'.freeze
|
9
|
+
YES = 'y'.freeze
|
10
|
+
EMPTY_INPUT = ''.freeze
|
11
|
+
|
12
|
+
attr_reader :game, :storage_path, :scores
|
13
|
+
|
14
|
+
def initialize(game = DEMO)
|
15
|
+
@locale = Localization.new(:console)
|
16
|
+
load_console(game)
|
17
|
+
start_game if game == DEMO
|
18
|
+
end
|
19
|
+
|
20
|
+
def start_game
|
21
|
+
puts message['alerts']['welcome'].colorize(:background => :blue)
|
22
|
+
puts message['alerts']['hint_info']
|
23
|
+
submit_answer
|
24
|
+
end
|
25
|
+
|
26
|
+
def erase_scores
|
27
|
+
print message['alerts']['erase_scores']
|
28
|
+
erase_game_data if input_selector
|
29
|
+
exit_console
|
30
|
+
end
|
31
|
+
|
32
|
+
private_constant :DEMO
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def load_console(game)
|
37
|
+
raise ArgumentError, message['errors']['wrong_object'] unless game.is_a?(Game)
|
38
|
+
@game = game
|
39
|
+
@locale.lang = game.configuration.lang
|
40
|
+
@game_config_snapshot = game.configuration.clone
|
41
|
+
@storage_path = File.expand_path('./data/scores.yml', File.dirname(__FILE__))
|
42
|
+
@scores = load_game_data
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_game_data
|
46
|
+
YAML.load(File.open(storage_path, 'r')) rescue []
|
47
|
+
end
|
48
|
+
|
49
|
+
def message
|
50
|
+
@locale.localization
|
51
|
+
end
|
52
|
+
|
53
|
+
def submit_answer
|
54
|
+
process(user_interaction)
|
55
|
+
end
|
56
|
+
|
57
|
+
def show_hint
|
58
|
+
puts begin
|
59
|
+
"#{message['alerts']['hint']}: #{game.hint.to_s.green}"
|
60
|
+
rescue => error
|
61
|
+
error.to_s.red
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def user_interaction
|
66
|
+
return if game.attempts.zero?
|
67
|
+
input, status, step = EMPTY_INPUT, false, 0
|
68
|
+
until status
|
69
|
+
begin
|
70
|
+
game.guess_valid?(input)
|
71
|
+
status = true
|
72
|
+
rescue => error
|
73
|
+
puts error.to_s.red unless step.zero? || input == HINT
|
74
|
+
puts "#{message['alerts']['guess']}:"
|
75
|
+
input = gets.chomp
|
76
|
+
step += 1
|
77
|
+
show_hint if input == HINT
|
78
|
+
end
|
79
|
+
end
|
80
|
+
input
|
81
|
+
end
|
82
|
+
|
83
|
+
def message_is_allowed?
|
84
|
+
!game.won? && game.attempts == rand(1..game.configuration.max_attempts)
|
85
|
+
end
|
86
|
+
|
87
|
+
def motivation_message
|
88
|
+
return unless message_is_allowed?
|
89
|
+
message['alerts']['motivation']
|
90
|
+
end
|
91
|
+
|
92
|
+
def process(input)
|
93
|
+
begin
|
94
|
+
puts game.to_guess(input)
|
95
|
+
puts motivation_message
|
96
|
+
rescue => error
|
97
|
+
puts error
|
98
|
+
finish_game
|
99
|
+
end
|
100
|
+
game.won? ? finish_game : submit_answer
|
101
|
+
end
|
102
|
+
|
103
|
+
def summary
|
104
|
+
game.won? ? message['alerts']['won'].green : message['alerts']['lose'].red
|
105
|
+
end
|
106
|
+
|
107
|
+
def finish_game
|
108
|
+
puts ERB.new(message['info']['results']).result(binding)
|
109
|
+
save_game
|
110
|
+
new_game
|
111
|
+
end
|
112
|
+
|
113
|
+
def input_selector
|
114
|
+
input = EMPTY_INPUT
|
115
|
+
until %w(y n).include?(input)
|
116
|
+
print " (y/n) #{message['alerts']['yes_or_no']}:"
|
117
|
+
input = gets.chomp
|
118
|
+
end
|
119
|
+
input == YES
|
120
|
+
end
|
121
|
+
|
122
|
+
def save_game
|
123
|
+
print message['alerts']['save_game']
|
124
|
+
save_game_data if input_selector
|
125
|
+
end
|
126
|
+
|
127
|
+
def save_game_data
|
128
|
+
save_user_score
|
129
|
+
save_to_yml
|
130
|
+
puts message['info']['successfully_saved'].green
|
131
|
+
end
|
132
|
+
|
133
|
+
def save_user_score
|
134
|
+
scores << current_user_score
|
135
|
+
end
|
136
|
+
|
137
|
+
def current_user_score
|
138
|
+
Score.new do |save|
|
139
|
+
save.date = Time.now
|
140
|
+
save.player_name = game.configuration.player_name
|
141
|
+
save.winner = game.won?
|
142
|
+
save.level = game.configuration.level
|
143
|
+
save.score = game.score
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def save_to_yml
|
148
|
+
File.open(storage_path, 'w') do |file|
|
149
|
+
file.write(YAML.dump(scores))
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def new_game
|
154
|
+
print message['alerts']['new_game']
|
155
|
+
if input_selector
|
156
|
+
load_new_game
|
157
|
+
start_game
|
158
|
+
else
|
159
|
+
puts message['alerts']['shutdown']
|
160
|
+
exit_console
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def exit_console
|
165
|
+
exit
|
166
|
+
end
|
167
|
+
|
168
|
+
def load_new_game
|
169
|
+
@game = Game.new do |config|
|
170
|
+
@game_config_snapshot.each_pair do |key, value|
|
171
|
+
config[key] = value
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def erase_game_data
|
177
|
+
begin
|
178
|
+
File.delete(storage_path)
|
179
|
+
scores.clear
|
180
|
+
puts message['info']['successfully_erased'].green
|
181
|
+
rescue
|
182
|
+
puts message['errors']['file_not_found'].red
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module Codebreaker
|
2
|
+
GameConfiguration = Struct.new(:player_name, :max_attempts, :max_hints, :level, :lang)
|
3
|
+
|
4
|
+
class Game
|
5
|
+
ZERO_POINTS = 0
|
6
|
+
TEN_POINTS = 10
|
7
|
+
TWENTY_POINTS = 20
|
8
|
+
FIFTY_POINTS = 50
|
9
|
+
ONE_HUNDRED_POINTS = 100
|
10
|
+
BONUS_POINTS = 500
|
11
|
+
RIGHT_ANSWER = '+'.freeze
|
12
|
+
RIGHT_ANSWER_DIFF_INDEX = '-'.freeze
|
13
|
+
WRONG_ANSWER = ' '.freeze
|
14
|
+
|
15
|
+
attr_reader :attempts, :hints, :configuration
|
16
|
+
|
17
|
+
def initialize(*config)
|
18
|
+
@locale = Localization.new(:game)
|
19
|
+
@configuration ||= GameConfiguration.new(*config)
|
20
|
+
yield @configuration if block_given?
|
21
|
+
apply_configuration
|
22
|
+
generate_secret_code
|
23
|
+
end
|
24
|
+
|
25
|
+
def guess_valid?(input)
|
26
|
+
raise message['errors']['invalid_input'] unless input.is_a?(String)
|
27
|
+
raise message['alerts']['invalid_input'] unless input[/\A[1-6]{4}\z/]
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_guess(input)
|
32
|
+
raise message['alerts']['no_attempts'] if attempts.zero?
|
33
|
+
@attempts -= 1
|
34
|
+
@result = fancy_algo(input, secret_code)
|
35
|
+
end
|
36
|
+
|
37
|
+
def won?
|
38
|
+
result == RIGHT_ANSWER * 4
|
39
|
+
end
|
40
|
+
|
41
|
+
def hint
|
42
|
+
raise message['alerts']['no_hints'] if hints.zero?
|
43
|
+
@hints -= 1
|
44
|
+
return secret_code.sample if result.empty?
|
45
|
+
not_guessed = result.chars.map.with_index do |item, index|
|
46
|
+
secret_code[index] unless item == RIGHT_ANSWER
|
47
|
+
end
|
48
|
+
not_guessed.compact.sample
|
49
|
+
end
|
50
|
+
|
51
|
+
def score
|
52
|
+
calculate_score
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def check_configuration
|
58
|
+
raise message['errors']['fail_configuration'] if configuration.any?(&:nil?)
|
59
|
+
begin
|
60
|
+
raise if configuration.max_attempts < 1 || configuration.max_hints.negative?
|
61
|
+
rescue
|
62
|
+
raise message['errors']['fail_configuration_values']
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_instance_vars
|
67
|
+
@attempts = configuration.max_attempts
|
68
|
+
@hints = configuration.max_hints
|
69
|
+
@locale.lang = configuration.lang
|
70
|
+
@result = ''
|
71
|
+
end
|
72
|
+
|
73
|
+
def apply_configuration
|
74
|
+
check_configuration
|
75
|
+
configuration.freeze
|
76
|
+
create_instance_vars
|
77
|
+
end
|
78
|
+
|
79
|
+
def result
|
80
|
+
@result
|
81
|
+
end
|
82
|
+
|
83
|
+
def message
|
84
|
+
@locale.localization
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_secret_code
|
88
|
+
@secret_code = (1..4).map { rand(1..6) }
|
89
|
+
end
|
90
|
+
|
91
|
+
def secret_code
|
92
|
+
@secret_code
|
93
|
+
end
|
94
|
+
|
95
|
+
def fancy_algo(guess, secret_code)
|
96
|
+
guess.chars.map(&:to_i).map.with_index do |item, index|
|
97
|
+
case
|
98
|
+
when item == secret_code[index] then RIGHT_ANSWER
|
99
|
+
when secret_code[index..-1].include?(item) then RIGHT_ANSWER_DIFF_INDEX
|
100
|
+
else WRONG_ANSWER
|
101
|
+
end
|
102
|
+
end.join
|
103
|
+
end
|
104
|
+
|
105
|
+
def calculate_score
|
106
|
+
level_rates = case configuration.level
|
107
|
+
when :simple then [TEN_POINTS, ZERO_POINTS]
|
108
|
+
when :middle then [TWENTY_POINTS, TWENTY_POINTS]
|
109
|
+
when :hard then [FIFTY_POINTS, ONE_HUNDRED_POINTS]
|
110
|
+
else raise message['errors']['unknown_level']
|
111
|
+
end
|
112
|
+
|
113
|
+
attempt_rate, hint_rate = level_rates
|
114
|
+
guessed = result.count(RIGHT_ANSWER)
|
115
|
+
|
116
|
+
used_attempts = configuration.max_attempts - attempts
|
117
|
+
used_hints = configuration.max_hints - hints
|
118
|
+
bonus_points = won? && used_attempts == 1 ? BONUS_POINTS : ZERO_POINTS
|
119
|
+
|
120
|
+
used_attempts * attempt_rate * guessed - used_hints * hint_rate + bonus_points
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
:en:
|
2
|
+
errors:
|
3
|
+
wrong_object: Wrong object type!
|
4
|
+
file_not_found: Game data file not found. Nothing to delete.
|
5
|
+
alerts:
|
6
|
+
welcome: ....Codebreaker Console Game....
|
7
|
+
hint_info: Use '-h' for help.
|
8
|
+
hint: Hint
|
9
|
+
guess: Enter your guess
|
10
|
+
motivation: Come on, step on it!
|
11
|
+
won: You won!
|
12
|
+
lose: You lost(
|
13
|
+
yes_or_no:
|
14
|
+
save_game: Do you want to save your score?
|
15
|
+
new_game: Do you want to play again?
|
16
|
+
erase_scores: Do you want to erase all scores data? Attention, this action cannot be undone.
|
17
|
+
shutdown: Exit...
|
18
|
+
info:
|
19
|
+
results: <%= summary %> With total score <%= game.score %> points.
|
20
|
+
successfully_saved: Current game data was successfully saved.
|
21
|
+
successfully_erased: All game data was successfully erased.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
:ru:
|
2
|
+
errors:
|
3
|
+
wrong_object: Неверный тип объекта!
|
4
|
+
file_not_found: Файл данных не найден. Ничего удалять.
|
5
|
+
alerts:
|
6
|
+
welcome: ....Терминальная игра Codebreaker....
|
7
|
+
hint_info: Используй '-h' для подсказки.
|
8
|
+
hint: Подсказка
|
9
|
+
guess: Введи свое предположение
|
10
|
+
motivation: Подумай хорошенько и сделай это!
|
11
|
+
won: Ты выиграл!
|
12
|
+
lose: Ты проиграл(
|
13
|
+
yes_or_no: латинская раскладка
|
14
|
+
save_game: Сохранить твой результат?
|
15
|
+
new_game: Еще поиграем?
|
16
|
+
erase_scores: Очистить всю игровую статистику? Внимание, это действие не может быть отменено.
|
17
|
+
shutdown: Выхожу...
|
18
|
+
info:
|
19
|
+
results: <%= summary %> С общим счетом <%= game.score %> поинтов.
|
20
|
+
successfully_saved: Твой текущий результат успешно сохранен.
|
21
|
+
successfully_erased: Все игровая статистика была успешно удалена.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
:en:
|
2
|
+
errors:
|
3
|
+
fail_configuration: The configuration is incomplete.
|
4
|
+
fail_configuration_values: Max_attempts must be more then zero, max_hints must be positive.
|
5
|
+
invalid_input: Invalid input type.
|
6
|
+
unknown_level: Unknown game level.
|
7
|
+
alerts:
|
8
|
+
invalid_input: Answer should equal 4 digits in range from 1 to 6!
|
9
|
+
no_attempts: Oops, no attempts left!
|
10
|
+
no_hints: Oops, no hints left!
|
@@ -0,0 +1,10 @@
|
|
1
|
+
:ru:
|
2
|
+
errors:
|
3
|
+
fail_configuration: Конфигурация не завершена.
|
4
|
+
fail_configuration_values: Max_attempts > 0, max_hints не должен быть <= 0.
|
5
|
+
invalid_input: Недопустимый тип ввода.
|
6
|
+
unknown_level: Неизвестный игровой уровень.
|
7
|
+
alerts:
|
8
|
+
invalid_input: Ответ должен содержать 4 цифры в диапазоне от 1 до 6!
|
9
|
+
no_attempts: Доступные попытки исчерпаны!
|
10
|
+
no_hints: К сожалению, больше нет подсказок!
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Codebreaker
|
4
|
+
class Localization
|
5
|
+
attr_accessor :lang
|
6
|
+
|
7
|
+
def initialize(app_type, lang = :en)
|
8
|
+
@lang = lang
|
9
|
+
select_application(app_type)
|
10
|
+
candidates_to_load
|
11
|
+
merge_localizations
|
12
|
+
end
|
13
|
+
|
14
|
+
def localization
|
15
|
+
return localizations[:en] unless localizations[lang]
|
16
|
+
localizations[lang]
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def localizations_dir
|
22
|
+
File.expand_path('./locale/.', File.dirname(__FILE__))
|
23
|
+
end
|
24
|
+
|
25
|
+
def authorized_apps
|
26
|
+
Dir.entries(localizations_dir).reject { |dir_name| dir_name.include?('.') }.map(&:to_sym)
|
27
|
+
end
|
28
|
+
|
29
|
+
def select_application(app_type)
|
30
|
+
raise 'Unknown application type.' unless authorized_apps.include?(app_type)
|
31
|
+
@app_dir = app_type.to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
def candidates_to_load
|
35
|
+
@ymls_paths = Dir.glob("#{localizations_dir}/#{@app_dir}/*.yml")
|
36
|
+
end
|
37
|
+
|
38
|
+
def localizations
|
39
|
+
@localizations ||= Hash.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def merge_localizations
|
43
|
+
localizations
|
44
|
+
loaded_ymls = @ymls_paths.map { |file| YAML.load(File.open(file, 'r')) }
|
45
|
+
loaded_ymls.each { |loaded_yml| @localizations.update(loaded_yml) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/codebreaker.rb
ADDED
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codebreaker2018
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vladislav Trotsenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: New version of logic terminal game.
|
70
|
+
email:
|
71
|
+
- admin@bestweb.com.ua
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-version"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- codebreaker.gemspec
|
87
|
+
- lib/codebreaker.rb
|
88
|
+
- lib/codebreaker/console.rb
|
89
|
+
- lib/codebreaker/game.rb
|
90
|
+
- lib/codebreaker/locale/console/en-US.yml
|
91
|
+
- lib/codebreaker/locale/console/ru-RU.yml
|
92
|
+
- lib/codebreaker/locale/game/en-US.yml
|
93
|
+
- lib/codebreaker/locale/game/ru-RU.yml
|
94
|
+
- lib/codebreaker/localization.rb
|
95
|
+
- lib/codebreaker/score.rb
|
96
|
+
- lib/codebreaker/version.rb
|
97
|
+
homepage: https://github.com/bestwebua/homework-04-codebreaker
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.7.3
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Codebreaker
|
121
|
+
test_files: []
|