gpt4 book ai didi

java - 井字棋盘,WIN方法错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:27 25 4
gpt4 key购买 nike

您好,您刚刚尝试了一个 TicTacToe 项目,但遇到了一个错误。我的错误与检查 win 解决方案特别是对角线有关。

我需要什么:创建嵌套循环以沿对角线向下循环数组,然后递增它,以便它沿对角线扫描小于或等于该大小,直到最终扫描整个数组。

我做了什么:我尝试制作一个嵌套的 for 循环,循环遍历行并将其添加到计数器直到行末,然后检查计数器是否等于内联(获胜所需的行中的数量)。我相信它适用于行和列。

问题:但是对于对角线,我得到一个数组越界异常,我认为这是因为我的 ab 被添加到 i 这可能是 gameBoard [3][4] 谈到 3x3 游戏板时。

尝试解决:我尝试了一个解决方案,您可以看到它是带有 j 的奇怪放置的 for 循环。这样我就只会去 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

}

最佳答案

据我在您的代码中看到的,您必须获得Array Index Out Of Bounds Exception。我假设您尝试实现经典的井字游戏,所以我们正在处理 3x3 矩阵。以下是您的游戏板的索引方式:

[0.0] [1.0] [2.0]

[0.1] [1.1] [2.1]

[0.2] [1.2] [2.2]

这就是在使用右对角线的循环中发生的情况:
int a 递增 0 --> 2
int j 递减 2 --> 0
int i 递增 0 --> 2


所以你的循环是这样的:
[0.0+0] --> i++ [1.1+0] --> i++ [2.2+0] j--
[0.0+0] --> i++ [1.1+0] j--
[0.0+0]一个++
[0.0+1] --> i++ [1.1+1] --> i++ [2.2+1] j-- <--到这里就退出Array了。


此外,在检查完主对角线后,你会经过 [0.0] [1.1],这根本不是对角线,你已经在行的循环中这样做了。甚至不需要通过底部对角线移动 ([0.1][1.2]),因为您之前已经在循环中这样做过。因此,检查 [0.0] [1.1] [2.2] 将为您工作。


我认为这是检查获胜条件的无效方法。您可以摆脱 3 个循环,只需存储找到的元素的位置。

关于java - 井字棋盘,WIN方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26436235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com