8パズルゲームをしました。スクランブル方法に問題があることがわかりましたが、修正方法がわかりません。私のコードを手伝ってくれる人はいますか? これがスクランブル メソッドのコードです。私のコードの問題は、スクランブル ボタンをクリックした後、数字が 2 つの数字の画像だけになり、もう一度スクランブルをクリックすると、9 つのボタンに 1 つの数字しか表示されないことです。
public void scramble()
{
for(int i = 0; i <SHUFFLE_NUM; i++)
{
int x1 = rand.nextInt(BOARD_SIZE);
int x2 = rand.nextInt(BOARD_SIZE);
int y1 = rand.nextInt(BOARD_SIZE);
int y2 = rand.nextInt(BOARD_SIZE);
Piece temp = board[x1][y1];
board [x1][y1] = board[x2][y2];
board[x1][y2] = temp;
}
}
アップデート
ここで別のバグを見つけました。リセットボタンをクリックした後、番号ボタンを移動しようとすると、移動ステップが間違っています。ここで、move メソッドと reset メソッドを添付します
public boolean move(int _x, int _y)
{
boolean valid = false;
if(_x == currentCol-1 && _y == currentRow ) // on the left of empty one
valid = true;
else if(_x == currentCol+1&&_y == currentRow) //on the right of empty one
valid = true;
else if(_x == currentCol&&_y == currentRow-1) // on the top of empty one
valid = true;
else if(_x == currentCol &&_y == currentRow +1) // on the bottom of empty one
valid = true;
if(valid)
{
Piece temp;
temp = board[_x][_y];
board[_x][_y] = board[currentCol][currentRow];
board[currentCol][currentRow] = temp;
currentCol = _x;
currentRow = _y;
}
return valid;
}
リセット方法はこちら
public void reset()
{
for(int i =0; i<BOARD_SIZE; i++)
for(int j =0; j<BOARD_SIZE; j++)
{
int value = i*BOARD_SIZE+j+1 ;
String filePath;
if(value!= BOARD_SIZE*BOARD_SIZE)
filePath = "Piece" + value +".jpg"; //what is this mean?
else
filePath = "blank piece.jpg";
board[i][j]= new Piece(new ImageIcon(filePath),i, j, value);
}
}
リセットボタンをクリックしなかった場合、移動は正常に機能します..