Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Includes:
GameConst, Message
Defined in:
lib/codebreaker/game.rb

Constant Summary

Constants included from GameConst

Codebreaker::GameConst::BONUS_POINTS, Codebreaker::GameConst::FIFTY_POINTS, Codebreaker::GameConst::HARD_LEVEL, Codebreaker::GameConst::MIDDLE_LEVEL, Codebreaker::GameConst::ONE_HUNDRED_POINTS, Codebreaker::GameConst::RIGHT_ANSWER, Codebreaker::GameConst::RIGHT_ANSWER_DIFF_INDEX, Codebreaker::GameConst::SIMPLE_LEVEL, Codebreaker::GameConst::TEN_POINTS, Codebreaker::GameConst::TWENTY_POINTS, Codebreaker::GameConst::WRONG_ANSWER, Codebreaker::GameConst::ZERO_POINTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*config) {|@configuration| ... } ⇒ Game

Returns a new instance of Game.

Yields:



10
11
12
13
14
15
16
# File 'lib/codebreaker/game.rb', line 10

def initialize(*config)
  @locale = Localization.new(:game)
  @configuration ||= GameConfiguration.new(*config)
  yield @configuration if block_given?
  apply_configuration
  generate_secret_code
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def attempts
  @attempts
end

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def configuration
  @configuration
end

#hintsObject (readonly)

Returns the value of attribute hints.



8
9
10
# File 'lib/codebreaker/game.rb', line 8

def hints
  @hints
end

Instance Method Details

#guess_valid?(input) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/codebreaker/game.rb', line 18

def guess_valid?(input)
  raise message['errors']['invalid_input'] unless input.is_a?(String)
  raise message['alerts']['invalid_input'] unless input[/\A[1-6]{4}\z/]
  true
end

#hintObject



34
35
36
37
38
39
40
41
42
# File 'lib/codebreaker/game.rb', line 34

def hint
  raise message['alerts']['no_hints'] if hints.zero?
  @hints -= 1
  return secret_code.sample if result.empty?
  not_guessed = result.chars.map.with_index do |item, index|
    secret_code[index] unless item == RIGHT_ANSWER
  end
  not_guessed.compact.sample
end

#scoreObject



44
45
46
# File 'lib/codebreaker/game.rb', line 44

def score
  calculate_score
end

#to_guess(input) ⇒ Object



24
25
26
27
28
# File 'lib/codebreaker/game.rb', line 24

def to_guess(input)
  raise message['alerts']['no_attempts'] if attempts.zero?
  @attempts -= 1
  @result = fancy_algo(input, secret_code)
end

#won?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/codebreaker/game.rb', line 30

def won?
  result == RIGHT_ANSWER * 4
end