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() は間違っています。対角線をチェックしていません。8 の勝ちの組み合わせを書いてください。

于 2011-08-22T12:26:21.513 に答える
0

このようなプロジェクトでバグを追跡する良い方法の 1 つは、print line ステートメントを使用することです。これを試してください: 問題があると思われる場所の近くに印刷行を置き、いくつかのローカル変数の値を印刷します。すべきではない値が表示された場合は、コードの早い段階でデータに問題が発生したことがわかります。その場合は、印刷行をフローのさらに後ろに移動して、もう一度やり直してください。値が良いものから悪いものへ (または悪いものから良いものへ) 変化することに気付くまで、これを続けてください。その時点で、バグを含むコードを突き止めたことになります。

これを行うと、通常、考える関数はほんのわずかになり、何が間違っていたのかを理解するのがずっと簡単になります。それでも問題が見つからない場合は、ここにコード スニペットを投稿してください。誰かが助けてくれるでしょう。

于 2011-05-12T03:28:43.330 に答える