Class: CodebreakerSmn::Game

Inherits:
Object
  • Object
show all
Includes:
ValidationHelper
Defined in:
lib/codebreaker_smn.rb

Constant Summary collapse

DIFFICULTIES =
{
  hell: { attempts: 5, hints: 1 },
  medium: { attempts: 10, hints: 1 },
  easy: { attempts: 15, hints: 2 }
}.freeze
CODE_RULES =
{
  size: 4,
  digits: 1..6
}.freeze
USERNAME_RULES =
{
  min_length: 3,
  max_length: 20
}
WIN_RESULT =
'++++'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidationHelper

#not_empty_string, #positive_integer, #positive_integers, #valid_digit, #valid_digits, #valid_length

Constructor Details

#initializeGame

Returns a new instance of Game.



28
29
30
# File 'lib/codebreaker_smn.rb', line 28

def initialize
  new_game
end

Instance Attribute Details

#attemptsObject

Returns the value of attribute attempts.



26
27
28
# File 'lib/codebreaker_smn.rb', line 26

def attempts
  @attempts
end

#codeObject

Returns the value of attribute code.



25
26
27
# File 'lib/codebreaker_smn.rb', line 25

def code
  @code
end

#difficultyObject

Returns the value of attribute difficulty.



25
26
27
# File 'lib/codebreaker_smn.rb', line 25

def difficulty
  @difficulty
end

#hintsObject

Returns the value of attribute hints.



26
27
28
# File 'lib/codebreaker_smn.rb', line 26

def hints
  @hints
end

#stateObject

Returns the value of attribute state.



25
26
27
# File 'lib/codebreaker_smn.rb', line 25

def state
  @state
end

#usernameObject

Returns the value of attribute username.



25
26
27
# File 'lib/codebreaker_smn.rb', line 25

def username
  @username
end

Instance Method Details

#get_hintObject



62
63
64
65
66
67
68
69
70
# File 'lib/codebreaker_smn.rb', line 62

def get_hint
  result = hint_code
  if @hints.positive? && @state == :started
    @hints -= 1
    result.pop
  else
    'No hints left!'
  end
end

#guess_code(input) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/codebreaker_smn.rb', line 53

def guess_code(input)
  return unless valid_guess?(input)

  CodeHandler.process_guess(@code, input) do |result|
    select_winner(result)
    take_attempt
  end
end

#new_gameObject



32
33
34
35
# File 'lib/codebreaker_smn.rb', line 32

def new_game
  reset_params
  @state = :new
end

#startObject



37
38
39
40
# File 'lib/codebreaker_smn.rb', line 37

def start
  generate_code
  @state = :started
end

#statisticsObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/codebreaker_smn.rb', line 42

def statistics
  attempts_total = DIFFICULTIES[@difficulty][:attempts]
  attempts_used = attempts_total - @attempts
  hints_total = DIFFICULTIES[@difficulty][:hints]
  hints_used = hints_total - @hints
  { name: @username, difficulty: @difficulty.to_s,
    attempts_total: attempts_total, attempts_used: attempts_used,
    hints_total: hints_total, hints_used: hints_used,
    date: Date.today }
end