Difference between revisions of "Maze.py"
From WLCS
(Created page with "<syntaxhighlight lang="Python"> from MazeRoom import * import random class Maze: numRows = 10 numCols = 10 rooms = [] stack = [] def __init__(self, heig...") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
numCols = 10 | numCols = 10 | ||
rooms = [] | rooms = [] | ||
− | |||
def __init__(self, height=10, width=10): | def __init__(self, height=10, width=10): | ||
Line 20: | Line 19: | ||
def connectRooms(self, r1, r2): | def connectRooms(self, r1, r2): | ||
− | + | # r1 is south of r2 | |
if r1.row == r2.row+1 and r1.col == r2.col: | if r1.row == r2.row+1 and r1.col == r2.col: | ||
r1.roomToNorth=r2 | r1.roomToNorth=r2 | ||
r2.roomToSouth=r1 | r2.roomToSouth=r1 | ||
+ | # r1 is north of r2 | ||
elif r1.row == r2.row-1 and r1.col == r2.col: | elif r1.row == r2.row-1 and r1.col == r2.col: | ||
− | |||
r2.roomToNorth=r1 | r2.roomToNorth=r1 | ||
r1.roomToSouth=r2 | r1.roomToSouth=r2 | ||
+ | #r1 is east of r2 | ||
elif r1.row == r2.row and r1.col == r2.col+1: | elif r1.row == r2.row and r1.col == r2.col+1: | ||
− | |||
r2.roomToEast=r1 | r2.roomToEast=r1 | ||
r1.roomToWest=r2 | r1.roomToWest=r2 | ||
+ | # r1 is west of r2 | ||
elif r1.row == r2.row and r1.col == r2.col-1: | elif r1.row == r2.row and r1.col == r2.col-1: | ||
− | |||
r1.roomToEast=r2 | r1.roomToEast=r2 | ||
r2.roomToWest=r1 | r2.roomToWest=r1 | ||
− | def | + | def getUnvisitedNeighbors(self, room): |
− | # | + | neighbors = [] |
+ | r = room.row | ||
+ | c = room.col | ||
+ | |||
+ | #check if there is a northern room and if it has been visited | ||
+ | if r-1 >= 0 and r-1 < self.numRows and self.rooms[r-1][c].visited == False: | ||
+ | neighbors.append(self.rooms[r-1][c]) | ||
+ | |||
+ | #check if there is a southern room and if it has been visited | ||
+ | if r+1 >= 0 and r+1 < self.numRows and self.rooms[r+1][c].visited == False: | ||
+ | neighbors.append(self.rooms[r+1][c]) | ||
+ | |||
+ | #check if there is an eastern room and if it has been visited | ||
+ | if c+1 >= 0 and c+1 < self.numCols and self.rooms[r][c+1].visited == False: | ||
+ | neighbors.append(self.rooms[r][c+1]) | ||
− | + | #check if there is an western room and if it has been visited | |
− | + | if c-1 >= 0 and c-1 < self.numCols and self.rooms[r][c-1].visited == False: | |
+ | neighbors.append(self.rooms[r][c-1]) | ||
+ | return neighbors | ||
def countUnvisited(self): | def countUnvisited(self): |
Latest revision as of 11:19, 20 May 2013
from MazeRoom import *
import random
class Maze:
numRows = 10
numCols = 10
rooms = []
def __init__(self, height=10, width=10):
self.numRows = height
self.numCols = width
for r in range(self.numRows):
roomRow = []
for c in range(self.numCols):
roomRow.append(MazeRoom(r, c))
self.rooms.append(roomRow)
def connectRooms(self, r1, r2):
# r1 is south of r2
if r1.row == r2.row+1 and r1.col == r2.col:
r1.roomToNorth=r2
r2.roomToSouth=r1
# r1 is north of r2
elif r1.row == r2.row-1 and r1.col == r2.col:
r2.roomToNorth=r1
r1.roomToSouth=r2
#r1 is east of r2
elif r1.row == r2.row and r1.col == r2.col+1:
r2.roomToEast=r1
r1.roomToWest=r2
# r1 is west of r2
elif r1.row == r2.row and r1.col == r2.col-1:
r1.roomToEast=r2
r2.roomToWest=r1
def getUnvisitedNeighbors(self, room):
neighbors = []
r = room.row
c = room.col
#check if there is a northern room and if it has been visited
if r-1 >= 0 and r-1 < self.numRows and self.rooms[r-1][c].visited == False:
neighbors.append(self.rooms[r-1][c])
#check if there is a southern room and if it has been visited
if r+1 >= 0 and r+1 < self.numRows and self.rooms[r+1][c].visited == False:
neighbors.append(self.rooms[r+1][c])
#check if there is an eastern room and if it has been visited
if c+1 >= 0 and c+1 < self.numCols and self.rooms[r][c+1].visited == False:
neighbors.append(self.rooms[r][c+1])
#check if there is an western room and if it has been visited
if c-1 >= 0 and c-1 < self.numCols and self.rooms[r][c-1].visited == False:
neighbors.append(self.rooms[r][c-1])
return neighbors
def countUnvisited(self):
# COMPLETE THE CODE
def generate(self):
# COMPLETE THE CODE
def print(self):
print(" _"*self.numCols)
for roomRow in self.rooms:
for room in roomRow:
if room.roomToWest == None:
print("|", end="")
else:
print(" ", end="")
if room.roomToSouth == None:
print("_", end="")
else:
print(" ", end="")
if room.col == self.numCols-1:
print("|")