/*
 * Copyright 2012 Evan Flynn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.foobar.minesweeper.model;

import java.util.List;

public class Square {
  private final int column;
  private final int row;
  private final Minefield minefield;
  private boolean mine;
  private Squares type = Squares.BLANK;
  private int nearbyMines;

  Square(Minefield minefield, int row, int column) {
    this.minefield = minefield;
    this.row = row;
    this.column = column;
  }

  public void addNearbyMine() {
    nearbyMines++;
  }

  /**
   * Gets the type of the Square.
   *
   * @return type of the Square
   */
  public Squares getType() {
    return type;
  }

  /**
   * Determines whether the square can be revealed. This is equivalent to
   * {@code !minefield.isGameOver() && getType() == Squares.BLANK}, where
   * {@code minefield} is the field this square belongs to.
   *
   * @return true if the square can be revealed.
   */
  public boolean isRevealable() {
    return !minefield.isGameOver() && type == Squares.BLANK;
  }

  /**
   * Gets the row of the square.
   *
   * @return the row of the square
   */
  public int getRow() {
    return row;
  }

  /**
   * Gets the column of the square.
   *
   * @return the column of the square.
   */
  public int getColumn() {
    return column;
  }

  /**
   * Gets the number of nearby mines.
   *
   */
  public int getMineCount() {
    return nearbyMines;
  }

  /**
   * Toggles the flag state of the square. If the game is over or the square
   * cannot be flagged, the method returns.
   *
   */
  public void toggleFlag() {
    if (minefield.isGameOver())
      return;

    if (type == Squares.FLAG)
      type = Squares.BLANK;
    else if (type == Squares.BLANK)
      type = Squares.FLAG;
    else
      return;

    minefield.updateSquare(this);
  }

  /**
   * Reveals this square. If the square is a mine, the game is over. If the game
   * is over or the square is flagged, the method returns.
   *
   * <p>
   * Calling this method repeatedly will have no effect until the game is
   * restarted.
   */
  public void reveal() {
    if (type != Squares.BLANK || minefield.isGameOver())
      return;

    if (mine) {
      type = Squares.HITMINE;
      mine = false;
      minefield.onGameLost();
    }
    else {
      minefield.reveal(this);
    }
  }

  /**
   * Reveals nearby squares. The square must be already exposed for this call to
   * work. Otherwise, the method returns with no change.
   */
  public void revealNearby() {
    if (minefield.isGameOver() || type != Squares.EXPOSED)
      return;

    List<Square> neighbors = minefield.findNeighbors(this);
    int nearbyFlags = 0;

    for (Square square : neighbors) {
      if (square.type == Squares.FLAG) {
        nearbyFlags++;
      }
    }

    if (nearbyFlags == nearbyMines) {
      for (Square square : neighbors) {
        square.reveal();
      }
    }
  }

  boolean isMine() {
    return mine;
  }

  void setMine(boolean isMine) {
    mine = isMine;
  }

  void onGameLost() {
    if (mine)
      type = Squares.MINE;
    else if (type == Squares.FLAG)
      type = Squares.WRONGMINE;
  }

  void onGameWon() {
    if (mine)
      type = Squares.FLAG;
  }

  int visit() {
    int exposed = 1;
    type = Squares.EXPOSED;

    if (nearbyMines == 0) {
      for (Square square : minefield.findNeighbors(this)) {
        if (square.type != Squares.EXPOSED) {
          exposed += square.visit();
        }
      }
    }

    return exposed;
  }
}

@@ -2132,32 +2132,34 @@
 ld.isGameOver())
+ %7B
 %0A      return;%0A%0A
@@ -2148,32 +2148,38 @@
  %7B%0A      return;
+%0A    %7D
 %0A%0A    if (type =
@@ -2185,32 +2185,34 @@
 == Squares.FLAG)
+ %7B
 %0A      type = Sq
@@ -2223,24 +2223,26 @@
 s.BLANK;%0A   
+ %7D
  else if (ty
@@ -2261,16 +2261,18 @@
 s.BLANK)
+ %7B
 %0A      t
@@ -2294,21 +2294,25 @@
 LAG;%0A   
+ %7D
  else
+ %7B
 %0A      r
@@ -2313,24 +2313,30 @@
      return;
+%0A    %7D
 %0A%0A    minefi
@@ -2701,16 +2701,18 @@
 eOver())
+ %7B
 %0A      r
@@ -2713,24 +2713,30 @@
      return;
+%0A    %7D
 %0A%0A    if (mi
@@ -2826,20 +2826,16 @@
 );%0A    %7D
-%0A   
  else %7B%0A
@@ -3114,16 +3114,18 @@
 EXPOSED)
+ %7B
 %0A      r
@@ -3130,16 +3130,22 @@
  return;
+%0A    %7D
 %0A%0A    Li
@@ -3590,32 +3590,34 @@
  %7B%0A    if (mine)
+ %7B
 %0A      type = Sq
@@ -3631,16 +3631,18 @@
 INE;%0A   
+ %7D
  else if
@@ -3664,16 +3664,18 @@
 es.FLAG)
+ %7B
 %0A      t
@@ -3699,16 +3699,22 @@
 NGMINE;%0A
+    %7D%0A
   %7D%0A%0A  v
@@ -3744,16 +3744,18 @@
 f (mine)
+ %7B
 %0A      t
@@ -3774,16 +3774,22 @@
 s.FLAG;%0A
+    %7D%0A
   %7D%0A%0A  i

Add braces