function move(x,y,endx,endy,copyArray){
alert(copyArray);
if (copyArray[x][y] == 0) {
if(x===endx && y===endy){
alert(x===endx);
alert(y===endy);
return true;
}
copyArray[x][y] = 1;
move(x, y + 1,endx,endy,copyArray); // 横向
move(x, y - 1,endx,endy,copyArray);
move(x - 1, y,endx,endy,copyArray); // 竖向
move(x + 1, y,endx,endy,copyArray);
}
return false;
}其中 if(x===endx && y===endy){
alert(x===endx);
alert(y===endy);
return true;
明明都现实是true了,应该返回true,这个方法结束了,为什么还是会执行下去? 最后居然还是返回false
alert(copyArray);
if (copyArray[x][y] == 0) {
if(x===endx && y===endy){
alert(x===endx);
alert(y===endy);
return true;
}
copyArray[x][y] = 1;
move(x, y + 1,endx,endy,copyArray); // 横向
move(x, y - 1,endx,endy,copyArray);
move(x - 1, y,endx,endy,copyArray); // 竖向
move(x + 1, y,endx,endy,copyArray);
}
return false;
}其中 if(x===endx && y===endy){
alert(x===endx);
alert(y===endy);
return true;
明明都现实是true了,应该返回true,这个方法结束了,为什么还是会执行下去? 最后居然还是返回false
alert(copyArray);
if (copyArray[x][y] == 0) {
if (x === endx && y === endy) {
alert(x === endx);
alert(y === endy);
return true; //如果返回true,说明copyArray[x][y]值为0,并且满足(x === endx && y === endy)条件
}
copyArray[x][y] = 1;
move(x, y + 1, endx, endy, copyArray); // 横向
move(x, y - 1, endx, endy, copyArray);
move(x - 1, y, endx, endy, copyArray); // 竖向
move(x + 1, y, endx, endy, copyArray);
}
return false; //如果返回的是false,那就说明copyArray[x][y]的值不是0
}
这个函数第一次为false也没事啊~ 会继续调用下面的move函数的