×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Python Program to Create Maze Assignment Solution

July 03, 2024
Martin Jonas
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
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

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

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" +

" 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()

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.