Class: CodebreakerKarolina::Game

Inherits:
Object
  • Object
show all
Includes:
CodebreakerConstants
Defined in:
lib/modules/game.rb

Constant Summary

Constants included from CodebreakerConstants

CodebreakerConstants::CODE_SIZE, CodebreakerConstants::DIFFICULTY, CodebreakerConstants::DIFFICULTY_SORT, CodebreakerConstants::FILE_NAME, CodebreakerConstants::MINUS, CodebreakerConstants::NUM_RANGE, CodebreakerConstants::PLUS

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/modules/game.rb', line 9

def date
  @date
end

#difficultyObject

Returns the value of attribute difficulty.



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

def difficulty
  @difficulty
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



9
10
11
# File 'lib/modules/game.rb', line 9

def secret_code
  @secret_code
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#compare_code(guess) ⇒ Object



17
18
19
# File 'lib/modules/game.rb', line 17

def compare_code(guess)
  comparison_result(@secret_code.chars, guess.chars)
end

#hintObject



34
35
36
37
38
39
# File 'lib/modules/game.rb', line 34

def hint
  return unless hints_left?

  @user.used_hints += 1
  @hint.shuffle!.pop
end

#lose?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/modules/game.rb', line 28

def lose?
  return true if @user.used_attempts >= @difficulty.attempts

  false
end

#startObject



11
12
13
14
15
# File 'lib/modules/game.rb', line 11

def start
  @date = DateTime.now
  @secret_code = generate_code.freeze
  @hint = @secret_code.chars.uniq
end

#valid_code?(code) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/modules/game.rb', line 41

def valid_code?(code)
  !!code[/\A[1-6]{4}\z/]
end

#win?(guess) ⇒ Boolean

Returns:

  • (Boolean)


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

def win?(guess)
  @user.used_attempts += 1
  return true if @secret_code == guess

  false
end