【发布时间】:2014-10-18 04:11:41
【问题描述】:
您好,刚刚尝试了一个井字游戏项目,但遇到了一个错误。我的错误与检查获胜解决方案特别是对角线有关。
我需要什么: 创建嵌套循环以沿对角线向下循环数组,然后将其递增,使其沿对角线向下扫描或扫描到该大小,直到最终扫描整个数组。
我做了什么: 我尝试创建一个嵌套的 for 循环,该循环将遍历行并将其添加到计数器直到行的末尾,然后检查计数器是否等于内联(赢得所需的一行中的数量)。我相信它适用于行和列。
问题: 但是对于对角线,我得到一个数组越界异常,我认为这是因为我的 a 或 b 被添加到可能是 gameBoard 的 i [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
}
【问题讨论】:
-
+1,在编辑之前对您的外部安排非常满意!
标签: java arrays tic-tac-toe