我下面的程序是Tic Tac Toe游戏。以下是作业的详细信息:
我的问题是我不知道如何取用户输入的坐标并将它们转换为“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;
}
}
答案 0 :(得分:1)
将您对main的电话转到
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。您可以简单地将主要内部放在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不知道如何访问其中的方法。希望这不会造成比它值得更多的混乱。
作为另一个指针,如果你真的想要创建一个新的2d char数组来传递给那个方法你就可以这样做了
ticTac.showBoard(new char[3][3]);
答案 1 :(得分:0)
我不知道如何获取用户输入的坐标并将其转换为x或o值,以便播放到阵列中,然后显示电路板本身。
你在readInput
的大部分路上。您有来自用户的row
和col
坐标输入。以同样的方式查看显示电路板时board[row][col]
中的值,并在检查获胜时查看board[row][0]
,您需要将board
数组中的位置设置为当前播放器。这可以通过说board[something][something] = value
来完成。 something
和value
应该是什么?
您对如何简化您看到的内容或错误所做的任何其他评论和错误都是绝对受欢迎的!
看起来几乎所有的程序都在那里并完成了。但是,将所有内容捆绑在一起的程序流程存在一些问题。这里有一些可能适用的编程原则:
考虑到这些想法,我将对您的计划流程提出一些意见:
findResult()
会调用setPlayerTurn()
。这是误导性的,因为每当你只想检查游戏结果时,当前玩家就会改变。什么时候应该切换?readInput()
在您到达showBoard()
的呼叫之前循环了8次。显示董事会应该是TicTacToe
的责任。 main
方法应该开始游戏。readInput()
读取输入或玩整个游戏吗?听起来它应该读取一组坐标输入,但它有一个循环来处理整个游戏。将它分成两个方法可能更有意义 - 一个处理单个移动,一个循环9次,显示板并调用readInput()
方法。