implemented parsing and printing of mazes

This commit is contained in:
Stephen McQuay 2012-10-25 22:05:38 -07:00
parent 612fb5f38e
commit 360668c4d3
2 changed files with 46 additions and 2 deletions

13
bin/p2.py Normal file
View File

@ -0,0 +1,13 @@
import sys
from maze import parse_maze, CS235Maze
m = parse_maze(sys.argv[1])
assert m[7][0][0] == 0
assert m[7][3][0] == 1
assert m[7][0][1] == 0
assert m[3][3][3] == 1
assert m[5][2][6] == 1
assert m[7][5][4] == 0
print CS235Maze(m)

View File

@ -1,6 +1,24 @@
import copy
import random
class CS235Maze(object):
def __init__(self, data, size=8):
self.data = data
self.size = size
def __str__(self):
r = derp_transpose(self.data)
rs = ''
for i in xrange(self.size):
for j in xrange(self.size):
for k in xrange(self.size):
rs += '{} '.format(r[i][j][k])
rs += '\n'
rs += '\n'
return rs
def make_maze(size=8):
r = [[[0] * size] * size] * size
for i in xrange(size):
@ -8,10 +26,23 @@ def make_maze(size=8):
r[i][j] = [random.randint(0, 1) for i in xrange(size)]
r[0][0][0] = 1
r[7][7][7] = 1
return r
return CS235Maze(r, size)
def parse_maze(filename):
f = open(filename, 'r')
t = f.read()
return [[int(j) for j in i.split()] for i in t.split('\n\n') if i]
soilded = [[[int(i) for i in j.split()]
for j in k.split('\n')]
for k in t.split('\n\n') if k]
cleaned = derp_transpose(soilded)
return cleaned
def derp_transpose(s, size=8):
r = copy.deepcopy(s)
for i in xrange(size):
for j in xrange(size):
for k in xrange(size):
r[i][j][k] = s[k][j][i]
return r