Ejemplo n.º 1
0
 public void update(List<Point> ghostPositions) {
   for (int i = 0; i < ghosts.size(); ++i) {
     Ghost ghost = ghosts.get(i);
     Cell currentCell = getCell(ghost.getPosition());
     Cell newCell = getCell(ghostPositions.get(i));
     if (!currentCell.equals(newCell)) {
       grid.get(currentCell.row).get(currentCell.column).remove(ghost);
       grid.get(newCell.row).get(newCell.column).add(ghost);
     }
   }
 }
Ejemplo n.º 2
0
 public int otherStringsEndInTheSameColumn(Cell cell) {
   if (!isStringsEnd(cell)) return 0;
   int result = 0;
   int height = getHeight();
   for (int y = 0; y < height; y++) {
     Cell cCell = new Cell(cell.x, y);
     if (!cCell.equals(cell) && isStringsEnd(cCell)) {
       result++;
     }
   }
   return result;
 }
Ejemplo n.º 3
0
  public static ArrayList<Cell> findPath(Cell start, Cell goal, Map<String, Cell> gridCells) {
    Set<Cell> closedSet = new HashSet<>();
    Set<Cell> openSet = new HashSet<>();
    openSet.add(start);
    start.g = 0;
    start.h = heuristicCostEstimate(start, goal);
    start.f = start.g + start.h;

    while (!openSet.isEmpty()) {
      double min = Integer.MAX_VALUE;
      Cell x = null;
      for (Cell cell : openSet)
        if (cell.f < min) {
          x = cell;
          min = cell.f;
        }
      if (goal.equals(x)) return reconstructPath(start, goal);
      openSet.remove(x);
      closedSet.add(x);
      for (Cell y : neighborNodes(x, gridCells)) {
        if (closedSet.contains(y)) continue;
        boolean tentativeIsBetter;
        // <tentative>
        double tentativeG = x.g + 2;
        if (x.cameFrom != null) {
          if (x.cameFrom.cameFrom != null
              && Math.abs(x.cameFrom.cameFrom.x - y.x) + Math.abs(x.cameFrom.cameFrom.y - y.y)
                  == 1) {
            tentativeG += 2;
          }
          if (x.cameFrom.y == x.y && y.y != x.y || x.cameFrom.x == x.x && y.x != x.x) {
            tentativeG += (2 * Math.sqrt(2) - 4);
          }
        }
        if (y.isPenult) tentativeG += 10;
        // </tentative>
        if (!openSet.contains(y)) {
          openSet.add(y);
          tentativeIsBetter = true;
        } else {
          tentativeIsBetter = tentativeG < y.g;
        }
        if (tentativeIsBetter) {
          y.cameFrom = x;
          y.g = tentativeG;
          y.h = heuristicCostEstimate(y, goal);
          y.f = y.g + y.h;
        }
      }
    }
    return new ArrayList<>();
  }
Ejemplo n.º 4
0
  /**
   * Returns the ghosts in the cell of the line segment ab and the ghosts of this cell's
   * 8-neighborhood.
   *
   * @param a Start point of the line segment.
   * @param b End point of the line segment.
   * @return List of ghosts.
   */
  public Set<Ghost> getGhosts(Point a, Point b) {
    Cell cA = getCell(a);
    Cell cB = getCell(b);

    Set<Ghost> ghosts = new HashSet<Ghost>();
    ghosts.addAll(grid.get(cA.row).get(cA.column));
    ghosts.addAll(get8Neighborhood(cA));

    if (!cB.equals(cA)) {
      ghosts.addAll(grid.get(cB.row).get(cB.column));
      ghosts.addAll(get8Neighborhood(cB));
    }

    return ghosts;
  }
  /** @return the largest connected component of the graph. */
  public ArrayList<Cell> getLargestConnectedComponent() {
    ArrayList<ArrayList<Cell>> components = new ArrayList<>();
    boolean visited[][] = new boolean[rowCount][colCount];
    for (int row = 0; row < rowCount; ++row)
      for (int col = 0; col < colCount; ++col) visited[row][col] = false;

    Queue<Cell> q;
    Cell t = null, u = null;
    for (Cell c : g.vertexSet()) {
      if (!visited[c.getRow()][c.getCol()]) {
        q = new LinkedList<Cell>();
        ArrayList<Cell> component = new ArrayList<>();
        visited[c.getRow()][c.getCol()] = true;

        // Find all connected nodes
        q.add(c);
        component.add(c);
        while (!q.isEmpty()) {
          t = q.remove();
          for (WeightedEdge e : g.edgesOf(t)) {
            u = t.equals(g.getEdgeSource(e)) ? g.getEdgeTarget(e) : g.getEdgeSource(e);
            if (!visited[u.getRow()][u.getCol()]) {
              visited[u.getRow()][u.getCol()] = true;
              q.add(u);
              component.add(u);
            }
          }
        }

        components.add(component);
      }
    }

    int largestSize = 0, largestIndex = 0;
    for (int i = 0; i < components.size(); ++i) {
      if (components.get(i).size() > largestSize) {
        largestSize = components.get(i).size();
        largestIndex = i;
      }
    }

    filterGraph(components.get(largestIndex));
    return components.get(largestIndex);
  }
Ejemplo n.º 6
0
 /**
  * Resets the current solution based on the given Constraints. Cells which are not constrained can
  * take all possible values
  *
  * @param constraints the Constraints object used to initialise this solution
  */
 public void reset(Constraints constraints) {
   Set<Cell> blankCells = new HashSet<Cell>();
   for (int i = 0; i < dimSq; i++) {
     for (int j = 0; j < dimSq; j++) {
       Cell cell = new Cell(i, j);
       Set<Integer> values = new HashSet<Integer>();
       for (int z = 1; z <= dimSq; z++) {
         Integer value = new Integer(z);
         values.add(value);
       }
       cell.setValues(values);
       blankCells.add(cell);
     }
   }
   Set<Cell> constrainedCells = constraints.getCells();
   for (Cell constrainedCell : constrainedCells) {
     for (Cell blankCell : blankCells) {
       if (blankCell.equals(constrainedCell)) {
         cells.remove(blankCell);
         cells.add(constrainedCell);
       }
     }
   }
 }
Ejemplo n.º 7
0
 boolean containsCell(Cell in) {
   for (Cell c : this) {
     if (c.equals(in)) return true;
   }
   return false;
 }