Create Anti-Maze

Learn how to convert the maze generator game into a different game called an anti-maze.

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.

  1. 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.

  1. Goto GenerateMaze.java.
  2. Update the rows and cols instance variables to create a grid with 5 rows and 5 columns.
  3. Update the text for the titleLabel instance variable to “Anti-Maze”.

Get hands-on with 1200+ tech skills courses.