Difference between revisions of "Maze.py"
From WLCS
Line 36: | Line 36: | ||
r2.roomToWest=r1 | r2.roomToWest=r1 | ||
− | + | r2.roomToWest=r1 | |
− | + | ||
− | |||
def getUnvisitedNeighbors(self, room): | 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): |
Revision as of 14:19, 16 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
elif r1.row == r2.row-1 and r1.col == r2.col:
# r1 is north of r2
r2.roomToNorth=r1
r1.roomToSouth=r2
elif r1.row == r2.row and r1.col == r2.col+1:
#r1 is east of r2
r2.roomToEast=r1
r1.roomToWest=r2
elif r1.row == r2.row and r1.col == r2.col-1:
# r1 is east of r2
r1.roomToEast=r2
r2.roomToWest=r1
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("|")