TicTacToe阵列。获胜者方法并且是免费的

时间:2011-05-12 03:19:37

标签: arrays tic-tac-toe

我不知道我在这里做错了什么。 任何人都可以通过checkRowcheckWinner方法告诉我isSquareFree代码有什么问题吗?

这是我的代码:

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;
        }
        }

2 个答案:

答案 0 :(得分:0)

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

一旦你完成了这个,你通常只会考虑一个小功能,而且你会更容易理解你做错了什么。如果您仍然无法找到问题,请在此处发布代码段,然后有人可能会帮助您。

答案 1 :(得分:0)

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