您刚刚尝试了一个TicTacToe项目并且遇到了错误。我的错误与检查win解决方案特别是对角线有关。
我需要什么: 创建嵌套循环以对角循环数组,然后递增它,使其在对角线下方或大小扫描,直到最终扫描整个数组。
我做了什么: 我试图创建一个嵌套的for循环,循环遍历行并将其添加到计数器直到行的末尾,然后检查计数器是否等于内联(获胜所需的行数)。我相信它适用于行和列。
问题: 但是对于对角线,我得到一个超出界限的数组,我认为这是因为我的 a 或 b 被添加到 i 这可能是gameBoard [3] [4]在谈到3x3游戏板时。
尝试解决: 我尝试了一个解决方案,你可以看到奇怪的放置循环与j。所以我只会去j而不是超过数组限制。
我想知道这背后的逻辑是否有效?
很抱歉,如果代码很乱,特别是添加的for循环包含j
/*
* Method winner will determine if the symbol (X or O) wins
*
* @param symbol will be either X or O
* @return will return true if the symbol has won from any of the methods
*/
public boolean winner(char symbol) {
int counter = 0;
/* Scan from ROWS for any symbols inline to win */
for (int i = 0; i < gameBoard.length; i++) { // loop through the rows
for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns
if (gameBoard[i][j] == symbol) {
counter++;
}
if (gameBoard[i][j] != symbol) { // If the next one in the row is not equal then reset counter to 0
counter = 0;
}
if (counter == inline) { // Counter will only equal inline if there is amount of inliine in a row
return true;
}
}
}
/* Scan and search for winning conditions in COLUMNS */
for (int i = 0; i < gameBoard.length; i++) { // loop through the rows
for (int j = 0; j < gameBoard.length; j++) { // Loop through the columns
if (gameBoard[j][i] == symbol) {
counter++;
}
if (gameBoard[j][i] != symbol) { // Reset counter to 0 if not equal to symbol
counter = 0;
}
if (counter == inline) { // If counter reached amount of inline then it must have had amount of inline in a row to win
return true;
}
}
}
/* Scan for RIGHT DIAGONALS for winning conditions */
// a shifts the position of diagonal to the right by one
// after diagonally looping through the board
for (int a = 0; a < gameBoard.length; a++) {
// i loops diagonally through the board
for (int j = gameBoard.length; j < 0; j--) {
for (int i = 0; i < j; i++) {
if (gameBoard[i][i + a] == symbol) {
counter++;
}
if (gameBoard[i][i + a] != symbol) {
counter = 0;
}
if (counter == inline) {
return true;
}
}
}
}
// b shifts the position of the diagonal down by one
for (int b = 1; b < gameBoard.length; b++) {
for (int j = gameBoard.length - 1; j < 0; j--)
// i loops diagonally through the board
for (int i = 0; i < j; i++) {
if (gameBoard[i + b][i] == symbol) {
counter++;
}
if (gameBoard[i + b][i] != symbol) {
counter = 0;
}
if (counter == inline) {
return true;
}
}
}
/* Scan for LEFT DIAGONALS for winning conditions */
// a shifts the position of diagonal to the left by one
for (int a = gameBoard.length; a >= 0; a--) {
for (int j = gameBoard.length; j < 0; j--) {
// i loops diagonally through the board
for (int i = 0; i < j; i++) {
if (gameBoard[i][a - i] == symbol) {
counter++;
}
if (gameBoard[i][a - i] != symbol) {
counter = 0;
}
if (counter == inline) {
return true;
}
}
}
}
// b shifts the position of the diagonal down by one
for (int b = 0; b < gameBoard.length; b++) {
for (int j = gameBoard.length - 1; j < 0; j--) {
// i loops diagonally in the left direction of through the board
for (int i = 0; i < j; i++) {
if (gameBoard[i + b][gameBoard.length - i] == symbol) {
counter++;
}
if (gameBoard[i + b][gameBoard.length - i] != symbol) {
counter = 0;
}
if (counter == inline) {
return true;
}
}
}
}
return false; // If it reaches here then no one has won yet and the game is ongoing
}
答案 0 :(得分:1)
据我所知,您必须获得Array Index Out Of Bounds Exception
。我假设你试图实现经典的tic-tac-toe,所以我们正在处理3x3矩阵。以下是您的游戏板编入索引的方式:
[0.0] [1.0] [2.0]
[0.1] [1.1] [2.1]
[0.2] [1.2] [2.2]
这就是你的Right Diagonals循环中发生的事情:
int a
增量0 - &gt; 2
int j
减量2 - &gt; 0
int i
增量0 - &gt; 2
所以你的循环是这样的:
[0.0 + 0] - &gt; i ++ [1.1 + 0] - &gt; i ++ [2.2 + 0] j--
[0.0 + 0] - &gt; i ++ [1.1 + 0] j--
[0.0 + 0] a ++
[0.0 + 1] - &gt; i ++ [1.1 + 1] - &gt; i ++ [2.2 + 1] j-- &lt; - 这里你离开了数组。
另外,在通过主对角线检查之后,你会经过[0.0] [1.1],这根本不是对角线,你已经在行的循环中做了这个。甚至不需要通过底部对角线移动([0.1] [1.2]),因为您之前在循环中已经这样做了。因此,检查[0.0] [1.1] [2.2]将对您有用。
我认为这是检查胜利条件的无效方法。只需存储找到的元素的位置,就可以摆脱3个循环。
答案 1 :(得分:0)
抱歉无法获得评论的格式,所以我会发布我在这里的内容
/* Scan for RIGHT DIAGONALS for winning conditions */
int j = gameBoard.length
for (int a = 0; a < gameBoard.length; a++) {
// i loops diagonally through the board
for (int i = 0; i < j; i++) {
if (gameBoard[i][i + a] == symbol) {
counter++;
}
if (gameBoard[i][i + a] != symbol) {
counter = 0;
}
if (counter == inline) {
return true;
}
} j--; // Incrementing after the i for loop.
}
output:
a:0 i:0 j:3
[0.0+0] --> i++ [1.1+0] --> i++ [2.2+0] /*end for loop. i:2 j:3 a: 0 */
j-- a++
[0.0+1] --> i++ [1.1+1] /*end for loop. i:1 j:2 a: 1*/
j-- a++
[0.0+2] /* end for loop. i:0 j: 1 a:2 */
并且在检查对角线时数组保持在边界内。所以我认为思考可以在更大的范围内发挥作用。