Class: MM::GuessEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/games/mastermind/guess_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#guessObject

Returns the value of attribute guess.



7
8
9
# File 'lib/games/mastermind/guess_evaluator.rb', line 7

def guess
  @guess
end

#resultObject

Returns the value of attribute result.



8
9
10
# File 'lib/games/mastermind/guess_evaluator.rb', line 8

def result
  @result
end

#secret_codeObject

Returns the value of attribute secret_code.



6
7
8
# File 'lib/games/mastermind/guess_evaluator.rb', line 6

def secret_code
  @secret_code
end

Instance Method Details

#delete_elements_already_matched(set_of_matched_indexes) ⇒ Object



40
41
42
43
# File 'lib/games/mastermind/guess_evaluator.rb', line 40

def delete_elements_already_matched(set_of_matched_indexes)
  secret_code.delete_if.with_index { |_, index| set_of_matched_indexes.include? index }
  guess.delete_if.with_index { |_, index| set_of_matched_indexes.include? index }
end

#evaluate_guess(secret_code, guess) ⇒ Object

returns “XXXX” if perfect guess. returns XXO, for example, if two perfect guesses and one right digit but wrong place



11
12
13
14
15
16
17
18
19
# File 'lib/games/mastermind/guess_evaluator.rb', line 11

def evaluate_guess(secret_code, guess)
  @secret_code = Marshal.load(Marshal.dump(secret_code))
  @guess = Marshal.load(Marshal.dump(guess))
  #default when game starts is to have empty current_guess array
  if guess.empty?
    return []
  end
  @result = find_result
end

#find_exact_matchesObject



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

def find_exact_matches
  resX = []
  set_of_matched_indexes = Set.new
  guess.each_with_index do |element, index|
    if element == secret_code[index]
      resX.push ("X")
      set_of_matched_indexes.add(index)
    end
  end
  delete_elements_already_matched(set_of_matched_indexes)
  resX
end

#find_partial_matchesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/games/mastermind/guess_evaluator.rb', line 45

def find_partial_matches
  resO = []
  return resO if secret_code.empty? || guess.empty?
  secret_code.sort!
  guess.sort!

  secret_code_iterator = Shared::ArrayIterator.new(secret_code)
  guess_iterator = Shared::ArrayIterator.new(guess)

  #get first elements from enums
  code_element = secret_code_iterator.next_item
  guess_element = guess_iterator.next_item


  while true
    if guess_element == code_element
      resO.push("O")
      if secret_code_iterator.has_next? && guess_iterator.has_next?
        code_element = secret_code_iterator.next_item
        guess_element = guess_iterator.next_item
      else
        break
      end
    elsif guess_element > code_element
      if secret_code_iterator.has_next?
        code_element = secret_code_iterator.next_item
      else
        break
      end
    else
      if guess_iterator.has_next?
        guess_element = guess_iterator.next_item
      else
        break
      end
    end
  end
  resO
end

#find_resultObject



21
22
23
24
25
# File 'lib/games/mastermind/guess_evaluator.rb', line 21

def find_result
  resX = find_exact_matches
  resO = find_partial_matches
  resX.concat resO
end