Java - 圈和叉/井字棋游戏获胜算法不起作用

标签 java arrays if-statement

最近刚开始上大学,我们正在学习如何用 Java 编程。如果我们想接受挑战,有人建议我们尝试用一段损坏的代码制作一个圈和十字游戏。

我设法让程序运行良好,X或O显示在板上,并检查一行或一列中是否有三个相同的符号工作正常,但是由于某种原因,如果它检查以查看如果任何一行上连续三个,它就不起作用,我不明白为什么。我问过 friend ,他们也不明白为什么它不起作用(可能是我错过的一些明显的事情)。

为了检查 X 或 O 是否获胜,我创建了另一个 2D 数组,该数组将 1 放入棋盘上 X 的位置,如果棋盘上的位置是 O,则将 -1 放入。然后检查查看行、列和对角线上是否有 -1 或 1。如果存在 -1,则将 1 添加到 O 的计数器中,如果是 1,则将 1 添加到 X 的计数器中。

在函数末尾,它检查 O 或 X 的计数器是否等于 3,如果是,则返回 true,否则返回 false。

这是代码(大约有 315 行代码,我认为复制并粘贴整个程序会更容易,这样您就可以看到我是否在其他地方出错,向下滚动到底部 checkXOrOHasWon 函数(损坏部分所在):

代码是:-

import java.util.Scanner;

public class NoughtsAndCrosses {

static int count[][] = new int[4][4];
static char board[][] = new char[4][4];
static String playerOneName;
static String playerTwoName;

static int xCoord;
static int yCoord;
static int noOfMoves;
static int checkO = 0;
static int checkX = 0;

static boolean validMove;
static boolean gameHasBeenWon;
static boolean gameHasBeenDrawn;

static char currentSymbol;

static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
    initialiseBoard();
    System.out.println("What is the name of player one? ");
    playerOneName = sc.next();
    System.out.println("What is the name of player two? ");
    playerTwoName = sc.next();

    currentSymbol = 'X';

    // Play a game
    noOfMoves = 0;
    gameHasBeenDrawn = false;
    gameHasBeenWon = false;

    System.out.println();
    displayBoard(board);
    System.out.println("\n" + playerOneName + " starts playing " + currentSymbol);
    System.out.println();

    do { //Play until a player wins or the game is drawn
        do { //Get a valid move

            xCoord = getX();
            yCoord = getY();
            System.out.println("");
            validMove = checkValidMove(xCoord, yCoord, board);
            if (!validMove) {
                System.out.println("Coordinates invalid, please try again");
            } // end if
        } while (!validMove);

        board[xCoord][yCoord] = currentSymbol;
        displayBoard(board);
        gameHasBeenWon = checkXOrOHasWon(board);
        noOfMoves++;

        if (!gameHasBeenWon) {
            // Check if maximum number of allowed moves has been reached
            if (noOfMoves == 9) {
                gameHasBeenDrawn = true;
            } else {
                if (currentSymbol == 'X') {
                    currentSymbol = 'O';
                    System.out.println(playerTwoName + "'s turn");
                } else {
                    currentSymbol = 'X';
                    System.out.println(playerOneName + "'s turn");
                } // end if/else
            } // end if/else
        } // end if
    } while (!gameHasBeenWon && !gameHasBeenDrawn);

    //Display result
    if (gameHasBeenWon) {
        if ('X' == currentSymbol) {
            System.out.println(playerOneName + " congratulations you win!");

        } else {
            System.out.println(playerTwoName + " congratulations you win!");

        }
    } else {
        System.out.println("A draw this time!");
    }
} //end method main

public static void initialiseBoard() {

}

public static void displayBoard(char[][] board) {
    int row;
    int column;
    System.out.println("  | 1 2 3 ");
    System.out.println("--+-------");

    for (row = 1; row <= 3; row++) {
        System.out.print(row + " | ");
        for (column = 1; column <= 3; column++) {
            System.out.print(board[column][row] + " ");
        } // end column
        System.out.println();
    } // end row
} // end method displayBoard

public static int getX() {
    System.out.println("Please enter an X coordinate");
    xCoord = sc.nextInt();

    return xCoord;
}

public static int getY() {
    System.out.println("Please enter an Y coordinate");
    yCoord = sc.nextInt();

    return yCoord;
}

public static boolean checkValidMove(int xCoordinate, int yCoordinate, char[][] board) {
    ////

    if (xCoordinate > board.length || yCoordinate > board.length || xCoordinate == 0 || yCoordinate == 0 || xCoordinate == 4 || yCoordinate == 4) {
        validMove = false;
    } else {
        validMove = true;
    }

    if (currentSymbol == 'O') {
        count[xCoordinate][yCoordinate] = -1;

    } else if (currentSymbol == 'X') {
        count[xCoordinate][yCoordinate] = 1;
    }

    return validMove;

} // end method checkValidMove

//THIS PART HERE IT SEEMS TO BE BROKEN FOR SOME REASON

public static boolean checkXOrOHasWon(char[][] board) {
    //ALL OF THE ROWS DON'T WORK
    //row 1
    for (int i = 1; i < 4; i++) {
        if (count[i][1] == -1) {
            checkO = checkO + 1;
        }
        if (count[i][1] == 1) {
            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }
    if (checkX != 3) {
        checkX = 0;
    }
    //row 2

    for (int i = 1; i < 4; i++) {
        if (count[i][2] == -1) {
            checkO = checkO + 1;
        }
        if (count[i][2] == 1) {
            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }
    if (checkX != 3) {
        checkX = 0;
    }
    //row 3
    for (int i = 1; i < 4; i++) {
        if (count[i][3] == -1) {
            checkO = checkO + 1;
        }
        if (count[i][3] == 1) {
            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }
    if (checkX != 3) {
        checkX = 0;
    }

    //col 1
    for (int i = 1; i < 4; i++) {
        if (count[1][i] == -1) {
            checkO = checkO + 1;

        }
        if (count[1][i] == 1) {

            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }

    if (checkX != 3) {
        checkX = 0;
    }

    //col 2
    for (int i = 1; i < 4; i++) {
        if (count[2][i] == -1) {
            checkO = checkO + 1;

        }
        if (count[2][i] == 1) {

            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }

    if (checkX != 3) {
        checkX = 0;
    }

    //col 3
    for (int i = 1; i < 4; i++) {
        if (count[3][i] == -1) {
            checkO = checkO + 1;

        }
        if (count[3][i] == 1) {

            checkX = checkX + 1;
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }

    if (checkX != 3) {
        checkX = 0;
    }

    //diag 1
    for (int i = 1; i < 4; i++) {
        if (count[i][i] == -1) {
            checkO = checkO + 1;
            break;
        }
        if (count[i][i] == 1) {

            checkX = checkX + 1;
            break;
        }

    }
    if (checkO != 3) {
        checkO = 0;
    }

    if (checkX != 3) {
        checkX = 0;
    }

    //diag 2
    for (int j = 2; j < 5; j++) {
        for (int i = 1; i < 4; i++) {
            if (count[i][j - 1] == -1) {
                checkO = checkO + 1;
                break;
            }
            if (count[i][j - 1] == 1) {

                checkX = checkX + 1;
                break;
            }
        }
    }

    if (checkO != 3) {
        checkO = 0;
    }

    if (checkX != 3) {
        checkX = 0;
    }

    if (checkX == 3 || checkO == 3) {
        gameHasBeenWon = true;
    } else {
        checkO = 0;
        checkX = 0;
        gameHasBeenWon = false;

    }

    return gameHasBeenWon;
  }

 }

最佳答案

if it checks to see if there is three in a row on any line it doesn't work and I cannot figure out why.

该代码仅测试 checkX 或 checkY 是否精确等于 3(或不等于 3),但如果它们在之前的检查中为 1 或 2,则可能会使它们的总数超过 3。

checkXOrOHasWon() 方法最简单的修复是替换所有这些 block (对于 checkX 和 checkO):

if (checkX != 3) {
  checkX = 0;
}

用这种声明代替:

if (checkX == 3) {
  return true;
}

关于Java - 圈和叉/井字棋游戏获胜算法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778703/

相关文章:

java - 如何在 Java 中使用 printf 格式化多行 toString()

java - 写入和读取 ArrayList<model>

javascript - 通过javascript将json转换成树数据

C程序在插入后查找数组中元素的数量并且在插入时不计数

arrays - A.length 和 A.heap-size 有什么区别?

r - 有没有更快的方法来检查列表中的列表是否相等?

java - 替换 BufferedWriter 中的字符

java - java中基数10到基数2,8,16的转换

C do while 循环用户选择

c - 在 C 语言中正确使用 EOF