git.fiddlerwoaroof.com
aldus.py
a9acab05
 import numpy as np
 
 class Traveller(object):
 	@property
 	def x(self): return self.pos[0]
 	@x.setter
 	def x(self, val): self.pos = val, self.pos[1]
 
 	@property
 	def y(self): return self.pos[1]
 	@y.setter
 	def y(self, val): self.pos = self.pos[0], val
 
 	def __init__(self, starting_cell):
 		self.pos = starting_cell
 		self.connections = []
 		self.visited = {self.pos}
 
 	def random_walk(self, array):
 		maxx, maxy = array.shape
 		cval = array[self.pos]
 
 		dx, dy = 0,0
 		r = np.random.random()
 		if r < 0.25: # x-1
 			dx = -1
 		elif r < 0.5: # x+1
 			dy = 1
 		elif r < 0.75: # y-1
 			dx = 1
 		else: # y+1
 			dy = -1
 
 		opos = self.pos
 		self.x += dx
 		self.y += dy
 
 		if self.x < 0: self.x = 0
 		elif self.x >= maxx: self.x = maxx-1
 		if self.y < 0: self.y = 0
 		elif self.y >= maxy: self.y = maxy-1
 
 		self.visited.add(self.pos)
 
 		nval = array[self.pos]
 
 		if cval != nval: 
 			array[self.pos] = cval
 			self.connections.append((opos, self.pos))
 			print cval, nval
 
 
 x,y = 24,36
 maze = np.array(range(1,x*y+1))
 maze.shape = x,y
 
 t = Traveller((x/2, y/2))