Android Open Source - Android-Minesweeper Board






From Project

Back to project page Android-Minesweeper.

License

The source code is released under:

MIT License

If you think the Android project Android-Minesweeper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jigglesoft.minesweeper.game.board;
//  www. j  ava 2 s  .  c o  m
import java.util.ArrayList;
import java.util.Collections;

import com.jigglesoft.minesweeper.game.exceptions.IllegalGameConstructionException;

public class Board {
  
  Cell[][] cells;

  public Board(int rows, int columns, int noOfBombs) throws IllegalGameConstructionException {
    
    int noOfCells = rows * columns;
    
    if ( noOfBombs > noOfCells ) {
      throw new IllegalGameConstructionException("More bombs than fields.");
    }
    
    ArrayList<Cell> cellList = createCellList(noOfBombs, noOfCells);
    
    Collections.shuffle(cellList);
    
    
    cells = new Cell[rows][columns];
    
    for (int row = 0; row < cells.length; row++) {
      for (int column = 0; column < cells[row].length; column++) {
        cells[row][column] = cellList.remove(0);
      }
    }
    
    setCellNeighbors();
    
  }



  private void setCellNeighbors() {
    
    for (int row = 0; row < cells.length; row++) {
      for (int column = 0; column < cells[row].length; column++) {
        
        ArrayList<Cell> neighborsList = new ArrayList<Cell>();
        
        for (int neighborRow = row - 1; neighborRow < row + 2; neighborRow++) {
          for (int neighborColumn = column - 1; neighborColumn < column + 2; neighborColumn++) {
            
            if (  neighborRow >= 0 && neighborRow < cells.length
                && neighborColumn >= 0 && neighborColumn < cells[row].length &&
                cells[neighborRow][neighborColumn] != cells[row][column]){
              
              neighborsList.add(cells[neighborRow][neighborColumn]);
              
            }
          }
        }
        
        Cell[] neighborsArray = new Cell[neighborsList.size()];
        neighborsList.toArray(neighborsArray);
        cells[row][column].setNeighbors(neighborsArray);
        
      }
    }
  }





  private ArrayList<Cell> createCellList(int noOfBombs, int noOfCells) {
    ArrayList<Cell> cellList = new ArrayList<Cell>();
    
    for (int i = 0; i < noOfCells - noOfBombs; i++) {
      cellList.add(new Cell());
    }
    for (int i = 0; i < noOfBombs; i++) {
      cellList.add(new BombCell());
    }
    return cellList;
  }
  
  
  
  public void revealAllBombs() {
    
    for (Cell[] row : cells) {
      for (Cell cell : row) {
        if (cell.isBomb()) cell.revealCell();
      }
    }
  }

  public boolean allNonBombCellsRevealed() {
    
    for (Cell[] row : cells) {
      for (Cell cell : row) {
        if (!cell.isRevealed() && !cell.isBomb()) {
          return false;
        }
      }
    }
    
    return true;
  }


  public Cell[][] getCells() {
    return cells;
  }


}




Java Source Code List

com.jigglesoft.minesweeper.BoardAdapter.java
com.jigglesoft.minesweeper.MainActivity.java
com.jigglesoft.minesweeper.MinesweeperFragment.java
com.jigglesoft.minesweeper.SettingsActivity.java
com.jigglesoft.minesweeper.SettingsFragment.java
com.jigglesoft.minesweeper.SquareImageView.java
com.jigglesoft.minesweeper.game.MinesweeperGame.java
com.jigglesoft.minesweeper.game.board.Board.java
com.jigglesoft.minesweeper.game.board.BombCell.java
com.jigglesoft.minesweeper.game.board.Cell.java
com.jigglesoft.minesweeper.game.board.Flags.java
com.jigglesoft.minesweeper.game.exceptions.IllegalGameConstructionException.java