1

我目前正在做一个小项目。我正在为 L 游戏编写一个简单的 java 程序。我需要编写一个移动4x4数组内容的方法。此方法将接受(row, column)参数。并且内容将相应移动。

{ 'x',' ',' ',' ' }, 

{ 'x',' ',' ',' ' },

{ 'x','x',' ',' ' },

{ ' ',' ',' ',' ' }

移动 (0, 2) --->

{ ' ',' ','x',' ' },

{ ' ',' ','x',' ' },

{ ' ',' ','x','x' },

{ ' ',' ',' ',' ' }

我不知道从哪里开始。我非常感谢对此提供的任何帮助。非常感谢你的帮助。

4

2 回答 2

1

你的方法应该是这样的

char[][] array = new char[4][4];

public static void move(row, column){
    for (int i = 0, i < 4; i++) {
        for (int j = 0; j < 4; j++){
            if (array[i][j] != null) {
                // add rows and column accordingly
                array[i + row][j + column] = array[i][j];
                array[i][j] = null;
            }
        }
    }
}

这是考虑到每行只有一个x,在您的情况下,有些行有两个。我会让你弄清楚那个。

于 2013-10-23T05:28:21.260 回答
1
    int moverow = 0;
    int moveCol = 2;

    for(int i = 0; i <=3; i++){
        for(int j = 0; j <=3; j++){
            int currentValue = board[i][j];
            //shifting value
            int shiftX = i + moverow;
            int shiftY = j + moveCol;
            // discarding the value if index overflows
            if(shiftX > 3 || shiftY > 3){

                // setting initial value on the original index.
                board[i][j] = 0;
                break;
            }else{
                board[shiftX][shiftY] = currentValue;
                // setting initial value on the original index.
                board[i][j] = 0;
            }
        }
    }
于 2013-10-23T05:49:09.230 回答