java - 二维数组 : TicTacToe program

标签 java

我下面的程序是一个井字棋游戏。以下是任务的详细信息:

井字游戏

  • 将游戏板存储在单个二维数组中。 (3 x 3) X
  • 添加 Action 的方法。
  • 显示面板的方法。
  • 判断轮到谁的方法(X 或 O)。
  • 寻找获胜者或平局的方法。
  • 将游戏初始化到开始的方法。
  • 允许两个玩家在同一个键盘上输入回合的主要方法。

我的问题是,我不知道如何获取用户输入的坐标并将其转换为“X”或“O”值,以便用户轮流转换为数组,然后将其显示在棋盘本身上用户每次玩游戏后。网上有编译错误; ticTac.showBoard(char[][] displayArray);。绝对欢迎您对如何简化事情或看到的错误提出任何其他评论和错误!

public class TicTacToeMain //main class that runs the system.
    {
      public static void main(String[] args)
      {

        System.out.println("             TIC TAC TOE");
        System.out.println();
        System.out.println("Instruction: You will be asked to enter the row number(0-2) and the column number(0-2) of");
        System.out.println("the board you wish to play your piece. You are to decide which player is X's and O's and");
        System.out.println("to move as prompted. X's always start first. To win you need to place 3 pieces in a row ");
        System.out.println("horizontally, vertically, or diagonally. An example of the board layout is below. Enjoy!");
        System.out.println();
        System.out.println("  0   1   2");
        System.out.println();
        System.out.println("0");
        System.out.println();
        System.out.println("1");
        System.out.println();
        System.out.println("2");
        System.out.println();

        TicTacToe ticTac = new TicTacToe();
        ticTac.showBoard(char[][] displayArray); //SYNTAX ERROR ON TOKEN "char" and "displayArray".
        ticTac.readInput();



      }
    }





import java.util.Scanner;

public class TicTacToe //helper methods class.
{
  private int moveCount;
  private char playerTurn;
  private int row, col;
  private char[][] board = new char[3][3];

  public TicTacToe() //constructor method
  { 
    char[][] board = new char[3][3];
    for(char row = 0 ; row < 3; row++)
      for(int col = 0; col < 3; col++)
      board[row][col] = ' ';
   playerTurn = 'X';
    moveCount = 0;



  }
   public void findResult() //constructor to find winner/tie and print to user.
  {
     this.setPlayerTurn();


     if(board[row][0] == board[row][1] && board[row][1] == board[row][2] && (board[row][0] == 'X' || board[row][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[0][col] == board[1][col] && board[1][col] == board[2][col] && (board[0][col] == 'X' || board[0][col] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && (board[0][0] == 'X' || board [0][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(board[2][0] == board [1][1] && board[1][1] == board[0][2] && (board [2][0] == 'X' || board[2][0] == 'O'))
      System.out.println( + playerTurn + " wins!");

    else if(moveCount == 9)
     System.out.println("Tie game!");

}

 public void readInput() //method to read user input.
  {
    int newRow, newCol;
    this.setPlayerTurn();
    do
    {
    Scanner keyboard = new Scanner(System.in);

      this.setPlayerTurn();
    System.out.println("Turn " + moveCount);
    System.out.println("Player " + playerTurn + " please select the row you wish to place your next move.");
    newRow = keyboard.nextInt();
    if(newRow < 0 || newRow > 2)
       System.out.println("Invalid Entry. Please re-enter.");
    else
    {
      row = newRow;
    System.out.println("Now, enter the column.");
    newCol = keyboard.nextInt();
    if(newCol > 2 || newCol < 0)
    System.out.println("Invalid Entry. Please re-enter.");
    else
      col = newCol;
      moveCount++;
      System.out.println("You entered row " + row + " and column " + col +".");
      System.out.println();
      findResult();
    }
    }while(moveCount <= 8);


  }

  public  void showBoard(char[][]displayArray) //to add inputs to as well as  display board.
  {
    int rowInput, colInput;
    readInput();
    rowInput = row;
    colInput = col;
    for(rowInput = 0; rowInput < displayArray.length; row++)
     {
      for(colInput = 0; colInput < displayArray[row].length; col++)
      System.out.print(" " + displayArray[row][col] + " ");
      System.out.println();
    }
  }

  private char setPlayerTurn() //method to find which players turn it is.
  {
    {
    if (moveCount == 0 || moveCount % 2 == 0)
      playerTurn = 'X'; 
    else
      playerTurn = 'O';
    }
    return playerTurn;



}

}

最佳答案

将对主屏幕中的显示板的调用更改为

ticTac.showBoard(); 

因为您已经在 TicTacToe 对象中拥有了棋盘。

那么 showBoard() 应该如下所示:

 public  void showBoard() //to add inputs to and display board.
  {
    int rowInput, colInput;
    readInput();
    rowInput = row;
    colInput = col;
    for(rowInput = 0; rowInput < board.length; row++)
     {
      for(colInput = 0; colInput < board[row].length; col++)
      System.out.print(" " + board[row][col] + " ");
      System.out.println();
    }
  }

这样做的原因是因为您的 TicTacToe 对象已经有一个棋盘,您不需要向它传递一个棋盘。

另外,作为另一个提示,您不需要另一个类来运行 TicTacToe。您可以简单地将 main 放在 TicTacToe 中,如下所示:

public class TicTacToe {

public TicTacToe(...){
...
}
public static void main(String[] args){
...read inputs and make board here...
}

...other methods...

}

一开始在类的主体中创建类的实例有点令人困惑,但你会习惯它......并且你的文件会少很多:-)。

但在执行此操作时请注意,您确实需要创建类的对象才能在 main 中使用其方法,因为 main 是一个“静态”方法。静态方法是在程序中的其他所有内容之前分配的,而 main 是第一个在 java 程序中运行的方法,因此,如果您不创建该对象,main 不知道如何访问其中的方法。希望这不会造成不必要的困惑。

作为另一个指针,如果您确实尝试创建一个新的二维字符数组来传递给该方法,您可以这样做

ticTac.showBoard(new char[3][3]);

关于java - 二维数组 : TicTacToe program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22571979/

相关文章:

Java TCP 延迟

java - Android Studio if/Else 语句不能正常工作

java - 我如何在这里等待java代码中的所有线程?

Session.getInstance 的 Java 邮件问题

JAVA 不兼容的类型值

java - 如何从同一模型中的不同表中检索对象列表?

java - 有些变量在 main 方法中不起作用

java - TextView 不能正常工作

java - 为什么是Object类的wait、notify、notifyAll方法?

java - Guava 先决条件的自定义异常