Question

J'ai donc fait un Tic Tac Toe affectation pour les élèves de ma classe.J'ai réussi à créer un simple Tic Tac Toe programme mais de toute façon la méthode de vérification pour le tirage n'a parfois pas correctement.Si tout est rempli, mais il n'y a pas de gagnant, alors c'est un tirage au sort.Mais si tout le reste est rempli, sauf pour la ligne 0, colonne 1, il continue à afficher "tirage au sort" même si cette zone est encore vierge.Si vous n'obtenez pas ce que je veux dire, juste essayer de remplir de tout, mais la case du milieu sur la ligne supérieure, mais ne gagne pas, on va dire "tirage" même si la dernière case n'est pas remplie.Qu'ai-je fait de mal dans mon code????Voici le pilote:import javax.swing.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.");}
  }
}

Et voici l'Objet:public class TwoDimensionalArrayy { private String currentState = "ALLER";privé char[][] conseil d'administration;private static final int ROWS = 3;private static final int COLONNES = 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}
         }
      }
   }
}
Était-ce utile?

La solution

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

L' break ici s'échapper à l'intérieur de la boucle, mais pas de l'extérieur.Essentiellement isDraw considère uniquement la dernière ligne.Vous devez essayer d'utiliser le return au lieu de cela.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top