Beispiel #1
1
  @Override
  public void useMineDetectorToGivenDirection(Direction direction) {
    final Point result = getCellPossiblePosition(direction);
    if (cells.contains(result)) {
      if (sapper.isEmptyCharge()) {
        return;
      }

      if (isFlag.contains(result)) {
        return;
      }

      sapper.tryToUseDetector(
          new DetectorAction() {
            @Override
            public void used() {
              isFlag.add(result);
              if (getMines().contains(result)) {
                removeMine(result);
              } else {
                fire(MinesweeperEvents.FORGET_CHARGE);
              }
            }
          });

      if (isEmptyDetectorButPresentMines()) {
        openAllBoard();
        fire(MinesweeperEvents.NO_MORE_CHARGE);
      }
    }
  }
Beispiel #2
0
 private List<Point> initializeBoardCells() {
   List<Point> result = new ArrayList<Point>();
   for (int x = 1; x < size.getValue() - 1; x++) {
     for (int y = 1; y < size.getValue() - 1; y++) {
       result.add(new PointImpl(x, y));
     }
   }
   return result;
 }
Beispiel #3
0
 @Override
 public List<Point> getFreeCells() {
   List<Point> result = new LinkedList<Point>();
   for (Point cell : getCells()) {
     boolean isSapper = cell.equals(getSapper());
     boolean isBoard =
         cell.getX() == 0
             || cell.getY() == 0
             || cell.getX() == size.getValue() - 1
             || cell.getY() == size.getValue() - 1; // TODO test me
     boolean isMine = isMine(cell);
     if (!isSapper && !isMine && !isBoard) {
       result.add(cell);
     }
   }
   return result;
 }
Beispiel #4
0
 private int getMinesNear(Point position) {
   int result = 0;
   for (Direction direction : Direction.values()) {
     Point newPosition = direction.change(position.copy());
     if (cells.contains(newPosition) && getMines().contains(newPosition)) {
       result++;
     }
   }
   return result;
 }
Beispiel #5
0
 private void removeMine(Point result) {
   removedMines.add(result);
   getMines().remove(result);
   increaseScore();
   recalculateWalkMap();
   fire(MinesweeperEvents.DESTROY_MINE);
   if (getMines().isEmpty()) {
     openAllBoard();
     fire(MinesweeperEvents.WIN);
   }
 }
Beispiel #6
0
 @Override
 public boolean isFlag(Point pt) {
   return isFlag.contains(pt);
 }
Beispiel #7
0
 @Override
 public boolean isMine(Point pt) {
   if (getMines() == null) return false;
   return getMines().contains(pt) || (isGameOver() && removedMines.contains(pt));
 }
Beispiel #8
0
 private boolean isSapperCanMoveToDirection(Direction direction) {
   Point cell = getCellPossiblePosition(direction);
   return cells.contains(cell);
 }