Android Open Source - Android-Minesweeper Board Adapter






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;
//w  ww  . ja v a 2s  . com
import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.jigglesoft.minesweeper.game.board.Board;
import com.jigglesoft.minesweeper.game.board.Cell;

public class BoardAdapter extends BaseAdapter {
  
  private Context context;
  
  private Cell[] cellArray;
  
  
  public BoardAdapter(Context c, Board board) {
    
    context = c;
    
    ArrayList<Cell> cells = new ArrayList<Cell>();
    
    for (Cell[] row : board.getCells()){
      for (Cell cell : row){
        cells.add(cell);
      }
    }
    
    this.cellArray = new Cell[cells.size()];
    cells.toArray(this.cellArray);
    
  }

  @Override
  public int getCount() {
    return cellArray.length;
  }

  @Override
  public Object getItem(int position) {
    return cellArray[position];
  }

  @Override
  public long getItemId(int position) {
    return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new SquareImageView(context);
    
    
    int resId = getImageResourceForCell(cellArray[position]);
    imageView.setImageResource(resId);
    

    return imageView;
  }
  
  private int getImageResourceForCell(Cell cell) {
    
    if (cell.isRevealed()){
      
      if (cell.isBomb()) return R.drawable.bomb_cell;
      if (cell.hasBombNeighbor()) {
        
        switch (cell.noOfNeighborIsBomb()) {
        case 1:
          return R.drawable.neighbor_1;
        case 2:
          return R.drawable.neighbor_2;
        case 3:
          return R.drawable.neighbor_3;
        case 4:
          return R.drawable.neighbor_4;
        case 5:
          return R.drawable.neighbor_5;
        case 6:
          return R.drawable.neighbor_6;
        case 7:
          return R.drawable.neighbor_7;
        case 8:
          return R.drawable.neighbor_8;
        default:
          return R.drawable.revealed_cell;
        }
        
      }
      return R.drawable.revealed_cell;
    }
    if (cell.hasWarningFlag()) return R.drawable.warning_flag;
    
    return R.drawable.hidden_cell;
  }

  
}




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