我想做一個checkWin方法返回true,如果有一個勝利的組合。我正在使用嵌套for循環。棋盤大小和勝率大小在遊戲類中預定義,並在初始化2D陣列和檢查勝利時傳遞給棋盤類。我希望能夠在任何尺寸的棋盤上使用此檢查方法獲得任何尺寸的組合。這樣我就可以重複使用多種遊戲的方法。 Tic Tac Toe,Connect Four,Gomoku等測試贏得二維數組的字符與預定義數組和贏得長度 - Java
爲什麼它不能正常工作?我很確定我的if語句是錯誤的。我會在一些測試中獲勝,但不會在其他測試中獲勝。而且我從來沒有在西北對角線贏得勝利,只有在一個東北對角線上。我可以很容易地測試某些長度,但是我無法測試任何尺寸的長度的任何尺寸的棋盤。
/**
* Tests board initialized with size parameter in class constructor.
* Utilizes nested loops to check all rows, columns, and diagonals for win.
*
* @param WIN_SIZE, The size to check for valid win.
* @return, Returns true if there is a winner.
*/
public boolean checkWin(int WIN_SIZE)
{
//Test columns
//Loop through columns
for (int col = 0; col < size; col++)
{
//Loop through rows
for (int row = 0; row < size; row++)
{
//Loop through win length
for (int counter = 0; counter < WIN_SIZE; counter++)
{
try
{
if (board[counter][col] == '_' || board[counter][col] != board[counter+1][col])
{
//Break if spot is blank or if next index does not match.
break;
}
if (counter == WIN_SIZE - 1)
{
//Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
return true;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
}
//Test rows
//Loop through rows
for (int row = 0; row < size; row++)
{
//Loop through columns
for (int col = 0; col < size; col++)
{
//Loop through win length
for (int counter = 0; counter < WIN_SIZE; counter++)
{
try
{
if (board[row][counter+col] == '_' || board[row][counter+col] != board[row][counter+col+1])
{
//Break if spot is blank or if next index does not match
break;
}
if (counter == WIN_SIZE - 1)
{
//Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
return true;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
}
//Test Diagonals North-West
//Loop through column
for (int col = 0; col < size; col++)
{
//Loop through rows
for (int row = 0; row < size; row++)
{
//Loop through length of a valid win
for (int counter = 0; counter < WIN_SIZE; counter++)
{
try
{
if (board[row+counter][counter] == '_' || board[row+counter][counter] != board[row+counter+1][counter+1])
{
//Break if spot is blank or if next index does not match
break;
}
else if (counter == WIN_SIZE - 1)
{
//Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
return true;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
}
//Test Diagonals North-West
//Loop through rows
for (int col = 0; col < size; col++)
{
//Loop through columns
for (int row = 0; row < size; row++)
{
//Loop through length of a valid win
for (int counter = 0; counter < WIN_SIZE; counter++)
{
try
{
if (board[row-counter][counter] == '_' || board[row-counter][counter] != board[row-counter-1][counter+1])
{
//Break if spot is blank or if next index does not match
break;
}
else if (counter == WIN_SIZE - 1)
{
//Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
return true;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
}
//Returns false if no winner is found
return false;
}
我這麼把功課標籤上的這隻小狗 – Bohemian 2012-01-29 19:34:51