java - 井字游戏。工作不正常

标签 java

伙计们,我已经从头开始完成 TicTacToe 游戏的代码,如果任何玩家在一行中水平、垂直或对角线获得相同的值,它就可以正常工作。但是,如果比赛是平局,例如:

X O X 
O X X 
O X O 

Player #2 (O) enter the row and column numbers: 

尽管我在 count 中设置了限制,但代码仍会要求用户输入。用户必须输入总共 9 个输入字符,因为 TicTacToe 中有 9 个框。每次用户输入 rowcol 数字时,count 都会增加 1。我的 while-loop 看起来像这样:

while(!(count > 9)){
.....................
}

我的 while 循环有什么问题吗?

import java.util.Scanner;
import java.util.*;
public class TicTacToe {

    public static final char[][] theBoard = new char[4][4];
    static Scanner kbd = new Scanner(System.in);

    public static void main(String[] args){


        System.out.println("Hello! Welcome to TicTacToe!");
        System.out.println("Player #1 is X! \nPlayer #2 is Y!");
        System.out.println();
        getBoard();
        System.out.println();
        playGame();
    }

    public static void getBoard(){
        for(int i = 1; i < theBoard.length; i++){
            for(int j = 1; j < theBoard[i].length; j++){
                theBoard[i][j] = '_';
                System.out.print(theBoard[i][j] + " ");
            }
            System.out.println();
        }
    }


    public static void playGame(){
        int row = 0;
        int col = 0;
        boolean check = false;
        int count = 0;

        while(!(count > 9)){

            System.out.println();
            System.out.println("Player #1 (X) enter the row and column numbers: ");
            row = kbd.nextInt();
            col = kbd.nextInt();
            System.out.println();

            count++;
            if (row < 0 || col < 0 || row > theBoard.length
                    || col > theBoard.length) {
                throw new RuntimeException("The numbers should be in range 1-3");
            }

            else if (theBoard[row][col] != '_') {
                throw new RuntimeException("The space already filled!");
            }

            else {
                theBoard[row][col] = 'X';
                for (int i = 1; i < theBoard.length; i++) {
                    for (int j = 1; j < theBoard.length; j++) {
                        System.out.print(theBoard[i][j] + " ");
                    }
                    System.out.println();
                }
            }
            check = checkIfWin();
            if(check == true){
                break;
            }


            System.out.println();
            System.out.println("Player #2 (O) enter the row and column numbers: ");
            row = kbd.nextInt();
            col = kbd.nextInt();
            System.out.println();
            count++;

            if (row < 0 || col < 0 || row > theBoard.length
                    || col > theBoard.length) {
                throw new RuntimeException("The numbers should be in range 1-3");
            }

            else if (theBoard[row][col] != '_') {
                throw new RuntimeException("The space already filled!");
            }

            else {
                theBoard[row][col] = 'O';
                for (int i = 1; i < theBoard.length; i++) {
                    for (int j = 1; j < theBoard.length; j++) {
                        System.out.print(theBoard[i][j] + " ");
                    }
                    System.out.println();
                }
            }
            check = checkIfWin();
            if(check == true){
                break;
            }

        }
        System.out.println("The game is tie!");
    }

    private static boolean checkIfWin(){
        boolean result = false;
        int countX = 0;
        int countO = 0;

        //Check 1st line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[1][i] == 'X'){
                countX++;
            }
            else if(theBoard[1][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 2nd line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[2][i] == 'X'){
                countX++;
            }
            else if(theBoard[2][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 3d line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[3][i] == 'X'){
                countX++;
            }
            else if(theBoard[3][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 1s column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][1] == 'X'){
                countX++;
            }
            else if(theBoard[i][1] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 2nd column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][2] == 'X'){
                countX++;
            }
            else if(theBoard[i][2] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 3d column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][3] == 'X'){
                countX++;
            }
            else if(theBoard[i][3] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }

        //Check first diagonal
        if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
            result = true;
            System.out.println("The Player #1 (X) Wins!");
        }
        else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
            result = true;
            System.out.println("The Player #2 (O) Wins!");
        }


        // Check first diagonal
        if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
            result = true;
            System.out.println("The Player #1 (X) Wins!");
        } else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
            result = true;
            System.out.println("The Player #2 (O) Wins!");
        }

        return result;
    }
}

最佳答案

你的问题源于这样一个事实,即你给了每个玩家离开的机会。您需要在循环中间添加另一个检查。在 Tic Tac Toe 中,并非每个玩家的轮次都相等,因此您是否在每个人都离开后检查,而不是在游戏完成时检查。

你需要在这里再检查一次(在我的代码中是第 67 行):

            check = checkIfWin();
            if((check == true) || (count >= 8)){
                break;
            }

这个程序有效:

package tictactoe;

import java.util.Scanner;
import java.util.*;

public class TicTacToe {

    public static final char[][] theBoard = new char[4][4];
    static Scanner kbd = new Scanner(System.in);

