我正在撰写获胜者算法的一部分。这个连续4的垂直胜利,是的,有大量的裁员和坏事,至少我是这么认为的,如果你愿意的话,让我知道哪些线路不好,并提出建议,放置这些请在评论部分。 BTW,标签是否必要?我不知道。我把它扔在那里是因为我觉得它可能有所帮助。
我遇到了运行部分检查程序的for循环问题。所以我决定用一份印刷声明来看看发生了什么。这就是我得到的:
2 /////// 1
3 /////// 2
4 /////// 1
请注意,电路板上只有三块垂直排列且颜色相同的部件。您可以在下面看到print语句结构:
public String verticalWin(RoundButton[][] useArray){
//int a = 1;
for(int row = 5; row > 2; row--){
for(int col = 0; col < 7; col++){
if(nextTileExists(row - 1, col) && !useArray[row][col].getBackground().equals(Color.YELLOW))
if(useArray[row][col].getBackground().equals(useArray[row - 1][col].getBackground())){
comeHereLabel:
for(int a = 1; a < 4; a++){//THIS LOOP
if(nextTileExists(row - a, col))
if(useArray[row - a][col].getBackground().equals(useArray[row][col].getBackground())){
pieceWin++;
System.out.println(pieceWin + " /////// " + a);
if(pieceWin == 4)
if(useArray[row][col].getBackground().equals(Color.RED))
return "red";
else
return "black";
}
else
break comeHereLabel;
}
}
}
}
return "A";
}
在方法的顶部,您可以看到我已注释掉int a = 1;
如果我取消注释该行,并转到标记为//THIS LOOP
的循环并将其更改为for(; a < 4; a++)
已打印代码的语句和操作完全改变。我从新印刷中获得的是:
2 /////// 1
3 /////// 2
4 /////// 3
现在板上实际上有4件。 造成这种奇怪的原因是什么?
如果您需要更多代码或其他任何说法。
答案 0 :(得分:0)
我强烈建议将程序拆分为函数。由于您只是在学习,让我们面对现实,Connect Four不需要一流的性能,我建议:
public enum Result {
kNone , kRed , kBlue
}
public RoundButton[] copyColumn ( RoundButton[][] board , int column )
{
// return a new array with the items from the specified column
}
public RoundButton[] copyDiagonal ( RoundButton[][] board , int startRow,int startCol , int slope )
{
// return a new array with items in the diagonal start at the specified location, either going up to the right or down to the right
}
public Result evaluate ( RoundButton[] array )
{
// return whether or not any winners were found in this array
}
public Result evaluateBoard ( RoundButton[][] board )
{
// Iterate through the columns & rows; if a winner is found: return it.
for ( int row=0 ; row<6 ; ++row ) {
Result r = evaluate ( board[row] ) ;
if ( r != kNone ) return r ;
}
for ( int col=0 ; col<7 ; ++col ) {
RoundButton[] column = copyColumn ( board , col ) ;
Result r = evaluate ( column ) ;
if ( r != kNone ) return r ;
}
// Iterate through the diagonals; if a winner is found: return it.
// Assuming board[0][0] is the bottom left...
// Only starting at rows [0,2] going up can have a winner
for ( int row=0 ; row<=2 ; ++row ) {
RoundButton[] diag = copyDiagonal ( board , row,0 , 1 ) ;
Result r = evaluate ( diag ) ;
if ( r != kNone ) return r ;
}
// Only starting at rows [3,5] going down can have a winner
for ( int row=3 ; row<=5 ; ++row ) {
RoundButton[] drag = copyDiagonal ( board , row,0 , -1 ) ;
Result r = evaluate ( diag ) ;
if ( r != kNone ) return r ;
}
// Only starting at cols [0,3] going up can have a winner
// ... but we'll cover 0 in the rows section first
for ( int col=1 ; col<=3 ; ++col ) {
RoundButton[] drag = copyDiagonal ( board , 0,col , 1 ) ;
Result r = evaluate ( diag ) ;
if ( r != kNone ) return r ;
}
// Only starting at cols [0,3] going down can have a winner
// ... but we'll cover 0 in the rows section first
for ( int col=1 ; col<=3 ; ++col ) {
RoundButton[] drag = copyDiagonal ( board , 5,col , -1 ) ;
Result r = evaluate ( diag ) ;
if ( r != kNone ) return r ;
}
return kNone ;
}
答案 1 :(得分:-1)
当for
循环开始时,它会执行第一个子句(如果有的话)。
for(int a = 1; a < 4; a++) {
}
for(; a < 10; a++) {
System.out.println(a);
}
此代码输出的第一件事是4
,因为这将是完成第一个循环后最后一个值a
。
for(a = 1; a < 20; a++) {
System.out.println(a);
}
如果您随后执行此操作,则第一个输出将为1
,因为a
的值在迭代之前会重置。