Class: Codebreaker::Game

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

Constant Summary collapse

ATTEMPTS =
7
HINT =
1
CODE_SIZE =
4
RANGE =
1..6

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Score

#save_score?, #score

Methods included from Engine

#check_guess

Constructor Details

#initialize(player = Player.new) ⇒ Game



12
13
14
15
16
17
18
19
# File 'lib/codebreaker/game.rb', line 12

def initialize(player = Player.new)
  @secret_code = Array.new(CODE_SIZE) { rand(RANGE) }.join
  @player  = player
  @hint    = HINT
  @attempts = ATTEMPTS

  start
end

Instance Attribute Details

#attemptsObject

Returns the value of attribute attempts.



11
12
13
# File 'lib/codebreaker/game.rb', line 11

def attempts
  @attempts
end

#hintObject

Returns the value of attribute hint.



11
12
13
# File 'lib/codebreaker/game.rb', line 11

def hint
  @hint
end

#playerObject

Returns the value of attribute player.



11
12
13
# File 'lib/codebreaker/game.rb', line 11

def player
  @player
end

#secret_codeObject

Returns the value of attribute secret_code.



11
12
13
# File 'lib/codebreaker/game.rb', line 11

def secret_code
  @secret_code
end

Instance Method Details

#startObject



21
22
23
24
25
26
# File 'lib/codebreaker/game.rb', line 21

def start
  puts "You have #{@attempts} attempts and #{@hint} hint."
  check_guess
  score
  new_game? ? start : save_score?
end