-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-logic.rb
55 lines (42 loc) · 1.7 KB
/
game-logic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'pry-byebug'
module GameLogic
def self.match?(guess, code)
code_copy = [] # To avoid mutating the original code array
code.map {|number| code_copy.push(number)}
exact_matches = 0
not_exact_matches = []
present_matches = 0
guess.each_with_index do |number, number_index|
if number == code[number_index]
exact_matches += 1
code_copy[number_index] = "Exact"
else not_exact_matches.push(number)
end
end
not_exact_matches.each do |number|
if code_copy.include?(number)
present_matches += 1
present_number_index = code_copy.index(number)
code_copy[present_number_index] = "Present"
end
end
return exact_matches, present_matches, not_exact_matches
end
def self.display_board(color_rows, peg_rows, current_row)
puts ""
puts " CURRENT BOARD:"
puts " Code Guesses Pegs"
puts "_____________________________________________________"
color_rows.each_with_index do |color_row, row_index|
peg_row = peg_rows[row_index]
if row_index == 1 + current_row
puts "| [0 0 0 0] | 0000 | 0000 | **"
elsif color_row == ["X", "X", "X", "X"]
puts "| [0 0 0 0] | 0000 | 0000 |"
else puts "| #{color_row} | #{peg_row.join(" | ")} |"
end
puts "-----------------------------------------------------"
end
puts ""
end
end