문제

그래서 나는 틱 전술 발가락 할당한다.나는 성공적으로 간단하게 만들었 틱 전술 발가락으로 어떻게든 방법을 확인에 대한 그리는 때로는 오지 않을 수도 있습니다.면 모든 것이 가득하지만 없자,그때 그립니다.그러나면 다른 모든 것은 가득을 제외한 행 0 1 열,그것은 여전히 표시됩니다""그릴 경우에도 그 상자는 여전히 비어 있습니다.얻지 않는 경우에 당신은 무엇을 의미하려고 작성 모든 것이지만 중간에 상자의 상단을 행하지만 승리,그것은 말할 것이다"그리"비록 그 마지막 상자가 채워지지 않습니다.나는 무엇을 잘못했는 내 코드는????여기에 운전사:import javax.한창이다.JOptionPane;

public class TwoDimensionalArray_Driverr
{
  public static void main(String[]args)
  {
    char player = 'o';
    TwoDimensionalArrayy game = new TwoDimensionalArrayy();

    while (game.checkGame() == "PLAY")
    {
      if (player == 'o') player = 'x';
      else player = 'o';
      System.out.println(game);
      String input = JOptionPane.showInputDialog("Enter Position of Row for player "+ player +" or     press Cancel to exit");
      if (input == null)
        System.exit(0);
      int row = Integer.parseInt(input);

      input = JOptionPane.showInputDialog("Enter Position of Column for player " + player);
      int column = Integer.parseInt(input);

      game.set(row,column,player);
      game.isDraw();
      game.hasWon(row,column,player);
      game.checkGame();
      System.out.println(game.checkGame());
    }
    if (game.checkGame()=="DRAW"){
      System.out.println(game);
      System.out.println("It's a draw.");
    }
    else {
      System.out.println(game);
      System.out.println(player + " has won.");}
  }
}

그리고 여기에 객체:공급 TwoDimensionalArrayy { private 문자열 currentState="GO";개인 char[][]보드개인 static final int ROWS=3;개인 static final int COLUMNS=3;

  public TwoDimensionalArrayy(){
  board = new char[ROWS][COLUMNS];

  for(int i=0;i<ROWS;i++) //always do ROWS first!!!!
    for(int j = 0;j<COLUMNS;j++)
    board[i][j]=' ';
  }

  public void set(int i, int j, char player)
  {
    if(board[i][j] != ' ' )
      throw new IllegalArgumentException("Position Occupied");
    board[i][j] = player;
  }

  public String toString()
  {
    System.out.println("This is the board. 3x3");
    System.out.println("Position start @ row[0]col[0],row[0]col[1],row[0]col[2]");
    String dString= "";
    for (int row = 0; row<ROWS; row++)
    {
      if (COLUMNS>0)
      dString += board[row][0];
      for (int col = 1; col<COLUMNS; col++)
      {
        dString+= "|" + board[row][col];
      } //end 2nd for
      dString += "\n";
    }//end first for
    return dString;
  }

  public String checkGame(){
    if (currentState=="Win"){
      return "END";}
    else if (currentState=="Draw"){
      return "DRAW";}
    else return "PLAY";
  }
  public void hasWon(int i,int j,char player){
    if (board[i][0] == player         // 3-in-the-row
                   && board[i][1] == player
                   && board[i][2] == player
              || board[0][j] == player      // 3-in-the-column
                   && board[1][j] == player
                   && board[2][j] == player
              || i == j            // 3-in-the-diagonal
                   && board[0][0] == player
                   && board[1][1] == player
                   && board[2][2] == player
              || i + j == 2  // 3-in-the-opposite-diagonal
                   && board[0][2] == player
                   && board[1][1] == player
                   && board[2][0] == player)
      currentState = "Win";
  }
  public void isDraw(){
     for ( int row = 0; row < ROWS; row++) {
         for (int col = 0; col < COLUMNS; col++) {
            if (board[row][col] == ' ') {
               currentState = "Play";
               break;
            }
            else {currentState = "Draw";} // no empty cell, it's a draw}
         }
      }
   }
}
도움이 되었습니까?

해결책

public void isDraw(){
  for ( int row = 0; row < ROWS; row++) {
    for (int col = 0; col < COLUMNS; col++) {
      if (board[row][col] == ' ') {
        currentState = "Play";
        break;
      } else {
        currentState = "Draw"; // no empty cell, it's a draw
      } 
    }
  }
}

break 여기에서 벗어나 내면에 대한 반복,그러나지 않는 외부 중 하나.기본적으로 isDraw 만을 고려한 마지막 행이 있습니다.당신이해야 사용하여 시험 return 대신 합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top