java - Tic Tac Toe java 抽奖游戏

标签 java tic-tac-toe

所以我为我的类(class)做了一个井字棋作业。我已经成功创建了一个简单的 Tic Tac Toe 程序,但不知何故,检查抽奖的方法有时不正确。如果所有内容都已满,但没有获胜者,则为平局。但是,如果除了第 0 行第 1 列之外的所有其他内容都已填充,即使该框仍为空白,它仍将显示“绘制”。如果您不明白我的意思,只需尝试填写除顶行中间框之外的所有内容,但没有获胜,即使最后一个框未填写,它也会显示“平局”。我的代码做错了什么???这是驱动程序: 导入 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.");}
  }
}

这是对象: 公共(public)类二维数组 { 私有(private)字符串当前状态=“GO”; 私有(private) char[][] 板; 私有(private)静态最终 int ROWS = 3; 私有(private)静态最终 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 将转义内部 for 循环,但不会转义外部循环。本质上 isDraw 仅考虑最后一行。您应该尝试使用 return 来代替。

关于java - Tic Tac Toe java 抽奖游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005325/

相关文章:

java - 使用流 Java 8 从 List<Object>,List<Object> 转换为 List<AnotherObject>

c++ - 在 C++ 中没有匹配函数调用 'game_rule'

java - java中的井字游戏,覆盖问题

algorithm - 我可以使用哪种井字游戏算法来确定 AI 的 "best move"?

java - Apache Lucene 6.2 标准分析器版本

java - 是否可以在 O(1) 时间内进行搜索?

java - 如何在 Neo4j3 中调试非托管服务器扩展

c - 没有人工智能的井字游戏

java - 井字游戏板的所有可能性 - 代码日志 - Java递归通用树

java - 通过java实现二进制搜索