Module: GameLogic
- Includes:
- I18n
- Included in:
- CodebreakerCaptainjns::Game
- Defined in:
- lib/codebreaker_captainjns/game_logic.rb
Constant Summary collapse
- DIFFICULTY_HASH =
{ I18n.t('game.easy') => [15, 3], I18n.t('game.medium') => [10, 2], I18n.t('game.hard') => [5, 1], I18n.t('game.hell') => [3, 0] }.freeze
Instance Method Summary collapse
- #calc_attempts_and_hints(difficulty) ⇒ Object
- #check_numbers(secret, numbers) ⇒ Object
- #hint(secrets) ⇒ Object
- #make_number(chars = 4, numbers = 6) ⇒ Object
Instance Method Details
#calc_attempts_and_hints(difficulty) ⇒ Object
32 33 34 |
# File 'lib/codebreaker_captainjns/game_logic.rb', line 32 def calc_attempts_and_hints(difficulty) DIFFICULTY_HASH[difficulty] end |
#check_numbers(secret, numbers) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/codebreaker_captainjns/game_logic.rb', line 17 def check_numbers(secret, numbers) minuses = (secret & numbers).map { |element| [secret.count(element), numbers.count(element)].min }.sum result = '-' * minuses numbers.each.with_index do |number, index| result.sub!('-', '+') if number == secret[index] end result end |
#hint(secrets) ⇒ Object
28 29 30 |
# File 'lib/codebreaker_captainjns/game_logic.rb', line 28 def hint(secrets) secrets.shuffle!.pop end |
#make_number(chars = 4, numbers = 6) ⇒ Object
13 14 15 |
# File 'lib/codebreaker_captainjns/game_logic.rb', line 13 def make_number(chars = 4, numbers = 6) (1..chars).map { rand(1..numbers) }.join end |