0

我不知道我在这里做错了什么。谁能告诉我我的checkRow代码checkWinnerisSquareFree方法有什么问题?

这是我的代码:

public class TicTacToe
{
/**
* intance variables to hold the data's state for the game.
*/
    public static final int GRIDSIZE = 3;
    public static final char BLANK = ' ';
    public static final char TIE = 'T';
    public static final char NOUGHT = 'O';
    public static final char CROSS = 'X';

    private char[][] grid;
    private char WhoseTurn;

    /**
     * Construct a tic tac toe grid ready to play a new game.
     * The game grid should be GRIDSIZE by GRIDSIZE spaces
     * all containing the BLANK character.
     * Initially, the starting player is not decided,
     * indicated by setting whoseturn as BLANK.
     */
    public TicTacToe()
    {

       this.WhoseTurn = BLANK;
       this.grid = new char[GRIDSIZE][GRIDSIZE];
      for (int r = 0; r < grid.length; r++) 
        {
            for ( int c = 0; c < grid[r].length; c++)
            {
            this.grid[r][c] = BLANK;
        }

    }
}
   /**
* Reset the tic tac toe game ready to play again.
* Conditions for play are the same as for the constructor.
*/
public void newGame()
{  
   char[][] boardToClear = getGrid();
   final int sizeOfBoard = grid.length;
   for ( int row = 0; row < grid.length; row++)
   {
       for ( int col = 0; col < grid.length; col++)
       {
           grid[row][col] = BLANK;
        }
    }
}
 public char[][] getGrid()
 {
    int gridLen = grid.length;
    char[][] gridCopy = new char[gridLen][];
    for ( int r = 0; r < gridCopy.length; r++)
    {
        gridCopy[r] = new char[gridCopy.length];
        for ( int c = 0; c < gridCopy.length; c++)
        {
            gridCopy[r][c] = grid[r][c];
        }
    }
return gridCopy;
}

    public char getWhoseTurn()
    {
        return WhoseTurn;
    }

/**
* printGrid() displays the current state of the game grid 
* on the console for debugging.
* It uses the form feed character \u000C to clear the console before
* printing the current grid.
*/

    private void printGrid() 
      {
        System.out.print('\u000C'); // clear the console window
        for (int x = 0; x < GRIDSIZE-1; x++) { 
            System.out.print(grid[x][0] + "|" +
                       grid[x][1] + "|" + grid[x][2]);
            System.out.println("\n-----"); //
            System.out.print(grid[GRIDSIZE-1][0] + "|" + 
            grid[GRIDSIZE-1][1] + "|" + 
            grid[GRIDSIZE-1][2]);
    }
        }
        // Now print last row (with no bottom edge)

        private boolean checkIfGridFull()
        {
            char[][] board = getGrid();
            int size = grid.length;
            for ( int row = 0; row < size; row++)
            {
                for ( int col = 0; col < board[row].length; col++)
                {
                    if ( grid[row][col] == BLANK)
                    {
                        return false;
                    }
                }
            }
            return true;
        }




        public boolean move(char player, int row, int col)
        {


          char[][] boardToPlay = getGrid();
            int size = grid.length;
            char x = player;

         if ( (player == NOUGHT) || ( player == CROSS))
           {
                if ( (x == WhoseTurn) || (WhoseTurn == BLANK))
               {
                   if ((checkIfGridFull() == false) && ( boardToPlay[row][col] == BLANK))
                   {

                       if( (row < size) && ( col < size))
                       {

                      boardToPlay[row][col] = player;
                      if ( player == CROSS)
            {
                WhoseTurn = NOUGHT;
            }
            if ( player == NOUGHT)
            {
                WhoseTurn = CROSS;
            }

                       return true;
                    }
                }
            }
        }





        return false;
    }
    public boolean isSquareFree( int row, int col)
        {


            if ((grid[row][col] == BLANK))
            {
                return true;
            }
            return false
            ;
        }

        public char checkWinner()
        {
            int countNought;
            int countCross ;
            int size = grid.length;

            for ( int row = 0; row < size; row++)
            {
                countNought = 0;
                countCross = 0;

                for ( int col = 0; col < size; col++)
                {
                    if ( grid[row][col] == CROSS)
                    {
                        countCross++;
                    }
                    if ( grid[row][col] == NOUGHT)
                    {
                        countNought++;
                    }
                    if ( countNought == size)
                    {
                        return NOUGHT;
                    }
                    if ( countCross == size)
                    {
                        return CROSS;
                    }
                }
            }

            return BLANK;
        }
        }
4

2 回答 2

0

checkWinner() 是错误的,你没有检查对角线,只写八的获胜组合

于 2011-08-22T12:26:21.513 回答
0

在这样的项目中跟踪错误的一种好方法是使用打印行语句。试试这个:在您认为有问题的地方附近放置一条打印线,并打印出一些局部变量的值。如果您看到不应该看到的值,那么您就知道您的数据在代码的早期遇到了一些麻烦。在这种情况下,将打印行移回流程中,然后重试。继续这样做,直到您注意到值从好到坏(或从坏到好):此时您已经找到了包含错误的代码。

完成此操作后,您通常只需要考虑一个小功能,并且更容易理解您做错了什么。如果您仍然找不到问题,请在此处发布代码片段,有人可能会帮助您。

于 2011-05-12T03:28:43.330 回答