在四个方向(上,下,左,右)遍历2D数组
我正在尝试创建一种遍历二维二维数组的方法,该方法采用一个参数来指定向上,向下,向左或向右的方向.我已经能够将任务分为两个单独的方法;一种用于垂直遍历,另一种用于水平遍历:
I'm trying to create a method for traversing a square 2-dimensional array which takes in a parameter that specifies the direction, either up, down, left or right. I've been able to split the task into two separate methods; one for traversing vertically and one for traversing horizontally:
private void concatHorizontal(boolean left) {
int rowIndex, columnIndex, index;
for (int i=0; i<board.size(); i++) {
index = (left) ? 0 : board.get(0).size()-1;
for (int j=0; j<board.get(0).size(); j++) {
if (left) {
rowIndex = i;
columnIndex = j;
} else {
rowIndex = board.size() - 1 - i;
columnIndex = board.get(0).size() - 1 - j;
}
int val = board.get(rowIndex).get(columnIndex).getValue();
if (val != 0) {
board.get(rowIndex).get(index).setValue(val);
if (columnIndex != index) {
board.get(rowIndex).get(columnIndex).setValue(0);
}
index = (left) ? index+1 : index-1;
}
}
}
}
private void concatVertical(boolean up) {
int rowIndex, columnIndex, index;
for (int i=0; i<board.get(0).size(); i++) {
index = (up) ? 0 : board.size()-1;
for (int j=0; j<board.size(); j++) {
if (up) {
columnIndex = i;
rowIndex = j;
} else {
columnIndex = board.get(0).size() - 1 - i;
rowIndex = board.size() - 1 - j;
}
int val = board.get(rowIndex).get(columnIndex).getValue();
if (val != 0) {
board.get(index).get(columnIndex).setValue(val);
if (rowIndex != index) {
board.get(rowIndex).get(columnIndex).setValue(0);
}
index = (up) ? index+1 : index-1;
}
}
}
}
该方法将数组中的所有值在给定方向上尽可能移至不等于零的位置,例如
The methods shifts all values in an array not equal to zero as far as possible in a given direction, like in the game 2048 but without merging neighbors with the same value.
如您所见,我的代码既不直观也不紧凑,并且我希望将上述两种方法合并为一个.换句话说,它应根据给定方向像下面的for循环之一那样遍历arraylist.
As you can see my code is neither intuitive nor compact, and I would like to merge the two methods above into one if possible. It should, in other words, traverse the arraylist like one of the for-loops below depending on the given direction.
for (int i=0; i<board.size(); i++) {
for (int j=0; j<board.get(0).size(); j++) {
// Traverse to the right
}
}
for (int i=0; i<board.size(); i++) {
for (int j=board.get(0).size()-1; j>=0; j--) {
// Traverse to the left
}
}
for (int i=0; i<board.get(0).size(); i++) {
for (int j=0; j<board.size(); j++) {
// Traverse downwards
}
}
for (int i=0; i<board.get(0).size(); i++) {
for (int j=board.size()-1; j>=0; j--) {
// Traverse upwards
}
}
这是解决方案的一步(简化了对元素值的访问):
This is one step of the solution (with simplified access to the element value):
private void concatHori(boolean left) {
if( left ){
concatHori( 0, 0, 1 );
} else {
concatHori( board.size()-1, board.get(0).size()-1, -1 );
}
}
private void concatHori(int rowstart, int colstart, int inc ) {
for (int i=0; i<board.size(); i++) {
int index = colstart;
for (int j=0; j<board.get(0).size(); j++) {
int rowIndex = rowstart + inc*i;
int colIndex = colstart + inc*j;
int val = board.get(rowIndex).get(colIndex);
if (val != 0) {
board.get(rowIndex).set(index, val);
if (colIndex != index) {
board.get(rowIndex).set(colIndex, 0);
}
index += inc;
}
}
}
}
垂直"方法可以用相同的方式转换.
The "vertical" method can be transformed in the same way.
第二步将是水平"和垂直"的结合,这可以通过为-然后-单个方法提供访问功能来完成.但是我认为所得到的代码将比现在的代码更多行,并且不那么直观,因此我避免添加步骤2.
The second step would be the union of Horizontal and Vertical, which could be done by providing access functions to the - then - single method. But I think that the resulting code would be more lines and less intuitive that what there is now, and therefore I refrain from adding step 2.