Java 二维阵列:TicTacToe程序

Java 二维阵列:TicTacToe程序,java,Java,我下面的程序是一个井字游戏。以下是任务的详细信息: 井字棋 将游戏板存储在单个2D阵列中。(3 x 3)x 方法来添加移动 方法来显示板 方法来判断轮到谁(X或O) 方法来查找赢家或平局 方法将游戏初始化为开始 允许两名玩家在同一键盘上输入轮次的主要方法 我的问题是,我不知道如何获取用户输入的坐标,并将其转换为“X”或“O”值,以便用户将其转换为数组,然后在用户每次播放后,该数组会显示在棋盘上。网上有编译错误;展示板(char[][]显示阵列);。任何其他关于如何简化事情的评论和错误,或者你

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

井字棋
  • 将游戏板存储在单个2D阵列中。(3 x 3)x
  • 方法来添加移动
  • 方法来显示板
  • 方法来判断轮到谁(X或O)
  • 方法来查找赢家或平局
  • 方法将游戏初始化为开始
  • 允许两名玩家在同一键盘上输入轮次的主要方法
我的问题是,我不知道如何获取用户输入的坐标,并将其转换为“X”或“O”值,以便用户将其转换为数组,然后在用户每次播放后,该数组会显示在棋盘上。网上有编译错误;展示板(char[][]显示阵列);。任何其他关于如何简化事情的评论和错误,或者你看到的错误,都是绝对欢迎的

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;



}

}
public类TicTacToeMain//运行系统的主类。
{
公共静态void main(字符串[]args)
{
系统输出打印项次(“TIC TAC TOE”);
System.out.println();
System.out.println(“说明:将要求您输入的行号(0-2)和列号(0-2”);
System.out.println(“你想演奏你的曲子的棋盘。你要决定哪个演奏者是X和O的和”);
System.out.println(“要按提示移动,X总是先开始。要赢得比赛,你需要连续放置3个棋子”);
System.out.println(“水平、垂直或对角。下面是电路板布局的示例。请欣赏!”);
System.out.println();
系统输出打印项次(“0 1 2”);
System.out.println();
系统输出打印项次(“0”);
System.out.println();
系统输出打印项次(“1”);
System.out.println();
系统输出打印项次(“2”);
System.out.println();
TicTacToe ticTac=新的TicTacToe();
ticTac.showBoard(char[][]displayArray);//标记“char”和“displayArray”上的语法错误。
ticTac.readInput();
}
}
导入java.util.Scanner;
公共类TicTacToe//helper方法类。
{
私有整数计数;
私人字符播放器;
列兵int row,col;
私有字符[][]板=新字符[3][3];
public TicTacToe()//构造函数方法
{ 
字符[][]板=新字符[3][3];
用于(字符行=0;行<3;行++)
for(int col=0;col<3;col++)
板[行][列]='';
playerTurn='X';
moveCount=0;
}
public void findResult()//用于查找赢家/结并打印给用户的构造函数。
{
这个.setPlayerTurn();
如果(线路板[行][0]==线路板[行][1]&线路板[行][1]==线路板[行][2]&&(线路板[行][0]='X'| |线路板[行][0]=='O'))
System.out.println(+playerTurn+“wins!”);
否则如果(板[0][col]==板[1][col]&板[1][col]==板[2][col]&&(板[0][col]='X'| |板[0][col]==''O'))
System.out.println(+playerTurn+“wins!”);
否则如果(板[0][0]==板[1][1]&&B[1][1]==板[2][2]&&(板[0][0]=='X'| |板[0][0]=='O'))
System.out.println(+playerTurn+“wins!”);
否则如果(板[2][0]==板[1][1]&&B[1][1]==板[0][2]&&(板[2][0]=='X'| |板[2][0]=='O'))
System.out.println(+playerTurn+“wins!”);
else if(moveCount==9)
System.out.println(“平局游戏!”);
}
public void readInput()//用于读取用户输入的方法。
{
新罗,新科;
这个.setPlayerTurn();
做
{
扫描仪键盘=新扫描仪(System.in);
这个.setPlayerTurn();
System.out.println(“旋转”+移动计数);
System.out.println(“Player”+playerTurn+“请选择您希望下一步移动的行。”);
newRow=keyboard.nextInt();
如果(新行<0 | |新行>2)
System.out.println(“输入无效,请重新输入”);
其他的
{
行=新行;
System.out.println(“现在,输入列。”);
newCol=keyboard.nextInt();
如果(新列>2 | |新列<0)
System.out.println(“输入无效,请重新输入”);
其他的
col=新col;
moveCount++;
System.out.println(“您输入了行“+行+”和列“+列+”);
System.out.println();
findResult();
}

}而(moveCount将您对主屏幕中showboard的调用更改为

ticTac.showBoard(); 
因为你已经在一个TictaToe对象中拥有了电路板

那么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();
    }
  }
一开始在类的主体中创建类的实例有点让人困惑,但您会习惯于这样做……而且您将拥有更少的文件:-)

但要注意的是,在执行此操作时,确实需要创建类的对象以在main中使用其方法,因为main是“静态的”方法。静态方法是在程序中分配的,main是第一个在java程序中运行的方法,因此,如果不创建对象,main不知道如何访问其中的方法。希望这不会造成更多的混乱

作为另一个指针,如果您真的试图创建一个新的2d字符数组来传递给该方法,您可以这样做

ticTac.showBoard(new char[3][3]);
我不知道如何获取用户输入的坐标,并将其转换为x或o值,以便在阵列中播放,然后显示板本身

readInput
中,您大部分时间都在那里。您拥有用户输入的
col
坐标。同样,您在显示板时查看
board[row][col]
中的值,以及
board[row][0]
在检查是否获胜时,您希望将
棋盘
数组中的位置设置为当前玩家。这可以通过说
棋盘[something][something]=value
来完成。应该是什么
something
value

关于如何简化事情或错误的任何其他评论和错误
ticTac.showBoard(new char[3][3]);