您好我正在尝试找出一种算法来填充2D阵列:http://websso/ 10X10的设置方式。在10X10中的草,墙,记事本和端点对象位置设置在各个阵列中,通过它们循环并设置到2D阵列中。这是我需要能够纠正的可怕的硬编码。我正在努力思考一种能够在没有奢侈线条的情况下执行此算法的算法。我想具体问一下我应该怎样做才能解决这个问题。你能否就解决这个问题向我提出建议?
Grass Grass Grass Wall null null Wall Grass Grass Grass
Grass Grass Grass Wall null null Wall Grass Grass Grass
Grass Grass Grass Wall null null Wall Grass Grass Grass
Grass Grass Wall null Note Note null Wall Grass Grass
Grass Grass Wall Note Wall Wall Note Wall Grass Grass
Grass Wall null null Wall Wall null null Wall Grass
Wall null null null Wall Wall null null null Wall
Grass Wall null Note Wall Wall Note null Wall Grass
Grass Wall Wall null Note Note null Wall Wall Grass
Grass Grass Wall Wall End End Wall Wall Grass Grass
这是目前的计划。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JComponent;
public class GameField {
private String[][] field;
private String grass, wall, note, end;
private ArrayList<Integer> arrayG;
private ArrayList<Integer> arrayW;
private ArrayList<Integer> arrayN;
private ArrayList<Integer> arrayE;
public GameField() {
field = new String[10][10];
grass = "Grass";
wall = "Wall";
note = "Note";
end = "End";
arrayG = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 7, 8, 9, 10, 11, 12, 17, 18, 19, 20, 21, 22, 27, 28, 29, 30, 31, 38, 39, 40, 41, 48, 49, 50, 59, 70, 79, 80, 89, 90, 91, 98, 99));
arrayW = new ArrayList<Integer>(Arrays.asList(3, 6, 13, 16, 23, 26, 32, 37, 42, 44, 45, 47, 51, 54, 55, 58, 60, 64, 65, 69, 71, 74, 75, 78, 81, 82, 87, 88, 92, 93, 96, 97));
arrayN = new ArrayList<Integer>(Arrays.asList(34, 35, 43, 46, 73, 76, 84, 85));
arrayE = new ArrayList<Integer>(Arrays.asList(94, 95));
fillField();
}
public void checkField(int x, int y) {
}
private void fillField() {
int k = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (arrayG.contains(k)) {
field[i][j] = grass;
}
else if (arrayW.contains(k)){
field[i][j] = wall;
}
else if (arrayN.contains(k)){
field[i][j] = note;
}
else if (arrayE.contains(k)){
field[i][j] = end;
}
k++;
}
}
print();
}
public void print() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.printf("%8s", field[i][j]);
}
System.out.println("");
}
System.out.println("");
}
}