Example #1
0
  private void knockDownRandomWalls() {
    int ln = (int) (Math.random() * (Math.sqrt(rows * columns)));
    for (int i = 0; i < ln; i++) {
      int randI = (int) (Math.random() * rows), randJ = (int) (Math.random() * columns);

      try {
        Thread.sleep(delay * delay);
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      if (theGrid[randI][randJ].getWalledSet().size() > 1) {
        if (theGrid[randI][randJ].getColor() == Color.RED) {
          continue;
        }

        theGrid[randI][randJ].setColor(Color.RED);
        repaint();

        GridVertex target = theGrid[randI][randJ].getWalledSet().iterator().next();
        if (target.getWalledSet().size() > 1) {
          theGrid[randI][randJ].knockDownWall(target);
          target.setColor(Color.RED);
        } else {
          theGrid[randI][randJ].setColor(Color.WHITE);
        }
      }
      repaint();
    }
  }
Example #2
0
  private void alduosBroderAlgo() {
    makeGrid();
    repaint();

    int numOfVertices = rows * columns;
    origin.visit();
    int visited = 1;
    int visits = origin.getVisits();

    GridVertex current = origin;
    current.setColor(green);

    while (visited < numOfVertices) {
      repaint();

      GridVertex next = current.randomNeighbor();

      if (next.getVisits() == visits - 1) {
        next.visit();
        visited++;
        current.knockDownWall(next);
      }
      current.setColor(Color.WHITE);
      next.setColor(green);
      current = next;
    }
    current.setColor(Color.WHITE);
    repaint();
  }
Example #3
0
  private void huntAndKillAlgo() {
    repaint();

    Set<GridVertex> unVisited = getVertexSet();
    unVisited.remove(origin);

    GridVertex current = origin; // TODO

    current.visit();
    current.setColor(Color.RED);
    int visits = current.getVisits();

    while (unVisited.size() > 0) {
      Set<GridVertex> tempSet = current.getUnVisitedNeighborSet();
      tempSet.retainAll(current.getWalledSet());

      repaint();

      if (tempSet.isEmpty()) {
        current.setColor(Color.WHITE);

        // We hunt for the new current
        boolean found = false;
        while (!found) {
          int i = (int) (Math.random() * rows);
          int j = (int) (Math.random() * columns);

          found = theGrid[i][j].getVisits() < visits;
          Set<GridVertex> temp = theGrid[i][j].getNeighborSet();
          temp.removeAll(unVisited);
          found &= temp.size() > 0;

          if (found) {
            current = theGrid[i][j];
            current.knockDownWall(temp.iterator().next());
            unVisited.remove(current);
            current.visit();
          }
        }
        current.setColor(green);
      } else {
        GridVertex chosen = tempSet.iterator().next();
        current.knockDownWall(chosen);

        current.setColor(Color.WHITE);
        unVisited.remove(chosen);
        current = chosen;
        current.visit();

        current.setColor(green);
      }
    }

    current.setColor(Color.WHITE);

    System.out.println("Generated");
    component.repaint();
  }
Example #4
0
  private void backtrackerAlgo() {
    // TODO random vertex method in the Grid Class
    GridVertex[][] grid = makeGrid();
    repaint();

    Set<GridVertex> unVisited = getVertexSet();
    unVisited.remove(origin);

    int randI = (int) (Math.random() * rows), randJ = (int) (Math.random() * columns);
    GridVertex current = grid[randI][randJ];
    Stack<GridVertex> stack = new Stack<GridVertex>();

    current.visit();
    current.setColor(Color.RED);

    while (!unVisited.isEmpty()) {
      Set<GridVertex> tempSet = current.getUnVisitedNeighborSet();
      tempSet.retainAll(current.getWalledSet());

      repaint();

      if (tempSet.isEmpty()) {
        current.setColor(Color.WHITE);

        if (stack.isEmpty()) {
          current = unVisited.iterator().next();
          current.visit();
          unVisited.remove(current);
        } else {
          current = stack.pop();
        }
        current.setColor(green);
      } else {
        stack.push(current);

        GridVertex chosen = tempSet.iterator().next();
        current.knockDownWall(chosen);

        unVisited.remove(chosen);
        current = chosen;
        current.visit();

        current.setColor(green);
      }
    }

    current.setColor(Color.WHITE);

    while (stack.size() > 0) {
      stack.pop().setColor(Color.WHITE);
      repaint();
    }

    System.out.println("Generated");
    component.repaint();
  }
Example #5
0
  public void solve() {
    if (end == null) { //
      return;
    }

    solved = !solved;

    GridVertex current = origin;
    Deque<GridVertex> deque = new LinkedList<GridVertex>();
    current.setColor(green);
    current.visit();

    while (current != end) {
      if (solved) {
        repaint();
      }
      Set<GridVertex> tempSet = current.getUnVisitedNeighborSet();
      tempSet.removeAll(current.getWalledSet());

      if (tempSet.isEmpty()) {
        current.setColor(Color.WHITE);

        try {
          current = deque.pop();
        } catch (NoSuchElementException e) {
          for (int i = 0; i < 40; i++) {
            System.out.print("*");
          }
          System.out.println("\nNo solution exists.");
          for (int i = 0; i < 40; i++) {
            System.out.print("*");
          }
          return;
        }
      } else {
        deque.push(current);

        GridVertex chosen = tempSet.iterator().next();
        current = chosen;
        current.visit();
      }
      if (solved) {
        current.setColor(green);
      }
    }

    while (!deque.isEmpty()) {
      deque.removeLast().setColor(solved ? Color.ORANGE : Color.WHITE);
      repaint();
    }
    end.setColor(solved ? Color.ORANGE : Color.WHITE);
    component.repaint();
    System.out.println("Solved!");
  }
Example #6
0
  private void primsAlgo() {
    GridVertex[][] grid = makeGrid();
    repaint();

    Set<GridVertex> maze = new HashSet<GridVertex>();

    Set<GridVertex> frontier = new HashSet<GridVertex>();

    int randI = (int) (Math.random() * rows), randJ = (int) (Math.random() * columns);
    GridVertex current = grid[randI][randJ];

    maze.add(current);

    frontier.addAll(current.getNeighborSet());
    current.setColor(Color.WHITE);

    // We color the frontier
    for (GridVertex v : frontier) {
      v.setColor(Color.CYAN);
    }
    repaint();
    while (!frontier.isEmpty()) {
      // repaint();

      // A random cell in the frontier which is not part of the maze yet.
      current = frontier.iterator().next(); // TODO Make this more random.
      current.setColor(Color.RED);
      repaint();

      Set<GridVertex> temp = current.getNeighborSet();
      temp.retainAll(maze);

      // Random cell in maze that is adjacent to current.
      GridVertex random = temp.iterator().next();
      current.knockDownWall(random);

      frontier.remove(current);
      maze.add(current);
      current.setColor(Color.WHITE);

      // Reset temp
      temp = current.getNeighborSet();
      temp.removeAll(maze);
      for (GridVertex v : temp) {
        v.setColor(Color.CYAN);
      }

      frontier.addAll(temp);
    }
  }
Example #7
0
  private void recursiveDivisionAlgo(int top, int bottom, int left, int right, double texture) {
    if (bottom - top < 1 || right - left < 1) {
      return;
    }

    // The following Math.random() conditions slightly vary the texture of
    // the maze.

    int height = bottom - top, width = right - left;
    boolean verticalLine = width > height;

    // Math.random() < (texture)/Math.log(1 + width * height)
    if (Math.random() < Math.pow(texture, Math.cbrt(width * height))) {
      verticalLine = !verticalLine;
    }

    if (height < 3) {
      verticalLine = Math.random() > texture;
    } else if (width < 3) {
      verticalLine = Math.random() < texture;
    }

    if (!verticalLine) {
      // Note: bottom > top because the origin is in the top left.
      int randRow = top + (int) (Math.random() * (bottom - top));
      int skipColumn = left + (int) (Math.random() * (right - left));

      for (int i = top; i <= bottom; i++) {
        for (int j = left; j <= right; j++) {
          theGrid[i][j].setColor(green);
        }
      }
      repaint();

      for (int i = left; i <= right; i++) {
        if (i != skipColumn) {
          theGrid[randRow][i].raiseWall(theGrid[randRow + 1][i]);
          repaint();
        }
      }

      repaint();

      for (int i = top; i <= bottom; i++) {
        for (int j = left; j <= right; j++) {
          theGrid[i][j].setColor(Color.WHITE);
        }
      }
      // repaint();

      recursiveDivisionAlgo(top, randRow, left, right, texture);
      recursiveDivisionAlgo(randRow + 1, bottom, left, right, texture);

    } else if (right - left > 0) {

      int randColumn = left + (int) (Math.random() * (right - left));
      int skipRow = top + (int) (Math.random() * (bottom - top));

      for (int i = top; i <= bottom; i++) {
        for (int j = left; j <= right; j++) {
          theGrid[i][j].setColor(green);
        }
      }
      repaint();

      for (int i = top; i <= bottom; i++) {
        if (i != skipRow) {
          theGrid[i][randColumn].raiseWall(theGrid[i][randColumn + 1]);
          repaint();
        }
      }

      repaint();

      for (int i = top; i <= bottom; i++) {
        for (int j = left; j <= right; j++) {
          theGrid[i][j].setColor(Color.WHITE);
        }
      }
      // repaint();

      recursiveDivisionAlgo(top, bottom, left, randColumn, texture);
      recursiveDivisionAlgo(top, bottom, randColumn + 1, right, texture);
    }
  }
Example #8
0
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == start2Button) {
      maze.sol = 2;
      timer1.start();
    }
    if (e.getSource() == resetButton) {
      timer1.stop();
      timer2.stop();
      timer3.stop();
      timer4.stop();
      maze.sol = 0;
      this.maze.readFromFile("maze.txt");
      maze.repaint();
    }
    if (e.getSource() == start1Button) {
      maze.sol = 1;
      timer2.start();
      // maze.leastVisitedNeighbour();
    }
    if (e.getSource() == start3Button) {
      maze.sol = 3;
      timer3.start();
    }
    if (e.getSource() == start4Button) {
      maze.sol = 4;
      maze.dijkstra();
      timer4.start();
    }
    if (maze.sol == 2) {
      if (!maze.cells[maze.currentX][maze.currentY].isEnd()) {
        maze.stepLeastVistedNeighbour();
        maze.repaint();
      } else {
        timer1.stop();
      }
    }

    if (maze.sol == 1) {
      if (!maze.cells[maze.currentX][maze.currentY].isEnd()) {
        maze.randomStep();
        maze.repaint();
      } else {
        timer2.stop();
      }
    }
    if (maze.sol == 3) {
      if (!maze.cells[maze.currentX][maze.currentY].isEnd()) {
        maze.stepRightHand();
        maze.repaint();
      } else {
        timer3.stop();
      }
    }
    if (maze.sol == 4) {
      if (!maze.cells[maze.currentX][maze.currentY].isEnd()) {
        maze.stepDjikstra();
        maze.repaint();
      } else {
        timer4.stop();
      }
    }
  }