0
我正在使用Android Studio編程Android掃雷,我正在計算一個單元的鄰居計算顯示的數字。掃雷算法卡住了
但計數不工作,因爲所有的細胞具有相同的錯誤數量的鄰居:
Here是一個例子圖片作爲超鏈接(顯示後的圖片沒有工作)
搜索方法:
public void countNeighbors() {
if(mine) {
} else {
int total = 0;
for (int xoff = -1 ; xoff <= 1 ; xoff++) {
for (int yoff = -1 ; yoff <= 1; yoff++) {
int row = i + xoff;
int col = j + yoff;
if (row > -1 && row < grid.length && col > -1 && col < grid[row].length) {
//Log.d("SweeperLog", "Found 1 Neighbor");
Cell neighbor = grid[row][col];
if (!neighbor.mine) {
total++;
}
}
}
}
neighborCount = total;
Log.d("SweeperLog", "NeighborCount " + neighborCount);
}
}
而且SweeperView.class中,一切都被drawed創造:
public class SweeperView extends View {
public static int cols = 10;
public static int rows = 10;
public static Cell[][] grid = new Cell[cols][rows];
private float height = 600;
private float width = 600;
private int w = 60;
public SweeperView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
/*
* Gives every grid a cell
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = new Cell(i, j, w, getContext());
}
}
/*
* Count neighbor of every cell
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j].countNeighbors();
}
}
}
public void checkCells(float x, float y) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j].contains(x, y)) {
grid[i][j].reveal();
}
}
}
}
@Override
protected void onDraw(Canvas canvas) {
/*
* Draws the outer rectangle
*/
//Paint paint = new Paint(Color.GRAY);
//paint.setStyle(Paint.Style.STROKE);
//canvas.drawRect(1, height, width, 0, paint);
/*
* Draws every grid
*/
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j].show(canvas);
}
}
}
而且Cell.class:
public class Cell {
private boolean revealed = true;
private boolean mine = false;
private Context context;
public static int i;
public static int j;
private int x;
private int y;
private int w;
private int neighborCount = 0;
private Bitmap unrev;
private Bitmap rev;
private Bitmap minePic;
private Bitmap[] tiles = new Bitmap[8];
public Cell(int i, int j, int w, Context context) {
this.i = i;
this.j = j;
this.x = i * w;
this.y = j * w;
this.w = w;
this.context = context;
Random rand = new Random();
if(rand.nextInt(2) == 0) {
mine = true;
}
/*
Bitmap coding
*/
unrev = BitmapFactory.decodeResource(context.getResources(), R.drawable.unrevealed_tile);
rev = BitmapFactory.decodeResource(context.getResources(), R.drawable.revealed_tile);
minePic = BitmapFactory.decodeResource(context.getResources(), R.drawable.mine);
tiles[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_0);
tiles[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_1);
tiles[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_2);
tiles[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_3);
tiles[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_4);
tiles[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_5);
tiles[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_6);
tiles[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.tile_7);
}
public boolean contains(float x, float y) {
if (x > this.x && x < this.x + this.w && y > this.y && y < this.y + this.w) {
return true;
}
return false;
}
public void reveal() {
revealed = true;
}
public void show(Canvas canvas) {
if(!revealed) {
canvas.drawBitmap(unrev, x-1, y, null);
} else {
if (mine) {
canvas.drawBitmap(minePic, x-1, y, null);
} else {
Bitmap draw = tiles[neighborCount];
canvas.drawBitmap(draw, x-1, y, null);
}
}
}
public void countNeighbors() {
if(mine) {
} else {
int total = 0;
for (int xoff = -1 ; xoff <= 1 ; xoff++) {
for (int yoff = -1 ; yoff <= 1; yoff++) {
int row = i + xoff;
int col = j + yoff;
if (row > -1 && row < grid.length && col > -1 && col < grid[row].length) {
//Log.d("SweeperLog", "Found 1 Neighbor");
Cell neighbor = grid[row][col];
if (!neighbor.mine) {
total++;
}
}
}
}
neighborCount = total;
Log.d("SweeperLog", "NeighborCount " + neighborCount);
}
}
是的,這是錯誤,現在每個細胞再次計算個體。 – Urbs