Class: Codebreaker::Game

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

Constant Summary collapse

ATTEMPTS =
10
HINTS =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @secret_code = generate
  @shuffled_secret_code = @secret_code.shuffle
  @used_attempts = 0
  @used_hints = 0
end

Instance Attribute Details

#used_attemptsObject (readonly)

Returns the value of attribute used_attempts.



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

def used_attempts
  @used_attempts
end

#used_hintsObject (readonly)

Returns the value of attribute used_hints.



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

def used_hints
  @used_hints
end

Instance Method Details

#hintObject



15
16
17
18
# File 'lib/codebreaker/game.rb', line 15

def hint
  @used_hints += 1
  @shuffled_secret_code.pop
end

#make_guess(user_code) ⇒ Object



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

def make_guess(user_code)
  return unless code_valid?(user_code)
  @used_attempts += 1
  @user_code = user_code.chars.map(&:to_i)
  return '++++' if @user_code == @secret_code
  exact_matches + number_matches
end

#save_result(username:, game_status:, file_name: 'player_results.txt') ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/codebreaker/game.rb', line 28

def save_result(username:, game_status:, file_name: 'player_results.txt')
  File.open(file_name, 'a') do |file|
    file.puts("Name: #{username}")
    file.puts("Game status: #{game_status}")
    file.puts("Attempts used: #{@used_attempts}")
    file.puts("Hints used: #{@used_hints}")
    file.puts('*' * 40)
  end
end