-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamelogic.py
158 lines (157 loc) · 4.65 KB
/
gamelogic.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
import random
cols=40
rows=35
class Cell():
def __init__(self,row,col):
self.col=col
self.row=row
self.walls=[True,True,True,True]
self.visited=False
def getRandomNeighbor(self,grid):
neighbors=[]
top,right,bottom,left=None,None,None,None
if index(self.row,self.col-1):
left=grid[index(self.row,self.col-1)]
if index(self.row+1,self.col):
bottom= grid[index(self.row+1,self.col)]
if index(self.row,self.col+1):
right= grid[index(self.row,self.col+1)]
if index(self.row-1,self.col):
top= grid[index(self.row-1,self.col)]
if top is not None and not top.visited:
neighbors.append(top)
if right is not None and not right.visited:
neighbors.append(right)
if bottom is not None and not bottom.visited:
neighbors.append(bottom)
if left is not None and not left.visited:
neighbors.append(left)
if len(neighbors)>0:
return neighbors[random.randint(0,len(neighbors)-1)]
else:
return None
def getRandomNeighborForSolve(self,grid):
neighbors=[]
top,right,bottom,left=None,None,None,None
if index(self.row,self.col-1):
left=grid[index(self.row,self.col-1)]
if index(self.row+1,self.col):
bottom= grid[index(self.row+1,self.col)]
if index(self.row,self.col+1):
right= grid[index(self.row,self.col+1)]
if index(self.row-1,self.col):
top= grid[index(self.row-1,self.col)]
if top is not None and not top.visited and not self.walls[0]:
neighbors.append(top)
if right is not None and not right.visited and not self.walls[1]:
neighbors.append(right)
if bottom is not None and not bottom.visited and not self.walls[2]:
neighbors.append(bottom)
if left is not None and not left.visited and not self.walls[3]:
neighbors.append(left)
if len(neighbors)>0:
return neighbors[random.randint(0,len(neighbors)-1)]
else:
return None
def index(r,c):
if r<0 or c<0 or r>rows-1 or c>cols-1:
return False
else:
return c + r * cols
def removeWall(current,neighbor):
if current.row < neighbor.row:
current.walls[2]=False
neighbor.walls[0]=False
elif current.row > neighbor.row:
current.walls[0]=False
neighbor.walls[2]=False
if current.col > neighbor.col:
current.walls[3]=False
neighbor.walls[1]=False
elif current.col < neighbor.col:
current.walls[1]=False
neighbor.walls[3]=False
def generateMaze():
grid=[]
for r in range(0,rows):
for c in range(0,cols):
grid.append(Cell(r,c))
stack=[]
current=grid[0]
stack.append(current)
while len(stack)>0:
current.visited=True
neighbor=current.getRandomNeighbor(grid)
if neighbor:
neighbor.visited=True
removeWall(current,neighbor)
stack.append(neighbor)
current=neighbor
else:
current=stack.pop()
return grid
def generateMazeSolution(maze,difficulty="easy"):
for cell in maze:
cell.visited=False;
reachedEndOfMaze=False;
steps=[]
stack=[]
current=maze[0]
hasPassedStepThreshold=False
addStep=True
#steps.append(current)
while not reachedEndOfMaze:
current.visited=True
if addStep:
steps.append(current)
addStep=True
neighbor=current.getRandomNeighborForSolve(maze)
if neighbor:
if neighbor.row ==rows-1 and neighbor.col==cols-1:
steps.append(neighbor)
reachedEndOfMaze=True
return steps
neighbor.visited=True
stack.append(current)
current=neighbor
else:
current=stack.pop()
if difficulty=="easy":
#steps.append(current)
pass
elif difficulty=="hard":
addStep=False;
steps.pop()
elif difficulty=="medium" and hasPassedStepThreshold:
addStep=False;
steps.pop()
if len(steps)>250:
hasPassedStepThreshold=True
return steps
def updatePlayer(player,direction,maze):
currentRow=player.row
currentCol=player.col
if direction == "top":
if not maze[index(currentRow-1,currentCol)].walls[2] and not maze[index(currentRow,currentCol)].walls[0]:
player.row=player.row-1
elif player.isAbleToPhase and player.row-1>=0:
player.row=player.row-1
player.isAbleToPhase=False
elif direction== "bottom":
if not maze[index(currentRow+1,currentCol)].walls[0] and not maze[index(currentRow,currentCol)].walls[2]:
player.row=player.row+1
elif player.isAbleToPhase and player.row+1<rows:
player.row=player.row+1
player.isAbleToPhase=False
elif direction=="left":
if not maze[index(currentRow,currentCol-1)].walls[1] and not maze[index(currentRow,currentCol)].walls[3]:
player.col=player.col-1
elif player.isAbleToPhase and player.col-1>=0:
player.col=player.col-1
player.isAbleToPhase=False
elif direction=="right":
if not maze[index(currentRow,currentCol+1)].walls[3] and not maze[index(currentRow,currentCol)].walls[1]:
player.col=player.col+1
elif player.isAbleToPhase and player.col+1<cols:
player.col=player.col+1
player.isAbleToPhase=False