SlideShare a Scribd company logo
1) Introduction:
This practical work consists of developing a set of definitions
of functions which make it
possible to generate mazes and draw them using the turtle.
In this work, you have to imagine that you are the programmer
of a library that allows
you to generate labyrinths. You must write functions that will
be used by other
programmers to make specific drawings. (create a labyrinth of a
certain size).
The algorithm for generating mazes is imposed on you and is
described in the next
section. Your task is to code this algorithm, and the necessary
helper functions, in
Python using Turtle.
2) Labyrinths
The mazes you need to generate are based on a rectangular grid
like this:
This grid is made up of square cells. In this example there are 8
columns and 4 rows of
cells. For a given cell there are 4 walls that we will call the
North, East, South, and West
walls (to simplify  N ",  E",  S ",  O") of the cell. A labyrinth
has a certain width in
number of cells (NX), a certain height in number of cells (NY),
and a certain “step”(the
width and height of each square cell, in number of pixels). The
labyrinth is therefore
created by eliminating certain walls from the grid. The outer
wall of the grid is drilled at
the top left (the entrance to the labyrinth) and lower right (the
exit from the labyrinth).
The creation of a labyrinth consists of determining which walls
in each cell need to be
removed. For each cell it is necessary to eliminate at least one
of its 4 walls, and
possibly all 4. Here is a labyrinth using the previous grid (i.e.
with NX = 8, NY = 4, step =
40):
The choice of the walls to eliminate cannot be done completely
randomly because this
could generate a drawing. which is not a labyrinth, that is to say
a drawing which does
not follow a path from the entrance to the exit. The algorithm
you should use uses
random numbers to create the maze but ensures that there
always is exactly one path
between entering and exiting the maze. In fact, the algorithm
guarantees that there is
always exactly one path between any cell and any other cell
(what is formally called
an underlying tree). Before explaining the algorithm we must
first number each
horizontal wall and each vertical wall and give a coordinate (x,
y) to each cell as follows:
It should be noted that the coordinate system (x, y) of the cells
places (0,0) at the top
left (x grows towards the right, and grows down there). Also
notice that there are NX
(NY + 1) horizontal walls and (NX + 1) NY vertical walls. So
the horizontal walls are
numbered from 0 to (NX (NY + 1)) 1 and the vertical walls
from 0 to ((NX + 1) NY) 1.
The relation between the (x, y) coordinate of a cell and the
numbers of its walls N, E, S,
O is the next one :
N = x + y x NX
E = 1 + x + y  x (NX + 1)
S = x + (y + 1)  x NX
O = x + y  x (NX + 1)
Note that the wall number N can be used to uniquely identify a
cell with
only one number. For example in the grid above the cell in
position (5,2) has the number
21, that is to say 5 + 2 NX.
The algorithm uses sets of numbers (in the mathematical sense
of a set, i.e. a collection
of numbers without duplication). We can define wallsH as the
set of horizontal walls
(and respectively wallsv as the set of vertical walls) which have
not been removed by
the maze creation algorithm. The information contained in these
sets can be combined
with the values of NX and NY to draw a labyrinth by tracing a
horizontal line for each
horizontal wall whose number is always in wallsH and a vertical
line for each vertical
wall whose number is always in wallsv.
The algorithm considers that the grid is an initially full space
except for a randomly
chosen cell, which is empty (this is the initial cavity). At each
iteration of the algorithm a
new cell will be chosen randomly among all the cells
neighboring the cavity (but not
forming part of the cavity) and one of the walls (i.e. horizontal
or vertical) which
separates it from the cavity will be removed randomly to form a
larger cavity (i.e. from
the whole wallsH if it is a horizontal wall or of the wallsv
assembly if it is a vertical wall).
This process is repeated until all the cells of the grid are part of
the cavity.
The choice of the next cell to add to the cavity can be done
simply by keeping at all
times two other sets of numbers: cellar and front. These are sets
that contain cell
numbers. The cellar set is the set of cells that have been put into
the cavity by the
algorithm. The forehead set is the set of cells that are
neighboring cells in the cavity (but
not in the cavity). We can maintain and update these sets as new
cells that are selected
to add to the cavity. Indeed if we have add to the cavity the
cells with coordinates (x, y)
contained in the front set, we must add the neighboring cells
with coordinates (x, y)
horizontally and vertically (but not in the cellar assembly) a the
front assembly and
remove the cell (x, y) of the front set and add the cell (x, y) to
the cellar set. Note that
we must remove or add from these sets the number of cells.
The coding of this algorithm will be greatly simplified by the
writing of set manipulation
functions. These functions are described in the next section. Use
them judiciously to
code the algorithm labyrinth creation. We will use lists of
numbers to represent sets. For
example the table [9,2,5] represents the set which contains the
three elements 2, 5 and
9.
3) Specifications:
Function Iota (n):
This function takes a non-negative integer n as parameter and
returns an array of length
n containing in order the integer values from 0 to n 1
inclusive. For example :
iota(5) = [0,1,2,3,4]
Function contain(tab,x):
This function takes as parameters an array of numbers (tab) and
a number x and
returns a boolean indicating if x is contained in the array by
traversing this array with a
loop (not the right to use the keyword in).
For example :
contain([9,2,5], 2) = True
contain([9,2,5], 7) = False
Function add(tab,x):
This function takes an array of numbers (tab) and a number x as
parameters and
returns a new array with the same content as tab except that x is
appended to the end if
it is not already contained in tab. For example :
add([9,2,5], 2) = [9,2,5]
add([9,2,5], 7) = [9,2,5,7]
Function remove(tab,x):
This function takes an array of numbers (tab) and a number x as
parameters and
returns a new array with the same contents as tab except that x
is removed from the
array. For example :
remove([9,2,5], 2) = [9,5]
remove([9,2,5], 7) = [9,2,5]
Function neighbors(x,y,nx,ny):
This function takes the coordinate (x, y) of a cell and the size of
a grid (width = nx and
height = ny) and returns an array containing the number of
neighboring cells. For
example :
neighbors(7, 2, 8, 4) = [15,22,31]
Function laby(nx,ny, step):
This procedure creates a random maze (width = nx and height =
ny) and draws this
maze in the center of the drawing window using a grid with
cells of pixel pitch width and
height. For example, here are two laby calls and the drawings
obtained in each case:
20% bonus points for the implementation of the labySol
procedure (nx, ny, pas) which
draws a labyrinth exactly like the laby procedure but which in
addition draws in the
labyrinth, in red, the path
traversed by Pledge's algorithm to exit the labyrinth from the
top left entrance.

