Class: Codebreaker::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker/game.rb

Constant Summary collapse

GAME_SETTINGS =
{ easy:       { attempts: 10, symbols_count: 3, symbols_range: 6,   hints: 3 },
medium:     { attempts: 10, symbols_count: 4, symbols_range: 6,   hints: 1 },
hard:       { attempts: 20, symbols_count: 6, symbols_range: 6,   hints: 1 },
very_hard:  { attempts: 25, symbols_count: 4, symbols_range: 15,  hints: 1 },
gosu:       { attempts: 35, symbols_count: 6, symbols_range: 15,  hints: 0 },
mlg:        { attempts: 1,  symbols_count: 4, symbols_range: 6,   hints: 0 } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



14
15
16
# File 'lib/codebreaker/game.rb', line 14

def initialize
  @state = :initialized
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def attempts
  @attempts
end

#elements_revealedObject (readonly)

Returns the value of attribute elements_revealed.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def elements_revealed
  @elements_revealed
end

#hints_leftObject (readonly)

Returns the value of attribute hints_left.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def hints_left
  @hints_left
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def max_attempts
  @max_attempts
end

#max_hintsObject (readonly)

Returns the value of attribute max_hints.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def max_hints
  @max_hints
end

#scoreObject (readonly)

Returns the value of attribute score.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def score
  @score
end

#secretObject (readonly)

Returns the value of attribute secret.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def secret
  @secret
end

#settingsObject (readonly)

Returns the value of attribute settings.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def settings
  @settings
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def state
  @state
end

#symbols_countObject (readonly)

Returns the value of attribute symbols_count.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def symbols_count
  @symbols_count
end

#symbols_rangeObject (readonly)

Returns the value of attribute symbols_range.



3
4
5
# File 'lib/codebreaker/game.rb', line 3

def symbols_range
  @symbols_range
end

Instance Method Details

#attempts_takenObject



74
75
76
# File 'lib/codebreaker/game.rb', line 74

def attempts_taken
  @max_attempts - @attempts
end

#guess(code) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/codebreaker/game.rb', line 36

def guess(code)
  fail IndexError     if code.length != @secret.length
  fail ArgumentError  unless code.all? { |x| (1..@symbols_range).include?(x) }

  if code == @secret
    win
  else
    @attempts -= 1
    lose if @attempts.zero?
  end
  secret = @secret.dup
  [extract_exact_matches(secret, code), extract_close_matches(secret, code)]
end

#hintObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/codebreaker/game.rb', line 78

def hint
  if @hints_left.zero?
    fail 'No more hints available'
  else
    random_symbol_index = 0
    loop do
      random_symbol_index = rand(@secret.size)
      break unless @elements_revealed.include? random_symbol_index
    end
    @elements_revealed << random_symbol_index
    @hints_left -= 1
    hint = ([nil]*@secret.size)
    hint[random_symbol_index] = @secret[random_symbol_index]
    hint
  end
end

#hint?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/codebreaker/game.rb', line 70

def hint?
  @hints_left > 0
end

#loseObject



65
66
67
68
# File 'lib/codebreaker/game.rb', line 65

def lose
  @score = 0
  @state = :lost
end

#lose?Boolean Also known as: lost?

Returns:

  • (Boolean)


55
56
57
# File 'lib/codebreaker/game.rb', line 55

def lose?
  @state == :lost
end

#restartObject



32
33
34
# File 'lib/codebreaker/game.rb', line 32

def restart
  start(@settings)
end

#start(diff = :medium) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codebreaker/game.rb', line 18

def start(diff = :medium)
  @settings = diff
  settings = GAME_SETTINGS[diff]
  @max_attempts       = settings[:attempts]
  @attempts           = @max_attempts
  @symbols_count      = settings[:symbols_count]
  @symbols_range      = settings[:symbols_range]
  @secret             = generate_code
  @max_hints          = settings[:hints]
  @hints_left         = @max_hints
  @elements_revealed  = []
  @state              = :playing
end

#winObject



60
61
62
63
# File 'lib/codebreaker/game.rb', line 60

def win
  @score = @attempts * @symbols_count * @symbols_range + @hints_left*20
  @state = :won
end

#win?Boolean Also known as: won?

Returns:

  • (Boolean)


50
51
52
# File 'lib/codebreaker/game.rb', line 50

def win?
  @state == :won
end