# Python Program to Create Maze Assignment Solution

July 03, 2024
Martin Jonas
🇦🇺 Australia
Python
Dr. Martin Jonas, PhD in Computer Science from Southern Cross University, Australia. With 4 years of experience in Python assignments, I offer expert guidance and support to help you excel in your programming projects.
Key Topics
• Instructions
• Requirements and Specifications
Tip of the day
News

## Instructions

Objective
Write a python assignment to create a maze as a sample work. This assignment challenges you to design a Python program that generates a maze using various algorithms. The goal is to develop a program that constructs a maze grid, implements maze generation techniques like Prim's or Kruskal's algorithm, and provides a visualization of the maze using libraries like Pygame or Matplotlib. Your code should demonstrate an understanding of data structures, graph theory, and Python programming. This assignment not only tests your coding skills but also your ability to think critically and creatively in solving problems.

## Requirements and Specifications

Source Code

`#!/usr/bin/env pythonimport randomimport sysdef 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 1sublist = []for j in range(N):# Generate random numberx = random.random()if x <= p:x = 1else:x = 0sublist.append(x)# If the current index is zero, then set the elment (0,0) as 0if i == 0:sublist[0] = 0elif i == N-1: # if the current index is N-1 then set the element (N,N) to 0sublist[-1] = 0maze.append(sublist)return mazedef 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" +" 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 screenN = int(sys.argv[1])if N < 4:N = 4lif N > 8:N = 8p = float(sys.argv[2])if p < 0:p = 0elif p >= 1:p = 0.5maze_style = sys.argv[3]if not maze_style in ['basic', 'xspace']:maze_style = 'basic'# Generate mazemaze = gen_maze(N, p)# Print mazeprint_maze(maze, maze_style)pass #(1 mark)if __name__=='__main__':main()`

## Related Samples

Explore our Python Assignment Samples for comprehensive solutions to coding challenges. These examples cover topics from basic syntax to advanced data structures and algorithms, offering clear explanations and step-by-step implementations. Ideal for students seeking practical insights to excel in Python programming assignments and deepen their coding proficiency effectively.