Android Open Source - FindtheMines Board






From Project

Back to project page FindtheMines.

License

The source code is released under:

MIT License

If you think the Android project FindtheMines 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 findtheminecore;
//from   w  w w .  j a v  a  2 s. c o m
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Board {

  private static final String START_STRING = ":";

  private Map<String,Tile> grid;
  
  private BoardSize boardSize;
  private int noOfSelected;
  private int noOfMarked;
  private int noOfMines;
  private int noOfMovesLeft;
  private boolean isExploded = false;

  //// constructors  ////
  
  public Board(BoardSize size){
    boardSize = size;
    noOfMarked = 0;
    noOfSelected = 0;
    noOfMines = 0;
    noOfMovesLeft =  boardSize.size();
    grid = new  HashMap<String,Tile>();
    intialiseTiles(boardSize);
  }  
  
  public Board(){
    this(BoardSize.STANDARD);
  }
  
  private void intialiseTiles(BoardSize boardSize) {
    for(int row = 0;row<boardSize.noOfRows();row++ ){
      for (int col = 0;col<boardSize.noOfCols();col++ ){
        Position pos = new Position(row,col);
        grid.put(pos.toString(),new Tile())  ;    
      }
    }  
  }

  //// methods ///
  
  public boolean selectTile(Position pos) {
    Tile tile = getTile(pos);
    // test that we get a tile
    if (tile!=null){
      
      if (tile.select()){
        noOfSelected++;
        noOfMovesLeft--;
        // test exploded
        setExploded(tile.isExploded());
      }
      return true;      
    }else{
      return false;
    }    
  }
  
  public void markTile(Position pos) {
    getTile(pos).setMarked(true);
    noOfMarked++;
    noOfMovesLeft--;  
  }

  public void revealBoard() {
    for (int row = 0 ;row<this.getNoOfRows();row++){
      for( int col = 0;col<this.getNoOfCols();col++){      
        this.getTile(new Position(row, col)).reveal();
      }      
    }
  }

  public void clear() {
    for (int row = 0 ;row<this.getNoOfRows();row++){
      for( int col = 0;col<this.getNoOfCols();col++){
        this.getTile(new Position(row, col)).clear();
        noOfMarked=0;
        noOfMovesLeft=0;  
      }
    }  
  }

  public void revealMines() {
    for (int row = 0 ;row<this.getNoOfRows();row++){
      for( int col = 0;col<this.getNoOfCols();col++){        
        Tile tile = getTile(new Position(row, col));
        if(tile.isMine() && !tile.isExploded()){
          tile.reveal();
        }
      }
    }  
  }
  
  public void unMark(Position pos) {
    noOfMarked--;
    noOfMovesLeft++;
    this.getTile(pos).unMark();
    
  }

  //// tests ////

  public boolean isExploded() {
    return isExploded;
  }
  
  /// Setters  ///
  
  public void setMine(Position pos) {
    // can not set a mine that is already a mine
    Tile tile = grid.get(pos.toString());
    if (!tile.isMine()){
      tile.setMine(true);
      noOfMines++;
      tile.addValueBy1();
      //add 1 to all surrounding tiles
      for (int i=pos.getRow()-1;i<=pos.getRow()+1;i++){
        for(int j=pos.getCol()-1;j<=pos.getCol()+1;j++){
          if ( (i>=0 && i<getNoOfRows()) && (j>=0 && j<getNoOfCols()) && !(i==pos.getRow() && j==pos.getCol())){
            grid.get(i+","+j).addValueBy1();
          }        
        }      
      }      
    }
  }
  
  public void setExploded(boolean isExploded) {
    this.isExploded = isExploded;
  }  
  
  /// Getters ///
  
  public int getValue(Position pos) {
    return getTile(pos).getValue();
    
  }

  public Tile getTile(Position pos) {  
    if (!(pos.getRow()<0) && !(pos.getCol() <0) && !(pos.getRow()>= boardSize.noOfRows()) && !(pos.getCol()>=boardSize.noOfCols())){
      return grid.get(pos.getRow()+","+pos.getCol());
    }else{
      return null;
    }  
  }
  

  
  public int getSize() {
    return boardSize.size();
  }

  public int getNoOfMovesLeft() {    
    return noOfMovesLeft ;
  }  
  public int getNoOfMines() {
    return noOfMines;
  }


  public int getNoOfMarked() {
    return noOfMarked;
  }
  public int getNoOfRows() {
    return boardSize.noOfRows();
  }

  public int getNoOfCols() {
    return boardSize.noOfCols();
  }


  public int getNoOfSelected() {
    return noOfSelected;
  }

  public BoardSize getBoardSize() {
    return this.boardSize;
  }

  public ArrayList<Tile> getAllTiles() {
    return  new ArrayList<Tile>(grid.values());
  }

  public ArrayList<String> getTileStringArray(int[] pos) {
    ArrayList<String> strings  = new ArrayList<String>();
    strings.add(START_STRING);
    strings.add(Integer.toString(pos[0]));
    strings.add(Integer.toString(pos[1]));
    strings.addAll(this.getTile(pos).toStringArray());
    return strings;
  }

  private Tile getTile(int[] pos) {
    return this.getTile(new Position(pos));
    
  }


}




Java Source Code List

com.stealthness.findthemines.ImageAdapter.java
com.stealthness.findthemines.MainActivity.java
com.stealthness.findthemines.SplashScreen.java
findtheminecore.BoardSize.java
findtheminecore.Board.java
findtheminecore.Game.java
findtheminecore.Position.java
findtheminecore.RuleSet.java
findtheminecore.TileType.java
findtheminecore.Tile.java