Class: Codebreaker::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#to_array, #verify

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @hint_avaliable = true
  @secret_code = generate_code
  @attempts_spent = 0
  @attempts_left = 0
end

Instance Attribute Details

#attempts_leftObject

Returns the value of attribute attempts_left.



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

def attempts_left
  @attempts_left
end

#attempts_spentObject

Returns the value of attribute attempts_spent.



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

def attempts_spent
  @attempts_spent
end

#hint_avaliableObject

Returns the value of attribute hint_avaliable.



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

def hint_avaliable
  @hint_avaliable
end

#secret_codeObject (readonly)

Returns the value of attribute secret_code.



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

def secret_code
  @secret_code
end

#wonObject

Returns the value of attribute won.



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

def won
  @won
end

Instance Method Details

#code_hucked_with?(user_input) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/codebreaker/game.rb', line 58

def code_hucked_with?(user_input)
  to_array(user_input) == secret_code
end

#delete_coincidence(code, user_input) ⇒ Object



50
51
52
# File 'lib/codebreaker/game.rb', line 50

def delete_coincidence(code, user_input)
  code.zip(user_input).delete_if { |item| item[0] == item[1] }
end

#delete_if_need(code, input) ⇒ Object



43
44
45
46
47
48
# File 'lib/codebreaker/game.rb', line 43

def delete_if_need(code,input)
  code.each_with_index do |elem, i|
    next if input.count(elem).zero?
    code.delete_at(i) if code.count(elem) > input.count(elem)
  end
end

#find_plus_minus(user_input) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/codebreaker/game.rb', line 33

def find_plus_minus(user_input)
  half_coincidence = delete_coincidence(secret_code, to_array(user_input))
  plus = '+' * (4 - half_coincidence.size)
  code_left = half_coincidence.transpose[0]
  input_left = half_coincidence.transpose[1]
  code_left = delete_if_need(code_left, input_left)
  minus = '-' * (code_left.size - without_minus_size(code_left, input_left))
  plus + minus
end

#formated_hintObject



68
69
70
71
72
73
# File 'lib/codebreaker/game.rb', line 68

def formated_hint
  hint = Array.new(4) { '*' }
  position = rand(0..3)
  hint[position] = @secret_code[position]
  hint.join
end

#generate_codeObject



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

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

#process_hintObject



62
63
64
65
66
# File 'lib/codebreaker/game.rb', line 62

def process_hint
  return 'no hint' unless @hint_avaliable
  @hint_avaliable = false
  formated_hint
end

#reply(input) ⇒ Object



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

def reply(input)
  return @won = :won if code_hucked_with? input
  return @won = :lose if @attempts_left.zero?
  route(input)
end

#route(input) ⇒ Object



26
27
28
29
30
31
# File 'lib/codebreaker/game.rb', line 26

def route(input)
  @attempts_left -= 1
  @attempts_spent += 1
  return find_plus_minus(input) if !!input.match(/[\d]/)
  process_hint
end

#without_minus_size(code, input) ⇒ Object



54
55
56
# File 'lib/codebreaker/game.rb', line 54

def without_minus_size(code, input)
  code.delete_if { |item| input.include?(item) }.size
end