2011-05-12 3 views
0

내가 뭘 잘못 알고있어 여기에. 누구나 내 checkRow 코드가 무엇이 잘못되었는지 checkWinnerisSquareFree 방법으로 말할 수 있습니까? 이 인쇄 라인 문을 사용하는 것입니다 같은 프로젝트에서 버그를 추적하는TicTacToe 어레이. 당첨 방법과 광장 무료입니다

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

답변

0

하나의 좋은 방법

여기 내 코드입니다. 이것을 시도하십시오 : 문제가있는 곳에 인쇄 선을 긋고 일부 지역 변수의 값을 인쇄하십시오. 그렇게해서는 안되는 값을 발견하면 코드에서 데이터가 이전에 문제가 있음을 알 수 있습니다. 이 경우 흐름에서 인쇄 줄을 더 뒤로 이동하고 다시 시도하십시오. 가치가 좋던 것에서 나쁜 것 (또는 좋지 않은 것)으로 바뀔 때까지이 작업을 계속하십시오. 그 시점에서 버그가있는 코드를 추적했습니다.

일단이 작업을 완료하면 일반적으로 생각할 수있는 기능이 거의 없으며 잘못 수행 한 사항을 이해하는 것이 훨씬 쉬울 것입니다. 그래도 문제를 찾을 수 없다면 여기에 코드 스 니펫을 게시하면 누군가가 도움을 줄 것입니다.

0

checkWinner()가 잘못되었습니다. 대각선을 확인하지 않고 8 개의 우승 조합을 작성하십시오.