プログラムで、無限ループと誤った応答に関する問題が発生しています。私のプログラムでは、戦艦ゲーム用に船をランダムに設定しようとしていますが、船のパーツの配置に問題があります。私はそれをコーディングしましたが、2 つの問題が発生しています。1 つはどこかに無限ループがあることですが、どこにあるのかわかりません。次は、ピースがグリッド上で適切に設定されていないことです。このコードを長い間調べましたが、修正が見つかりませんでした。ここにあります :
public void placeAllShips() {
int direction = (int) Math.random()*2 ;
int p1 = 0 ;
int p2 = 0 ;
for(int ships = 1 ; ships < 6 ; ships ++ ) {
p1 = (int)(Math.random()*10);
p2 = (int)(Math.random()*10);
if ( p1 !=0 && p2!= 0 && direction == 0 /* Horizontal Direction*/ ){
for(int i= 0; i < ships ; i ++ ){
while(board[p1][p2+i].hasShip() == true || p2 + i > 10 && p2 - i < 0 ){
randomize(p1,p2) ;
}
}
for(int j = 0 ; j < ships ; j ++ ) {
board[p1][p2+j].setHasShip(true) ;
}
}
else if ( p1 !=0 && p2!= 0 && direction == 1 /*Vertical Direction*/ ){
for(int i= 0; i < ships ; i ++ ){
while(board[p1+i][p2].hasShip() == true || p1 + i > 10 && p1 - i < 0 ){
randomize(p1,p2) ;
}
}
for(int j = 0 ; j < ships ; j ++ ) {
board[p1+j][p2].setHasShip(true) ;
}
}
}
}
public void randomize( int x , int y ) {
//Generates random numbers.
x = (int)Math.random()*10 ;
y = (int)Math.random()*10 ;
}
助けてくれてありがとう !