+1 (315) 557-6473 

Python Program to Create Maze Assignment Solution.


Instructions

Objective
Write a program to create maze in python.

Requirements and Specifications

program to create maze in python
program to create maze in python 1

Source Code

#!/usr/bin/env python

import random

import sys

def gen_maze(N, p):

'''

Randomly generate an embedded list with N sublists, each of which has N integer elements with values either 0 or 1. For example, if N = 4, the generated list named maze could be

[[0, 0, 1, 0],

[1 0, 0, 1],

[0, 1, 0, 0],

[1 0, 0, 0]].

he starting point is at maze[0][0], and the end point is at maze[N-1][N-1].

N: integer, dimension of the maze.

p: float, threshold used to compare with a random number.

'''

maze = [] # initialization the embedded list

# implement the gen function here (2 marks)

for i in range(N):

# Create a sublist of N elements with random 0 and 1

sublist = []

for j in range(N):

# Generate random number

x = random.random()

if x <= p:

x = 1

else:

x = 0

sublist.append(x)

# If the current index is zero, then set the elment (0,0) as 0

if i == 0:

sublist[0] = 0

elif i == N-1: # if the current index is N-1 then set the element (N,N) to 0

sublist[-1] = 0

maze.append(sublist)

return maze

def print_maze(maze, style='basic'):

"""

Print the maze on the screen (standard output).

maze: list of N sublists. Each sublist is of length N. maze is generated by function gen_maze.

style string from {'basic', 'xspace'}. If style=='basic', just print the raw list maze on the screen. If style == 'xspace', print letter O when seeing an element being 0, and print letter 'X' when seeing an element being 1; and the starting point and end point should be printed as '*'s.

"""

print('maze style is: ' + style)

# write your code below (2 marks)

if style == 'basic':

print(maze)

elif style == 'xspace':

N = len(maze)

for i in range(N):

row = maze[i]

for j in range(N):

if (i == 0 and j == 0) or (i == N-1 and j == N-1):

print('*', end = "")

else:

if maze[i][j] == 0:

print('O', end='')

else:

print('X', end='')

print()

print('Done!')

def usage():

print("maze_using_argv.py <N> <p> <maze_style>\n" +

" N: dimension of the maze, must be a number from 4 to 8.\n" +

" p: threshold, if a random number is <= p, assign 1 to the cell, otherwise 0.\n" +

" maze_style: {\"basic\", \"xspace\"} ")

def main():

# obtain the commandline parameters here from the sys.argv

# You need to make sure that N is an integer between 4 and 8. If N received from the commandline is <4, change N to 4. If N received from the commandline is > 8, change N to 8.

# You should also make sure that p is in range [0,1). If p received from the commandline is < 0, assign 0 to p. If p received from the commandline is >=1, assign 0.5 to p.

# You should also make sure that one string from {'basic','xspace'} should be assigned to maze_style. If other string is provided from the commandline, assign 'basic' to maze_style.

if len(sys.argv)<4:

usage()

sys.exit(0)

pass #(1 mark)

# do actual work here: generate a maze and print it on screen

N = int(sys.argv[1])

if N < 4:

N = 4

lif N > 8:

N = 8

p = float(sys.argv[2])

if p < 0:

p = 0

elif p >= 1:

p = 0.5

maze_style = sys.argv[3]

if not maze_style in ['basic', 'xspace']:

maze_style = 'basic'

# Generate maze

maze = gen_maze(N, p)

# Print maze

print_maze(maze, maze_style)

pass #(1 mark)

if __name__=='__main__':

main()