/
gameboard.py
176 lines (136 loc) · 4.98 KB
/
gameboard.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from numpy import arange, array, empty, ndenumerate, vectorize
from graph_tool.all import *
from queue import Queue
numrounds = 20
class BoardSpot(object):
occupied = False
vertex = None
def __init__(self, x=0,y=0):
self.x = x
self.y = y
class GameBoard(object):
width = 25
height = 25
graphMain = Graph(directed=False)
color = graphMain.new_vertex_property("string")
pos = graphMain.new_vertex_property("vector<double>")
def __init__(self):
totalSpots = self.width*self.height
self._boardSpots = arange(totalSpots).reshape(self.width, self.height)
self.lattice = array( [ [BoardSpot(i,j) for i in range(self.height)] for j in range(self.width) ],
dtype=object)
self.connectSpots()
def connectSpots(self):
width, height = self.lattice.shape
for (row, col), val in ndenumerate(self.lattice):
self.lattice[row][col].vertex = self.graphMain.add_vertex()
for (row, col), val in ndenumerate(self.lattice):
neighbors = self.find_neighbors(row,col)
for neighbor in neighbors:
if neighbor.vertex is not None:
self.graphMain.add_edge(self.lattice[row][col].vertex, neighbor.vertex)
def printSpots(self):
for row in self.lattice:
for column in row:
print(column,end=",")
print()
def reset_colors(self):
width, height = self.lattice.shape
for (row, col), val in ndenumerate(self.lattice):
target = self.lattice[row][col]
self.color[target.vertex] = "green"
self.pos[target.vertex] = (target.x, target.y)
# Draw walls
startingDiagonal=12
squareSize=6
wallPositions = (
(3,18),(15,2),(12,1),
)
for x,y in wallPositions:
for row in range(y,y+squareSize):
for col in range(x,x+squareSize):
target = self.lattice[row][col]
target.occupied = True
self.color[target.vertex] = "red"
def find_neighbors(self, targetRow, targetCol):
spotList = []
width, height = self.lattice.shape
for row in range(targetRow-1, targetRow+2):
for col in range(targetCol-1, targetCol+2):
valid = True
finalRow = row
finalCol = col
if row < 0:
finalRow = height-1
valid = False
if row > height-1:
finalRow = 0
valid = False
if row == targetRow and col == targetCol:
valid = False
if col < 0:
finalCol = width-1
valid = False
if col > width-1:
finalCol = 0
valid = False
if valid and self.lattice[row][col].occupied is True:
valid = False
if valid:
spotList.append( self.lattice[finalRow][finalCol] )
return spotList
def graphSnapshot(myGameBoard,picCnt,fileName):
result = graph_draw(
myGameBoard.graphMain,
# vertex_text=myGameBoard.graphMain.vertex_index,
vertex_font_size=4,
output_size=(600, 600),
vertex_size=6,
vertex_color=myGameBoard.color,
vertex_fill_color=myGameBoard.color,
pos=myGameBoard.pos,
#Remove output parameter for interactive window
output=fileName+format(picCnt, '02d')+".png"
)
print(result)
return picCnt + 1
myGameBoard = GameBoard()
myQueue = Queue()
came_from = {}
frontier = Queue()
startIdx = 5
start = myGameBoard.lattice[startIdx][startIdx]
goal = myGameBoard.lattice[startIdx][startIdx+17]
frontier.put( start )
picCnt=0
myGameBoard.reset_colors()
fileNameMain="board"
fileNameFrontier="frontier"
curRound = 0
while curRound < numrounds and goal not in came_from:
myGameBoard.reset_colors()
nextFrontier = Queue()
while not frontier.empty():
target = frontier.get()
row = target.y
col = target.x
for neighbor in myGameBoard.find_neighbors(row,col):
if neighbor not in came_from:
came_from[neighbor] = target
nextFrontier.put(neighbor)
for spot in came_from:
myGameBoard.color[spot.vertex] = "grey"
while not nextFrontier.empty():
transitionSpot = nextFrontier.get()
myGameBoard.color[transitionSpot.vertex] = "blue"
frontier.put(transitionSpot)
picCnt = graphSnapshot(myGameBoard,picCnt,fileNameFrontier)
curRound+=1
current = goal
path = [current]
while current != start:
current = came_from[current]
path.append(current)
for spot in path:
myGameBoard.color[spot.vertex] = "orange"
picCnt = graphSnapshot(myGameBoard,picCnt,fileNameFrontier)