2017-06-16 6 views
0

AndroidスタジオでAndroidでMinesweeperをプログラミングしていますが、表示されている数値を計算するためにセルの近傍を数えようとしています。地雷除去アルゴリズムがスタックされています

しかし、すべての細胞が隣人の同じ数が間違って持っているので、カウントは動作しません:

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); 
    } 
} 

答えて

1

私はこの問題はここにあるかもしれないと思う:

public static int i; 
public static int j; 

iとjは、それらがセルクラスのすべてのメンバー間で共有されている静的な意味です。

つまり、それらはクラスのインスタンスではなくクラスに属します。

静的を削除してみます。

+0

ええ、それは間違いでした。今では、すべてのセルが再び個別に計算されます。 – Urbs

関連する問題