More Related Content

PPT
10 Recursion
PDF
Find the shortest route through a maze using linking and linked list.pdf
PPTX
13 recursion-120712074623-phpapp02
PPTX
10. Recursion
PDF
Maze Solver Robot using Arduino
PPT
computer notes - Data Structures - 31
DOCX
Illustrative problems for python programming- Unit 1.docx
PPTX
Rat_in_a_maze((Dynamic programming-tabulation))
10 Recursion
Find the shortest route through a maze using linking and linked list.pdf
13 recursion-120712074623-phpapp02
10. Recursion
Maze Solver Robot using Arduino
computer notes - Data Structures - 31
Illustrative problems for python programming- Unit 1.docx
Rat_in_a_maze((Dynamic programming-tabulation))

Similar to 1) IntroductionThis practical work consists of developing (20)

DOCX
Maze Solver - Rubric.xlsxSheet1Maze Solver - RubricStudent Nam.docx
PDF
Fun with Mazes - sitNL 2015
PDF
Write a C++ program which generates and displays a random walk acros.pdf
PPT
Computer notes - Maze Generator
PDF
E017142429
PDF
A Comprehensive and Comparative Study Of Maze-Solving Techniques by Implement...
PPTX
Rat_in_a_maze((Dynamic programming-tabulation))
PPT
08 recursive backtracking
PDF
Fun with Mazes
PPTX
Rat_in_Maze(Dynamic programming-tabulation)
DOCX
python_assignmentHanoi (1).py################################.docx
PPTX
HandyBug Robot
PPTX
Maze-Solving-Alhghgghgorithhggggggms.pptx
PPTX
Square seeds and round numbers 01 19 2015
PPTX
Square seeds and round numbers
PPT
An elementary navigation simulated in Java
PPTX
Problem-solving-with-Computational-Thinking-present.pptx
PPTX
Problem-solving-with-Computational-Thinking-present.pptx
PPTX
Ip 5 discrete mathematics
PDF
hello the code given is mostly complete but I need help with the Sol.pdf
Maze Solver - Rubric.xlsxSheet1Maze Solver - RubricStudent Nam.docx
Fun with Mazes - sitNL 2015
Write a C++ program which generates and displays a random walk acros.pdf
Computer notes - Maze Generator
E017142429
A Comprehensive and Comparative Study Of Maze-Solving Techniques by Implement...
Rat_in_a_maze((Dynamic programming-tabulation))
08 recursive backtracking
Fun with Mazes
Rat_in_Maze(Dynamic programming-tabulation)
python_assignmentHanoi (1).py################################.docx
HandyBug Robot
Maze-Solving-Alhghgghgorithhggggggms.pptx
Square seeds and round numbers 01 19 2015
Square seeds and round numbers
An elementary navigation simulated in Java
Problem-solving-with-Computational-Thinking-present.pptx
Problem-solving-with-Computational-Thinking-present.pptx
Ip 5 discrete mathematics
hello the code given is mostly complete but I need help with the Sol.pdf

