# Pacman – Pac-dots Randomiser

In this challenge, we are looking at using a 2D Array to create the maze used in a Pacman Game.

Our 2D array will contains different numerical values to represents the corridors, walls and the pac-dots:

So in our 2D-array a corridor is represented using a 0, a wall using a 1 and a pac-dot using a 2.

```maze = [[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,0,1,0,1,0,1,1,0,1],
[1,0,1,0,0,1,0,1,0,0,1,0,1],
[1,0,0,0,1,1,0,1,1,0,0,0,1],
[1,0,1,0,0,0,0,0,0,0,1,0,1],
[1,0,1,0,1,1,0,1,1,0,1,0,1],
[1,0,1,0,0,0,0,0,0,0,1,0,1],
[1,0,0,0,1,1,0,1,1,0,0,0,1],
[1,0,1,0,0,1,0,1,0,0,1,0,1],
[1,0,1,1,0,1,0,1,0,1,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1]]```

We can then easily add pac-dots in our maze using the following code:

```maze[3][3] = 2
maze[3][9] = 2
maze[9][3] = 2
maze[9][9] = 2```

We can then use a script to paint all the tiles of this maze on the screen. In this case we have done so using Python Turtle.

#### Python Code

Your task is to add a piece of Python code to add four new pac-dots on this maze. These pac-dots should be randomly placed in the maze but not over a wall or an existing pac-dot.

Here is the flowchart for this algorithm:

[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ display_to=”not_subscribed” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

main.pymaze.py

#### Python Code

```#Pac-dots randomiser! - www.101computing.net/pac-dots-randomiser

import turtle, random
from maze import *

pen = turtle.Turtle()
# 3. Set up your turtle
pen.speed(100)
pen.color(0,0,0)
wn = turtle.Screen()
wn.bgcolor(0,0,0)

ROWS = 13
COLS = 13
maze = [[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,0,1,0,1,0,1,1,0,1],
[1,0,1,0,0,1,0,1,0,0,1,0,1],
[1,0,0,0,1,1,0,1,1,0,0,0,1],
[1,0,1,0,0,0,0,0,0,0,1,0,1],
[1,0,1,0,1,1,0,1,1,0,1,0,1],
[1,0,1,0,0,0,0,0,0,0,1,0,1],
[1,0,0,0,1,1,0,1,1,0,0,0,1],
[1,0,1,0,0,1,0,1,0,0,1,0,1],
[1,0,1,1,0,1,0,1,0,1,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1]]

maze[3][3] = 2
maze[3][9] = 2
maze[9][3] = 2
maze[9][9] = 2

#Add some code here to add an extra 4 pac-dots, randomly positioned in the corridors of this mazes
for i in range(1,5):
row = random.randint(0,ROWS-1)
col = random.randint(0,COLS-1)
while maze[row][col]>0:
row = random.randint(0,ROWS-1)
col = random.randint(0,COLS-1)
maze[row][col]=2

#Draw the maze on screen (using Python Turtle!)
drawMaze(pen,maze)
```

#### Python Code

```def drawTile(pen,x,y,w,h,color):
pen.penup()
pen.goto(x,y)
pen.pendown()
pen.fillcolor(color)
pen.begin_fill()
pen.goto(x+w,y)
pen.goto(x+w,y+h)
pen.goto(x,y+h)
pen.goto(x,y)
pen.end_fill()
pen.penup()

pen.penup()
pen.pendown()
pen.fillcolor(color)
pen.begin_fill()
pen.end_fill()
pen.penup()

def drawMaze(pen,maze):
x = -195
y = 165
for row in range (0,len(maze)):
for col in range (0,len(maze[row])):
if maze[row][col]==1:
drawTile(pen,x,y,30,30,"#3311FF")
if maze[row][col]==2:
drawPacDot(pen,x,y,10,"#FFFFFF")
x =x + 30
x = -195
y = y - 30
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 26

No votes so far! Be the first to rate this post.

As you found this challenge interesting...