-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rb
83 lines (55 loc) · 2.05 KB
/
tests.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
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
#Find the shortest path using BFS and the adjacency_list which starts from the initial
#coordenate
def shortest_path(adja_list, start, end_square)
visited = [start]
parent = {}
parent[start] = nil
queue = [start]
until queue.length == 0
current_node = queue.shift
if current_node == end_square
path = []
while current_node != nil
path.unshift(current_node)
current_node = parent[current_node]
end
return path
end
current_node.neighbor_squares.each do |neighbor|
unless visited.include?(neighbor)
visited.push(neighbor)
parent[neighbor] = current_node
queue.push(neighbor)
end
end
end
return nil
end
def adjacent_coordinates (coordinate)
adjacent_array = []
(-1..1).each do |x|
(-1..1).each do |y|
#These conditionals limit the size of the board
unless (coordinate[0] + x ) < 0 || coordinate[1] + y < 0 || (coordinate[0] + x ) > 7 || coordinate[1] + y > 7 || (x == 0 && y == 0)
adjacent_array << [coordinate[0] + x, coordinate[1]+ y]
end
end
end
return adjacent_array
end
#ADJACENT COORDINATES FOR A HORSE
def adjacent_moves_horse (coord)
adjacent_coordinates = []
j = 0
[-2, -1, 1, 2].each do |i|
j = 2 if i.abs == 1
j = 1 if i.abs == 2
unless coord[0] + i > 7 ||coord[0] + i < 0 || coord[1] + j > 7
adjacent_coordinates << [coord[0] + i ,coord[1] + j]
end
unless coord[0] + i > 7 ||coord[0] + i < 0 || coord[1] - j < 0
adjacent_coordinates << [coord[0] + i ,coord[1] - j]
end
end
return adjacent_coordinates
end