我写了一个基于试图解决骑士之旅问题的程序。我相信我已经提出了一个合适的解决方案,一切都很好。
我很好奇的一个小问题是使用一小部分代码,在展望未来可能的方块的基础上实现最佳移动。
如果我像这样实现它(implementation1) -
if( moveMade ) // if any move is possible
{
currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
board[ currentRow ][ currentColumn ] = squareCounter;
squareCounter++;
moveMade = false;
}
else
moveMade = true; // if there are no moves possible this is the end
软件会挂起 - 为什么?
如果我做了一个小的无害且看似微不足道的变化,例如这个(implementation2) -
int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );
if( moveMade ) // if any move is possible
{
currentRow += vertical[ temp1 ];
currentColumn += horizontal[ temp1 ];
board[ currentRow ][ currentColumn ] = squareCounter;
squareCounter++;
moveMade = false;
}
else
moveMade = true; // if there are no moves possible this is the end
然后一切都很好,代码将会得出结论。
我正在使用netbeans 7.1编写我的软件并认为它必须与IDE有关,所以我尝试使用'javac'和命令行编译它只是为了找到相同的结果。我不明白为什么我不能在这样的数组参数中调用此方法 - vertical [HERE]或horizontal [HERE]并且具有返回在表达式中使用的结果。我之前没有遇到任何问题就设法编写代码。
以下是调用的方法 -
public static int betterMove( int moverMatrix[], int theHorizontal[], int theVertical[], int accessBoard[][], int newCurrentRow, int newCurrentColumn )
{
int[] equalMatrix = new int [ 8 ]; // records the positions which are equal in value with a (1)
int[] bmValueMatrix = new int[ 8 ]; // holds the numbers taken from accessibility heuristic
int[] finalTable = new int[ 8 ]; // the best move discovered
int best = bestMove( moverMatrix ); // the lowest number in the given array
int startPos = best + 1;
int moveNumber = 0;
int originalCurrentRow = newCurrentRow;
int originalCurrentColumn = newCurrentColumn;
equalMatrix[ best ] = 1; // mark the lowest value position with a 1
initVMatrix( bmValueMatrix );
initVMatrix( finalTable );
for( int i = startPos; i < 8; i++ ) // mark the elements of equal value in equalMatrix with (1)
{
if( moverMatrix[ best ] == moverMatrix[ i ] )
equalMatrix[ i ] = 1;
}
for( int j = 0; j < 8; j++ ) // go through each element of equalMatrix and look forward
{ // for best accessibility heuristic
newCurrentRow = originalCurrentRow;
newCurrentColumn = originalCurrentColumn;
if( equalMatrix[ j ] == 1 )
{
newCurrentRow += theVertical[ j ];
newCurrentColumn += theHorizontal[ j ];
while( moveNumber < 8 )
{
if( newCurrentRow + theVertical[ moveNumber ] >= 0 &&
newCurrentRow + theVertical[ moveNumber ] < 8 )
{
if( newCurrentColumn + theHorizontal[ moveNumber ] >= 0 &&
newCurrentColumn + theHorizontal[ moveNumber ] < 8 )
{
bmValueMatrix[ moveNumber ] = accessBoard[ newCurrentRow + theVertical[ moveNumber ] ]
[ newCurrentColumn + theHorizontal[ moveNumber ] ];
} // end if
} // end if
moveNumber++;
} // end while
moveNumber = 0;
finalTable[ j ] = bestMove( bmValueMatrix );
initVMatrix( bmValueMatrix );
} // end if
} // end for
return bestMove( finalTable );
}
最好的方法在上面的return语句中使用 -
public static int bestMove( int theMoves[] )
{
int theLowest = 10,
idealMove = 0;
for( int i = 0; i < 8; i++ )
{
if( theMoves[ i ] < theLowest )
{
theLowest = theMoves[i];
idealMove = i;
}
}
return idealMove;
}
答案 0 :(得分:4)
currentRow += vertical[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
currentColumn += horizontal[ betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn ) ];
在上述情况下,两个阵列的index value
都不相同。
问题是,当您第二次调用betterMove
方法时,currentRow
值已更改,并且当您在方法中传递currentRow
作为其中一个参数时因此,用于index
数组的horizontal
与在if (moveMade)
之外调用方法时使用的2nd statement
不同。
请注意,区别仅在于vertical
。第一个将与两种方式相同。
所以,这可能是一个问题。
因此,您需要先存储该方法执行的返回值,然后将该返回值用作horizontal
和int temp1 = betterMove( valueMatrix, horizontal, vertical, accessibility, currentRow, currentColumn );
if( moveMade ) // if any move is possible
{
currentRow += vertical[ temp1 ];
currentColumn += horizontal[ temp1 ];
// Rest of the code
}
数组的索引。
index value
现在,在这种情况下,两个数组都具有相同的temp1
- passed parameter
。
此外,如果您多次使用方法调用的返回值,并且该方法正在修改任何{{1}},则应始终调用该方法一次,并存储返回值,并使用它。这将使您免于目睹奇怪的结果。