Class: CodebreakerGem::Game

Inherits:
Object
  • Object
show all
Includes:
Loader
Defined in:
lib/codebreaker_gem.rb

Constant Summary collapse

HINTS =
3
ATTEMPTS =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loader

#read_achievements, #save_achievement

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @response = []
  @hints = HINTS
  @attempts = ATTEMPTS
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



14
15
16
# File 'lib/codebreaker_gem.rb', line 14

def attempts
  @attempts
end

#guessObject

Returns the value of attribute guess.



15
16
17
# File 'lib/codebreaker_gem.rb', line 15

def guess
  @guess
end

#hintObject (readonly)

Returns the value of attribute hint.



14
15
16
# File 'lib/codebreaker_gem.rb', line 14

def hint
  @hint
end

#hintsObject (readonly)

Returns the value of attribute hints.



14
15
16
# File 'lib/codebreaker_gem.rb', line 14

def hints
  @hints
end

#responseObject (readonly)

Returns the value of attribute response.



14
15
16
# File 'lib/codebreaker_gem.rb', line 14

def response
  @response
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



14
15
16
# File 'lib/codebreaker_gem.rb', line 14

def secret_code
  @secret_code
end

Instance Method Details

#checkObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/codebreaker_gem.rb', line 27

def check
  return @response = '+' * @secret_code.length if @secret_code == @guess
  code, guess = @secret_code.split('').zip(@guess.split('')).delete_if { |item| item[0] == item[1] }.transpose
  if !code || !guess
    @response = ['+'] * @secret_code.length
  else
    @response = ['+'] * (@secret_code.length - code.length)
    @response.concat(get_minuses(code, guess))
  end
  @response = @response.join.to_s
  @attempts -= 1
end

#generate_codeObject



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

def generate_code()
  @secret_code = 4.times.map{ Random.rand(1...6) }.join.to_s
end

#get_hintObject



40
41
42
43
44
# File 'lib/codebreaker_gem.rb', line 40

def get_hint
  @hint = @hints >= 1 ? @secret_code.split('').sample : false
  @hints -= 1 if @hints > 0
  @attempts -= 1
end

#get_scoresObject



46
47
48
49
50
51
52
# File 'lib/codebreaker_gem.rb', line 46

def get_scores
  scores = Hash.new
  scores[:hints] = HINTS - @hints
  scores[:attempts] = ATTEMPTS - @attempts
  scores[:secret_code] = @secret_code
  scores
end