maze-maker-2.py
'''Maze Maker.
commands:
www-pub - Generate mazes and solutions for /pub/python/maze
main - []
demo - []
test - []
Coder@Sonnack.com
September 16, 2014
'''
from sys import stdout, stderr, argv
from os import path
from logger import logger, info, debug, trace
from colors import *
from maze_obj import MazeObject
from maze_gif import MazeImage as MazeImageGIF
from maze_img import MazeImage as MazeImagePNG
from maze_xml import MazeXML
from maze_sol import MazeSolver
from maze_gen import MazeGen1
Log = logger('MazeMaker2')
def www_pub_python_maze (*args):
'''Generate multiple mazes (w/ solutions).'''
Log.debug('www-pub/arguments: %d' % len(args))
n_imgs = int(args[0]) if 0 < len(args) else 50
n_rows = int(args[1]) if 1 < len(args) else 40
n_cols = int(args[2]) if 2 < len(args) else 40
f_name = args[3] if 3 < len(args) else r'C:\CJS\www\root\pub\python\maze'
Log.debug('www-pub/images: %d' % n_imgs)
Log.debug('www-pub/dimensions: %dx%d' % (n_rows,n_cols))
Log.debug('www-pub/filename: %s' % f_name)
filelist = []
for ix in range(n_imgs):
Log.info(ix)
fn1 = path.join(f_path, 'maze%03d.png' % ix)
fn2 = path.join(f_path, 'maze%03d_solved.png' % ix)
filelist.append([fn1, fn2])
Log.info(fn1)
Log.info(fn2)
mz = MazeObject(n_rows, n_cols)
mg = MazeGen1(mz)
mg.generate_maze()
mi = MazeImagePNG(mz)
mi.draw_maze()
mi.save(fn1)
maze_path = []
ms = MazeSolver(mz)
ms.solve(maze_path)
mz.set_all_cells(MazeObject.EmptyCell)
for cell in maze_path:
mz.set_cell(cell[0], cell[1], MazeObject.PathMarker)
mz.set_entry_and_exit()
mz.print_maze(Log.fp())
mi.draw_maze()
mi.save(fn2)
return ((n_rows,n_cols), n_imgs, filelist)
def test (*args):
Log.debug('test/arguments: %d' % len(args))
n_rows = int(args[0]) if 0 < len(args) else 10
n_cols = int(args[1]) if 1 < len(args) else 16
Log.debug('test/dimensions: %dx%d' % (n_rows,n_cols))
mz = MazeObject(n_rows, n_cols)
mi = MazeImageGIF(mz)
mg = MazeGen1(mz)
mz.cell_syms = [' ', ' o ', ' A ', ' Z ', ' - ']
mg.generate_maze()
mz.print_maze(Log.fp())
mi.draw_maze()
mi.show()
mi.save('maze-test.gif')
return (mz, mi, mg)
def demo (*args):
Log.debug('demo/arguments: %d' % len(args))
n_rows = int(args[0]) if 0 < len(args) else 25
n_cols = int(args[1]) if 1 < len(args) else 40
Log.debug('demo/dimensions: %dx%d' % (n_rows,n_cols))
mz = MazeObject(n_rows, n_cols)
mi = MazeImagePNG(mz)
mx = MazeXML(mz)
ms = MazeSolver(mz)
mg = MazeGen1(mz)
mg.generate_maze()
mi.draw_maze()
mi.save('maze-demo.png')
a = []
ms.solve(a)
for t in a:
if t[0] or t[1]:
mz.set_cell(t[0], t[1], ixColorLightYellow)
mz.set_entry_and_exit()
mz.print_maze(Log.fp())
try:
mi.draw_maze()
except:
raise
mi.show()
mi.save('maze-demo-solved.png')
xml = mx.get_xml_paths()
fp = open('maze-demo.xml', 'w')
fp.write(xml)
fp.close()
return (mz, mi, mg)
def main (*args):
Log.debug('main/arguments: %d' % len(args))
n_rows = int(args[0]) if 0 < len(args) else 8
n_cols = int(args[1]) if 1 < len(args) else 8
f_name = args[2] if 2 < len(args) else 'maze.png'
Log.debug('main/dimensions: %dx%d' % (n_rows,n_cols))
Log.debug('main/filename: %s' % f_name)
mz = MazeObject(n_rows, n_cols)
mi = MazeImagePNG(mz)
mg = MazeGen1(mz)
mg.generate_maze()
mz.print_maze(Log.fp())
mi.draw_maze()
mi.show()
mi.save(f_name)
return (mz, mi, mg)
def dispatch (cmd, *args):
Log.debug('command: %s' % cmd)
Log.debug('arguments: %d' % len(args))
if cmd == 'www-pub': return www_pub_python_maze(*args)
if cmd == 'main': return main(*args)
if cmd == 'demo': return demo(*args)
if cmd == 'test': return test(*args)
return (None, cmd, args)
if __name__ == '__main__':
print 'autorun:',argv[0]
Log.start('maze-maker-2.log')
Log.level(info())
cmnd = argv[1] if 1 < len(argv) else 'main'
args = argv[2:]
try:
obj = dispatch(cmnd, *args)
Log.info()
Log.info('exit: %s' % type(obj))
except Exception as e:
print >> stderr, e
Log.error(e)
raise
finally:
Log.end()
'''eof'''