Class: CodebreakerCaptainjns::Game

Inherits:
Object
  • Object
show all
Includes:
GameLogic, I18n
Defined in:
lib/codebreaker_captainjns/game.rb

Constant Summary

Constants included from GameLogic

GameLogic::DIFFICULTY_HASH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GameLogic

#calc_attempts_and_hints, #check_numbers, #hint, #make_number

Constructor Details

#initialize(params) ⇒ Game

Returns a new instance of Game.



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

def initialize(params)
  @name = params['username']
  @difficulty = params['difficulty']
  @attempts = calc_attempts_and_hints(@difficulty)[0]
  @hints = calc_attempts_and_hints(@difficulty)[1]
  @win = false
  @secret = make_number
  @unused_hints = @secret.chars
end

Instance Attribute Details

#att_totalObject (readonly)

Returns the value of attribute att_total.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def att_total
  @att_total
end

#attemptsObject (readonly)

Returns the value of attribute attempts.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def attempts
  @attempts
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def difficulty
  @difficulty
end

#hintsObject (readonly)

Returns the value of attribute hints.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def hints
  @hints
end

#hints_totalObject (readonly)

Returns the value of attribute hints_total.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def hints_total
  @hints_total
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def name
  @name
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def secret
  @secret
end

#winObject (readonly)

Returns the value of attribute win.



6
7
8
# File 'lib/codebreaker_captainjns/game.rb', line 6

def win
  @win
end

Instance Method Details

#check(number) ⇒ Object



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

def check(number)
  @attempts -= 1
  result = check_numbers(@secret.chars, number.chars)
  @win = true if result == '++++'
  result
end

#use_hintObject



25
26
27
28
29
30
# File 'lib/codebreaker_captainjns/game.rb', line 25

def use_hint
  return I18n.t('game.no_hints') unless @hints.positive?
  
  @hints -= 1
  hint(@unused_hints)
end