下面是一个如何实现的示例
right()
,其他的方法也一样。正如你的评论所暗示的那样,我假设这一举动的合法性已经得到证实。
/*
represent the board in a 2-dimensional array with the following coordinate system
x --->
y
|
\/
*/
int x, y = 0; // keeping track of the blank position
int[][] board = ... // initialize the board as needed (sequentially? randomly?), assume -1 represents the blank space
public void right() { // move the blank in the positive x direction
// get the value currently in the position that the blank must move to, as it will need to be swapped
int tmp = board[x + 1][y];
board[x + 1][y] = -1; // move the blank here
board[x][y] = tmp; // complete the swap
x = x + 1; // the new x position of the blank needs to be updated
}