Example #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);
      }
    }
  }
Example #2
0
  private boolean moveSapperAndFillFreeCell(Direction direction) {
    walkAt.put(sapper.copy(), getMinesNearSapper());
    direction.change(sapper);

    boolean wasHere = walkAt.containsKey(sapper.copy());
    return !wasHere;
  }
Example #3
0
 @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();
 }
Example #4
0
 @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();
   }
 }
Example #5
0
 @Override
 public boolean isWin() {
   return getMines().size() == 0 && !sapper.isDead();
 }
Example #6
0
 @Override
 public boolean isEmptyDetectorButPresentMines() {
   return getMines().size() != 0 && sapper.isEmptyCharge();
 }
Example #7
0
 @Override
 public Point getCellPossiblePosition(Direction direction) {
   return direction.change(sapper.copy());
 }
Example #8
0
 @Override
 public boolean isGameOver() {
   return sapper.isDead() || isEmptyDetectorButPresentMines() || isWin();
 }