-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.rb
46 lines (34 loc) · 1.06 KB
/
pacman.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
class Pacman
def next_step lookout
pacmans_next_step = []
pacman_surrounds = get_pacman_surrounds lookout
pacman_surrounds.each_pair do |position, holder|
pacmans_next_step = position if pacman_surrounds[position] == :empty
end
pacmans_next_step
end
def get_pacman_surrounds lookout
pacman_surrounds = {}
pacman_spot = get_pacman_spot lookout
if pacman_spot.length > 0
positions = [[pacman_spot],
[pacman_spot[0], pacman_spot[1]+1],
[pacman_spot[0], pacman_spot[1]-1],
[pacman_spot[0]+1, pacman_spot[1]],
[pacman_spot[0]-1, pacman_spot[1]],
[pacman_spot[0]+1, pacman_spot[1]+1],
[pacman_spot[0]+1, pacman_spot[1]-1],
[pacman_spot[0]-1, pacman_spot[1]+1],
[pacman_spot[0]-1, pacman_spot[1]-1]]
pacman_surrounds = lookout.select{|position, holder| positions.include? position}
end
pacman_surrounds
end
def get_pacman_spot lookout
pacman_spot = []
lookout.each_pair do |position, holder|
pacman_spot = position if holder == :pacman
end
pacman_spot
end
end