Java TicTacToe游戏无法正确显示

时间:2012-11-19 23:28:06

标签: java arrays tic-tac-toe

好的,所以我正在写一个Tic Tac Toe游戏。我已经编写了整个程序,但我无法理解为什么它没有正确显示?

import java.util.Scanner;

public class TicTacToeGame {

        //constants for game options
        static final int STANDARD_GAME = 0;
    static final int QUIT = -1;

    //constants for the number of rows and columns in a standard TicTacToe grid
    static final int STANDARD_GRID_ROWS = 3;
    static final int STANDARD_GRID_COLUMNS = 3;

    //two dimensional array to represent a TicTacToe grid
    static char[][]grid = new char[STANDARD_GRID_ROWS][STANDARD_GRID_COLUMNS];

    //Scanner for input
    static Scanner keyboard = new Scanner(System.in);

//***RUN STANDARD GAME******************************************************

    static void runStandardGame() {

        int position = QUIT;
        char playerChar = 'X';
        boolean isGameOver = false;

        fillPosition();

        System.out.println("Choose a position to play.\n");

        displayGrid();

        do {
            System.out.print("Player " + playerChar +
             " (Enter a position or " + QUIT + " to resign): ");
            position = keyboard.nextInt();

            System.out.println("Choose a position to play.\n");

            displayGrid();

            if(isWin()) {
                System.out.print("\nPlayer " + playerChar +"WINS!!!\n");
                isGameOver = true;
            }
            else if (isTie()) {
                System.out.print("\nTIE.\n");
                isGameOver = true;
            }
            else {
                //switch players because one of the players has just played
                //and not won;, so, it is the other player's turn
                if (playerChar == 'X') {
                    playerChar = 'O';
                }
                else{
                    playerChar = 'X';
                }
            }
           }while(!isGameOver && position != QUIT);

    }//end of runStandardGame

//***PLAY*******************************************************************    

    static void play(int cell, char playerChar) {
        //the player has entered a number 1 through 9 for the position they want
        //to play, so, figure out which row and which column of the array this     is
        //For example, if the player wants to play 'X' in position 7, this means
        //the 'X' must go into row 2, column 0 of the array
        int row = 0;
        int column = 0;

        if (cell > 0 && cell <= (STANDARD_GRID_ROWS * STANDARD_GRID_COLUMNS)){
            row = (cell - 1) / STANDARD_GRID_ROWS;
            column = (cell - 1) % STANDARD_GRID_COLUMNS;
            grid[row][column] = playerChar;
        }
    }

//***IS WIN*****************************************************************    

    static boolean isWin() {
        boolean isWin = false;

        if (isRowWin() || isColumnWin() || isDiagonalWin()){
            isWin = true;
        }

        return isWin;
    }

//***IS TIE*****************************************************************

    static boolean isTie() {
        boolean isTie = false;

                if(!isRowWin() || !isColumnWin() || !isDiagonalWin()){

        int count = 9;                   

        for (int row = 0; row < 3; row ++){

            for (int column = 0; column < 3; column ++){
                if (grid[row][column] == 0)
                    count --;
                }
        }

        if (count == 0)
            {
            System.out.println("\nSorry, this game is tied.  Better luck next time!");
            return true;                
            }
        }
        return isTie;
    }

//***IS ROW WIN**********************************************************

    static boolean isRowWin() {

        boolean isRowWin = false;
        int column = 0 ;

        for (int row = 0;row < STANDARD_GRID_ROWS && !isRowWin; row++) {

            //if the three array elements on the same row are equal, 
            //then one of the players has either three 'X's on that
            //row, or three 'O's, and therefore, has won
            if (grid[row][column] == grid[row][column+1] &&
                grid[row][column+1] == grid[row][column+2]){
                    isRowWin = true;
                }
        }

        return isRowWin;

    }

//***IS COLUMN WIN**********************************************************

