Create Anti-Maze
Learn how to convert the maze generator game into a different game called an anti-maze.
We'll cover the following
Converting maze to an anti-maze
This maze generator is now fairly complete. MazeGenerator can generate a maze and you can move a ball through the maze. However, by making a slight change, you can make MazeGenerator into a whole different game: an anti-maze. An anti-maze is a maze in which you can only cross lines to move from cell to cell. You cannot cross open areas.
- In the draw the walls section of
paintComponent()
, instead of drawing the line on the top or left if the top or left edge is a wall, draw the line if the top or left edge is not a wall.
...
public void paintComponent(Graphics g) {
...
// draw the walls
if (isWall(TOP)) {
g.drawLine(0, 0, SIZE, 0);
}
if (isWall(LEFT)) {
g.drawLine(0, 0, 0, SIZE);
}
...
On adding the above changes in your coding playground. Click the run button. The window should now show an anti-maze. The ball should only be allowed to move through walls, not through any open areas.
Make the anti-maze smaller
That is all you would have to change to make MazeGenerator draw an anti-maze. However, until you are more used to recognizing paths through an anti-maze, you might want to work with a smaller maze.
- Goto
GenerateMaze.java
. - Update the
rows
andcols
instance variables to create a grid with5
rows and5
columns. - Update the text for the titleLabel instance variable to “Anti-Maze”.
Get hands-on with 1200+ tech skills courses.