Class: CodebrekerManfly::Game

Inherits:
ValidatableEntity show all
Defined in:
lib/codebreker_manfly/entities/game.rb

Instance Attribute Summary collapse

Attributes inherited from ValidatableEntity

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ValidatableEntity

#valid?

Methods included from Validator

#valid_class?, #valid_non_empty_string?, #valid_non_negative_integer?, #valid_only_numeric_string?, #valid_positive_integer?, #valid_range?, #valid_string_max_length?, #valid_string_min_length?

Constructor Details

#initialize(difficulty, user) ⇒ Game

Returns a new instance of Game.



7
8
9
10
11
# File 'lib/codebreker_manfly/entities/game.rb', line 7

def initialize(difficulty, user)
  super()
  @difficulty = difficulty
  @user = user
end

Instance Attribute Details

#attempts_amountObject (readonly)

Returns the value of attribute attempts_amount.



5
6
7
# File 'lib/codebreker_manfly/entities/game.rb', line 5

def attempts_amount
  @attempts_amount
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/codebreker_manfly/entities/game.rb', line 5

def code
  @code
end

#difficultyObject (readonly)

Returns the value of attribute difficulty.



5
6
7
# File 'lib/codebreker_manfly/entities/game.rb', line 5

def difficulty
  @difficulty
end

#hints_amountObject (readonly)

Returns the value of attribute hints_amount.



5
6
7
# File 'lib/codebreker_manfly/entities/game.rb', line 5

def hints_amount
  @hints_amount
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'lib/codebreker_manfly/entities/game.rb', line 5

def user
  @user
end

Class Method Details

.user_statisticObject



17
18
19
20
# File 'lib/codebreker_manfly/entities/game.rb', line 17

def self.user_statistic
  store = CodebrekerManfly::CodebreakerStore.new
  store.data[:user_statistics].sort_by { |stats| [stats.difficulty, stats.attempts, stats.hints] }
end

Instance Method Details

#lose?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/codebreker_manfly/entities/game.rb', line 45

def lose?
  attempts_amount < 1 && !win?
end

#make_turn(guess) ⇒ Object



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

def make_turn(guess)
  @guess = guess
  @attempts_amount -= 1

  matcher = CodebrekerManfly::CodeMatcher.new(code, guess.code)
  matcher.match_codes
end

#restartObject



49
50
51
# File 'lib/codebreker_manfly/entities/game.rb', line 49

def restart
  prepare_game
end

#save_statisticObject



22
23
24
25
26
# File 'lib/codebreker_manfly/entities/game.rb', line 22

def save_statistic
  store = CodebrekerManfly::CodebreakerStore.new
  store.data[:user_statistics] << current_statistic
  store.save
end

#startObject



13
14
15
# File 'lib/codebreker_manfly/entities/game.rb', line 13

def start
  prepare_game
end

#take_hintObject



28
29
30
31
# File 'lib/codebreker_manfly/entities/game.rb', line 28

def take_hint
  @hints_amount -= 1
  @hints.pop
end

#win?Boolean

Returns:

  • (Boolean)


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

def win?
  code == @guess&.code
end