【发布时间】:2015-11-07 11:46:38
【问题描述】:
所以我是一个二维数组上的玩家,当我做一个动作时,我希望玩家移动到他周围的 8 个可用块之一,下面的代码随机移动他,但会移动两次
移动前的地图
草草草草
草雷克草草
草草草草
草草草草
随机移动
0 0
0 0 //这不应该发生
移动后的地图
草草草草
草草草草
草草草草
GrassGrassGrass Rek
import java.util.Random;
public class command_Movment implements command_Move {
inSwamp map = new inSwamp();
inSwamp rek = new Rek();
Random random = new Random();
int row = random.nextInt(3);
int col = random.nextInt(3);
@Override
public Command move() {
for (int i = 0; i < map.grid.length; i++) {
for (int j = 0; j < map.grid[i].length; j++) {
if (map.grid[i][j] == rek.getName()) {
try {
map.grid[i][j] = "Grass";
if (row == 0) {
i++;
}
if (row == 1) {
i--;
}
if (col == 0) {
j++;
}
if (col == 1) {
j--;
}
map.grid[i][j] = rek.getName();
System.out.println(col + " " + row);
break;
} catch (ArrayIndexOutOfBoundsException exception) {
if (row == 0) {
i--;
}
if (row == 1) {
i++;
}
if (col == 0) {
j--;
}
if (col == 1) {
j++;
}
map.grid[i][j] = rek.getName();
System.out.println("Error");
break;
}
}
}
}
return null;
} }
【问题讨论】:
标签: java for-loop multidimensional-array