public void markMine(int x, int y) {
        final Cell cell = cells[x][y];
        final Cell.Type type = cell.getType();

        // dont allow opened tiles to be flagged
        if (cell.getState() == Cell.State.OPEN)
            return;

        // unflag the cell if its already marked
        if (type == Cell.Type.FLAGGED)
            cell.setType(Cell.Type.EMPTY);
        else if (type == Cell.Type.FLAGGED_MINE)
            cell.setType(Cell.Type.MINE);

            // don't allow more flags than mines
        else if (countFlags(false) == numMines)
            return;

            // flag the cell
        else if (type == Cell.Type.MINE)
            cell.setType(Cell.Type.FLAGGED_MINE);
        else
            cell.setType(Cell.Type.FLAGGED);
    }