Class: Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_name, difficulty) ⇒ Game

Returns a new instance of Game.



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

def initialize(player_name, difficulty)
  @stats = Statistic.stats
  @player_name = player_name
  @difficulty_name = difficulty
  @attempts = DIFFICULTIES[difficulty.to_sym][:attempts]
  @hints = DIFFICULTIES[difficulty.to_sym][:hints]
  @db = DB
  @secret_code_for_hint = []
end

Instance Attribute Details

#attemptsObject

Returns the value of attribute attempts.



4
5
6
# File 'lib/game.rb', line 4

def attempts
  @attempts
end

#difficulty_nameObject

Returns the value of attribute difficulty_name.



4
5
6
# File 'lib/game.rb', line 4

def difficulty_name
  @difficulty_name
end

#hintsObject

Returns the value of attribute hints.



4
5
6
# File 'lib/game.rb', line 4

def hints
  @hints
end

#player_nameObject

Returns the value of attribute player_name.



4
5
6
# File 'lib/game.rb', line 4

def player_name
  @player_name
end

#secret_codeObject

Returns the value of attribute secret_code.



4
5
6
# File 'lib/game.rb', line 4

def secret_code
  @secret_code
end

#secret_code_for_hintObject (readonly)

Returns the value of attribute secret_code_for_hint.



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

def secret_code_for_hint
  @secret_code_for_hint
end

#statsObject (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
end

#user_codeObject

Returns the value of attribute user_code.



4
5
6
# File 'lib/game.rb', line 4

def user_code
  @user_code
end

Instance Method Details

#generate_codeObject



27
28
29
# File 'lib/game.rb', line 27

def generate_code
  Array.new(4) { rand(1..6) }
end

#lost?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/game.rb', line 48

def lost?
  attempts.zero?
end

#result(response) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/game.rb', line 31

def result(response)
  @user_code = response.each_char.map(&:to_i)
  @attempts -= 1
  return WINNING_RESULT if @secret_code == user_code

  Matching.new(self).create_response
end

#runObject



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

def run
  @secret_code = generate_code
  @secret_code_for_hint = @secret_code.clone
end

#save_resultObject



39
40
41
42
# File 'lib/game.rb', line 39

def save_result
  @stats.push(Statistic.generate_stats)
  DbUtils.add(@db, stats)
end

#use_hintObject



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

def use_hint
  @hints -= 1
  secret_code_for_hint.sort_by! { rand }.pop
end

#won?(result) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/game.rb', line 44

def won?(result)
  result == WINNING_RESULT
end