Pregunta

Así que hice un Tic Tac Toe tarea para mi clase.He creado con éxito un simple Tic Tac Toe programa, pero de alguna manera el método de comprobación de dibujar a veces no sale bien.Si todo está lleno, pero no hay ganador, entonces es un empate.Pero si todo lo demás está lleno, excepto para la fila 0 columna 1, se mostrará "dibujar" incluso si ese cuadro es todavía en blanco.Si usted no consigue lo que me refiero, solo tratar de llenar todo, pero el centro de la caja en la parte superior de la fila, pero no gana, es decir "dibujar" a pesar de que el último cuadro que no se llena.¿Qué hice mal en mi código????Aquí está el controlador:import javax.el 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.");}
  }
}

Y aquí es el Objeto de:clase pública TwoDimensionalArrayy { Cadena privada de currentState = "IR";privado char[][] tabla;private static final int ROWS = 3;private static final int COLUMNAS = 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}
         }
      }
   }
}
¿Fue útil?

Solución

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

El break aquí se escape el interior del bucle for, pero no el exterior.Esencialmente isDraw solo se considera el último de la fila.Usted debe tratar de usar return en su lugar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top