More from MartineMccracken314 (20)

DOCX
1. Jack is the principal.  Mary is Jacks agent.  Mary enters into
DOCX
1. IntroversionScore 11 pts.4 - 22 pts.Feedback Some peop
DOCX
1. International financial investors are moving funds from Talona
DOCX
1. Interventionstreatment· The viral pinkeye does not need any
DOCX
1. Introduction and background information about solvatochromism u
DOCX
1. Integrity, the basic principle of healthcare leadership.Conta
DOCX
1. Information organized and placed in a logical sequence (10 po
DOCX
1. In our grant application, we included the following interventio
DOCX
1. I believe that the protagonist is Nel because she is the one th
DOCX
1. If the profit from the sale of x units of a product is P =
DOCX
1. How does CO2 and other greenhouse gases promote global warmin
DOCX
1. How do you think communication and the role of training address
DOCX
1. How brain meets its requirement for its energy in terms of well
DOCX
1. Give an introduction to contemporary Chinese art (Talk a little
DOCX
1. For this reaction essay is a brief written reaction to the read
DOCX
1. Find something to negotiate in your personal or professional li
DOCX
1. FAMILYMy 57 year old mother died after a short illness
DOCX
1. Explain the four characteristics of B-DNA structure Differenti
DOCX
1. examine three of the upstream impacts of mining. Which of these
DOCX
1. Examine Hofstedes model of national culture. Are all four dime
1. Jack is the principal.  Mary is Jacks agent.  Mary enters into
1. IntroversionScore 11 pts.4 - 22 pts.Feedback Some peop
1. International financial investors are moving funds from Talona
1. Interventionstreatment· The viral pinkeye does not need any
1. Introduction and background information about solvatochromism u
1. Integrity, the basic principle of healthcare leadership.Conta
1. Information organized and placed in a logical sequence (10 po
1. In our grant application, we included the following interventio
1. I believe that the protagonist is Nel because she is the one th
1. If the profit from the sale of x units of a product is P =
1. How does CO2 and other greenhouse gases promote global warmin
1. How do you think communication and the role of training address
1. How brain meets its requirement for its energy in terms of well
1. Give an introduction to contemporary Chinese art (Talk a little
1. For this reaction essay is a brief written reaction to the read
1. Find something to negotiate in your personal or professional li
1. FAMILYMy 57 year old mother died after a short illness
1. Explain the four characteristics of B-DNA structure Differenti
1. examine three of the upstream impacts of mining. Which of these
1. Examine Hofstedes model of national culture. Are all four dime

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Complications of Minimal Access Surgery at WLH
PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Complications of Minimal Access Surgery at WLH
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

1) IntroductionThis practical work consists of developing

  • 1. 1) Introduction: This practical work consists of developing a set of definitions of functions which make it possible to generate mazes and draw them using the turtle. In this work, you have to imagine that you are the programmer of a library that allows you to generate labyrinths. You must write functions that will be used by other programmers to make specific drawings. (create a labyrinth of a certain size). The algorithm for generating mazes is imposed on you and is described in the next section. Your task is to code this algorithm, and the necessary helper functions, in Python using Turtle. 2) Labyrinths The mazes you need to generate are based on a rectangular grid like this: This grid is made up of square cells. In this example there are 8 columns and 4 rows of cells. For a given cell there are 4 walls that we will call the North, East, South, and West walls (to simplify N ", E", S ", O") of the cell. A labyrinth has a certain width in number of cells (NX), a certain height in number of cells (NY), and a certain “step”(the width and height of each square cell, in number of pixels). The labyrinth is therefore
  • 2. created by eliminating certain walls from the grid. The outer wall of the grid is drilled at the top left (the entrance to the labyrinth) and lower right (the exit from the labyrinth). The creation of a labyrinth consists of determining which walls in each cell need to be removed. For each cell it is necessary to eliminate at least one of its 4 walls, and possibly all 4. Here is a labyrinth using the previous grid (i.e. with NX = 8, NY = 4, step = 40): The choice of the walls to eliminate cannot be done completely randomly because this could generate a drawing. which is not a labyrinth, that is to say a drawing which does not follow a path from the entrance to the exit. The algorithm you should use uses random numbers to create the maze but ensures that there always is exactly one path between entering and exiting the maze. In fact, the algorithm guarantees that there is always exactly one path between any cell and any other cell (what is formally called an underlying tree). Before explaining the algorithm we must first number each horizontal wall and each vertical wall and give a coordinate (x, y) to each cell as follows: It should be noted that the coordinate system (x, y) of the cells places (0,0) at the top
  • 3. left (x grows towards the right, and grows down there). Also notice that there are NX (NY + 1) horizontal walls and (NX + 1) NY vertical walls. So the horizontal walls are numbered from 0 to (NX (NY + 1)) 1 and the vertical walls from 0 to ((NX + 1) NY) 1. The relation between the (x, y) coordinate of a cell and the numbers of its walls N, E, S, O is the next one : N = x + y x NX E = 1 + x + y  x (NX + 1) S = x + (y + 1)  x NX O = x + y  x (NX + 1) Note that the wall number N can be used to uniquely identify a cell with only one number. For example in the grid above the cell in position (5,2) has the number 21, that is to say 5 + 2 NX. The algorithm uses sets of numbers (in the mathematical sense of a set, i.e. a collection of numbers without duplication). We can define wallsH as the set of horizontal walls (and respectively wallsv as the set of vertical walls) which have not been removed by the maze creation algorithm. The information contained in these sets can be combined with the values of NX and NY to draw a labyrinth by tracing a horizontal line for each
  • 4. horizontal wall whose number is always in wallsH and a vertical line for each vertical wall whose number is always in wallsv. The algorithm considers that the grid is an initially full space except for a randomly chosen cell, which is empty (this is the initial cavity). At each iteration of the algorithm a new cell will be chosen randomly among all the cells neighboring the cavity (but not forming part of the cavity) and one of the walls (i.e. horizontal or vertical) which separates it from the cavity will be removed randomly to form a larger cavity (i.e. from the whole wallsH if it is a horizontal wall or of the wallsv assembly if it is a vertical wall). This process is repeated until all the cells of the grid are part of the cavity. The choice of the next cell to add to the cavity can be done simply by keeping at all times two other sets of numbers: cellar and front. These are sets that contain cell numbers. The cellar set is the set of cells that have been put into the cavity by the algorithm. The forehead set is the set of cells that are neighboring cells in the cavity (but not in the cavity). We can maintain and update these sets as new cells that are selected to add to the cavity. Indeed if we have add to the cavity the cells with coordinates (x, y) contained in the front set, we must add the neighboring cells with coordinates (x, y) horizontally and vertically (but not in the cellar assembly) a the front assembly and remove the cell (x, y) of the front set and add the cell (x, y) to
  • 5. the cellar set. Note that we must remove or add from these sets the number of cells. The coding of this algorithm will be greatly simplified by the writing of set manipulation functions. These functions are described in the next section. Use them judiciously to code the algorithm labyrinth creation. We will use lists of numbers to represent sets. For example the table [9,2,5] represents the set which contains the three elements 2, 5 and 9. 3) Specifications: Function Iota (n): This function takes a non-negative integer n as parameter and returns an array of length n containing in order the integer values from 0 to n 1 inclusive. For example : iota(5) = [0,1,2,3,4] Function contain(tab,x): This function takes as parameters an array of numbers (tab) and a number x and returns a boolean indicating if x is contained in the array by traversing this array with a loop (not the right to use the keyword in). For example : contain([9,2,5], 2) = True contain([9,2,5], 7) = False
  • 6. Function add(tab,x): This function takes an array of numbers (tab) and a number x as parameters and returns a new array with the same content as tab except that x is appended to the end if it is not already contained in tab. For example : add([9,2,5], 2) = [9,2,5] add([9,2,5], 7) = [9,2,5,7] Function remove(tab,x): This function takes an array of numbers (tab) and a number x as parameters and returns a new array with the same contents as tab except that x is removed from the array. For example : remove([9,2,5], 2) = [9,5] remove([9,2,5], 7) = [9,2,5] Function neighbors(x,y,nx,ny): This function takes the coordinate (x, y) of a cell and the size of a grid (width = nx and height = ny) and returns an array containing the number of neighboring cells. For example : neighbors(7, 2, 8, 4) = [15,22,31] Function laby(nx,ny, step):
  • 7. This procedure creates a random maze (width = nx and height = ny) and draws this maze in the center of the drawing window using a grid with cells of pixel pitch width and height. For example, here are two laby calls and the drawings obtained in each case: 20% bonus points for the implementation of the labySol procedure (nx, ny, pas) which draws a labyrinth exactly like the laby procedure but which in addition draws in the labyrinth, in red, the path traversed by Pledge's algorithm to exit the labyrinth from the top left entrance.