    public static void main(String[] args){


        System.out.println("Hello! Welcome to TicTacToe!");
        System.out.println("Player #1 is X! \nPlayer #2 is Y!");
        System.out.println();
        getBoard();
        System.out.println();
        playGame();

    }

    public static void getBoard(){
        for(int i = 1; i < theBoard.length; i++){
            for(int j = 1; j < theBoard[i].length; j++){
                theBoard[i][j] = '_';
                System.out.print(theBoard[i][j] + " ");
            }
            System.out.println();
        }
    }


    public static void playGame(){
        int row = 0;
        int col = 0;
        boolean check = false;
        int count = 0;

        //change this 
        while(count <= 8){
            count++;
            System.out.println();
            System.out.println("Player #1 (X) enter the row and column numbers: ");
            row = kbd.nextInt();
            col = kbd.nextInt();
            System.out.println();


            if (row < 0 || col < 0 || row > theBoard.length
                    || col > theBoard.length) {
                throw new RuntimeException("The numbers should be in range 1-3");
            }

            else if (theBoard[row][col] != '_') {
                throw new RuntimeException("The space already filled!");
            }

            else {
                theBoard[row][col] = 'X';
                for (int i = 1; i < theBoard.length; i++) {
                    for (int j = 1; j < theBoard.length; j++) {
                        System.out.print(theBoard[i][j] + " ");
                    }
                    System.out.println();
                }
            }
            check = checkIfWin();
            //change this
            if((check == true) || (count >= 8)){
                break;
            }


            System.out.println();
            System.out.println("Player #2 (O) enter the row and column numbers: ");
            row = kbd.nextInt();
            col = kbd.nextInt();
            System.out.println();
            count++;

            if (row < 0 || col < 0 || row > theBoard.length
                    || col > theBoard.length) {
                throw new RuntimeException("The numbers should be in range 1-3");
            }

            else if (theBoard[row][col] != '_') {
                throw new RuntimeException("The space already filled!");
            }

            else {
                theBoard[row][col] = 'O';
                for (int i = 1; i < theBoard.length; i++) {
                    for (int j = 1; j < theBoard.length; j++) {
                        System.out.print(theBoard[i][j] + " ");
                    }
                    System.out.println();
                }
            }
            check = checkIfWin();
            if(check == true){
                break;
            }

        }
        System.out.println("The game is tie!");
    }

    private static boolean checkIfWin(){
        boolean result = false;
        int countX = 0;
        int countO = 0;

        //Check 1st line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[1][i] == 'X'){
                countX++;
            }
            else if(theBoard[1][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 2nd line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[2][i] == 'X'){
                countX++;
            }
            else if(theBoard[2][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 3d line
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[3][i] == 'X'){
                countX++;
            }
            else if(theBoard[3][i] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 1s column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][1] == 'X'){
                countX++;
            }
            else if(theBoard[i][1] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 2nd column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][2] == 'X'){
                countX++;
            }
            else if(theBoard[i][2] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }
        countX = 0;
        countO = 0;

        //Check 3d column
        for(int i = 1; i < theBoard.length; i++){
            if(theBoard[i][3] == 'X'){
                countX++;
            }
            else if(theBoard[i][3] == 'O'){
                countO++;
            }
            if(countX == 3){
                result = true;
                System.out.println("The Player #1 (X) Wins!");
                break;
            }
            else if(countO == 3){
                result = true;
                System.out.println("The Player #2 (O) Wins!");
                break;
            }
        }

        //Check first diagonal
        if(theBoard[1][1] == 'X' && theBoard[2][2] == 'X' && theBoard[3][3] == 'X'){
            result = true;
            System.out.println("The Player #1 (X) Wins!");
        }
        else if(theBoard[1][1] == 'O' && theBoard[2][2] == 'O' && theBoard[3][3] == 'O'){
            result = true;
            System.out.println("The Player #2 (O) Wins!");
        }


        // Check first diagonal
        if (theBoard[1][3] == 'X' && theBoard[2][2] == 'X'&& theBoard[3][1] == 'X'){
            result = true;
            System.out.println("The Player #1 (X) Wins!");
        } else if (theBoard[1][3] == 'O' && theBoard[2][2] == 'O'&& theBoard[3][1] == 'O') {
            result = true;
            System.out.println("The Player #2 (O) Wins!");
        }

        return result;
    }
}

附带说明一下,我是通过使用调试器并单步执行程序来发现这个错误的。

关于java - 井字游戏。工作不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443604/

相关文章:

java - 尽量不要混合使用 Swing 和 AWT

java - 解析xml stax文件中的特殊字符

java - FileReader 和 ActionListener 之间的同步问题,在我给他任何勇气之前,FileReader 给出了空指针;

java - Docker 容器中的 AWS SAM CLI 导致 ClassNotFoundException

java - 使用 Java var 关键字的缺点

java - 资源 : Parallelism in Java for OpenGL realtime applications

java - Android 使用不同的工具

java - 使用 UCanAccess 插入行时更改自动编号值

java - 对于不可能的情况应该抛出什么错误?

java - Kotlin ArrayList<HashSet<data class>> .reduce 函数不适用于 ArrayList 的子集