@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 boolean moveSapperAndFillFreeCell(Direction direction) { walkAt.put(sapper.copy(), getMinesNearSapper()); direction.change(sapper); boolean wasHere = walkAt.containsKey(sapper.copy()); return !wasHere; }
@Override public void newGame() { validate(); isFlag = new LinkedList<Point>(); walkAt = new HashMap<Point, Integer>(); printer = new Printer(this.getSize(), new MinesweeperPrinter(this)); useDetector = false; maxScore = 0; score = 0; cells = initializeBoardCells(); sapper = initializeSapper(); sapper.iWantToHaveMineDetectorWithChargeNumber(detectorCharge.getValue()); mines = minesGenerator.get(minesCount.getValue(), this); removedMines = new LinkedList<Point>(); tick(); }
@Override public void sapperMoveTo(Direction direction) { if (isSapperCanMoveToDirection(direction)) { boolean cleaned = moveSapperAndFillFreeCell(direction); if (isSapperOnMine()) { sapper.die(); openAllBoard(); fire(MinesweeperEvents.KILL_ON_MINE); } else { if (cleaned) { fire(MinesweeperEvents.CLEAN_BOARD); } } nextTurn(); } }
@Override public boolean isWin() { return getMines().size() == 0 && !sapper.isDead(); }
@Override public boolean isEmptyDetectorButPresentMines() { return getMines().size() != 0 && sapper.isEmptyCharge(); }
@Override public Point getCellPossiblePosition(Direction direction) { return direction.change(sapper.copy()); }
@Override public boolean isGameOver() { return sapper.isDead() || isEmptyDetectorButPresentMines() || isWin(); }