    static boolean isColumnWin() {

        boolean isColumnWin = false;

        int row = 0 ;

        for (int column = 0;column < STANDARD_GRID_COLUMNS && !isColumnWin;      column++) {

            //if the three array elements on the same row are equal, 
            //then one of the players has either three 'X's on that
            //row, or three 'O's, and therefore, has won
            if (grid[row][column] == grid[row+1][column] &&
                grid[row+1][column] == grid[row+2][column]){
                    isColumnWin = true;
                }
        }

        return isColumnWin;

    }

//***IS DIAGONAL WIN********************************************************

    static boolean isDiagonalWin() {

        boolean isDiagonalWin = false;
        int row = 0;
        int column = 0;
        if(
                (grid[row][column] == grid[row+1][column+1] &&
            grid[row+1][column+1] == grid[row+2][column+2])||
            (grid[row][column+2] == grid[row+1][column+1] &&
            grid[row+1][column+1] == grid[row+2][column])){
                isDiagonalWin = true;
            }

        return isDiagonalWin;

    }

//***GET USER CHOICE********************************************************

    static int getUserChoice() {
        int choice;

        System.out.print("Please enter your choice: ");
        choice = keyboard.nextInt();

        return choice;

    }

//***FILL POSITIONS*********************************************************

    static void fillPosition(){

        char position = '1';

                for (int row = 0; row < STANDARD_GRID_ROWS; row++) {
                    for (int column = 0; column < STANDARD_GRID_COLUMNS; column++) {
                         grid[row][column] = (char) ('1' + row*STANDARD_GRID_COLUMNS + column);
                    }
                }

        }

//***DISPLAY GRID***********************************************************

    static void displayGrid() {



    System.out.println((grid[0][0]) + " | " + (grid[0][1]) + " | " + (grid[0][2]));
    System.out.println("---------");
    System.out.println((grid[1][0]) + " | " + (grid[1][1]) + " | " + (grid[1][2]));
    System.out.println("---------");
    System.out.println((grid[2][0]) + " | " + (grid[2][1]) + " | " + (grid[2][2]));


    }

//***END********************************************************************

}//end of class

在游戏中,我希望它将棋盘显示为:

 1 | 2 | 3
-----------
 4 | 5 | 6
-----------
 7 | 8 | 9

无论我选择什么,或者在哪里,它都会反复要求我做出选择......即使我会刻意赢得一场胜利,或者让它们结合起来让它结束,但它仍然是无止境的...是的逻辑错误?

另外,我删除了一些我知道正确的代码,因此更容易阅读和理解。

4 个答案:

答案 0 :(得分:3)

fillPosition中的do-while循环只执行一次。在条件row<4 && column<4 && position<10中,布尔position<10最初为false。因为position char

position < 10

被解释为

(int)position < 10

(int)position = (int)'2' = 49(第一次循环迭代后position'2'),且不小于10,因此此条件为false,表示整个循环条件也是false - 因此循环结束。

因此,'1'被正确放置,但没有其他任何东西(它是一个do-while循环,所以它至少执行一次,无论其条件如何)。

答案 1 :(得分:2)

你试图比较一个char(列)和10是一个数字,字符'1'的ascii数字比那时大,所以循环正在破坏

答案 2 :(得分:1)

您的fillPosition()方法不正确。看起来应该是这样的:

    static void fillPosition(){
        for (int row = 0; row < STANDARD_GRID_ROWS; row++) {
            for (int column = 0; column < STANDARD_GRID_COLUMNS; column++) {
                grid[row][column] = (char) ('1' + row*STANDARD_GRID_COLUMNS + column);
            }
        }

    }

答案 3 :(得分:1)

fillPosition do-loop的退出条件有误......

我还建议不要使用字符算法,最好使用int和Interger.toString(int),但那只是我。

循环使用复合for循环来填充grid数组,用于行的外部开头和用于列的内部,例如