@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); } } }
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; }
@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; }
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; }
private void removeMine(Point result) { removedMines.add(result); getMines().remove(result); increaseScore(); recalculateWalkMap(); fire(MinesweeperEvents.DESTROY_MINE); if (getMines().isEmpty()) { openAllBoard(); fire(MinesweeperEvents.WIN); } }
@Override public boolean isFlag(Point pt) { return isFlag.contains(pt); }
@Override public boolean isMine(Point pt) { if (getMines() == null) return false; return getMines().contains(pt) || (isGameOver() && removedMines.contains(pt)); }
private boolean isSapperCanMoveToDirection(Direction direction) { Point cell = getCellPossiblePosition(direction); return cells.contains(cell); }