私は行列内の自由な位置を探すためのこの方法を持っています。これは私がmatrix
を印刷したときの出力です。理由がないためにポイントxの値が変化します
1 0 0 0 0 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0
0 1 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
私は(0,0)
のcurrentPos
を持っています。次の自由な位置は(0,1)でなければなりません。私がNextFreePos()
と呼ぶと、それは変わるべきではないにしても、currentPos.x
の値が変わるでしょう。
NEXT POS = (0;0)
SAME X
NEXT POS = (1;1) // I dont get why the x (first value) changed to 1
public void nextFreePos(Point currentPos){
// display the OLD POS (x,y) <-- x = row and y = column
System.out.println("OLD POS = ("+(currentPos.x)+";"+currentPos.y+")");
boolean stop = false;
// check if there is a free Pos in current row, starting from current y
for(int y = currentPos.y; y < 7; y++){
// check if Position is free and no free position has been found yet
if(matrix[currentPos.x][y] == 0 && stop == false){
stop = true;
System.out.println("SAME X");
currentPos.y = y;
}
}
currentPos.x++;
for(int x = currentPos.x; x < 7; x++){
for(int y = 0; y < 7; y++){
// check if Position is free and no free position has been found yet
if(matrix[x][y] == 0 && stop == false){
stop = true;
System.out.println("OTHER X");
// set the position to x and y
currentPos.x = x;
currentPos.y = y;
}
}
}
// display the NEXT POS (x,y)
System.out.println("NEXT POS = ("+currentPos.x+";"+currentPos.y+")");
}
あなたは 'currentPos.x ++;でインクリメントします –