gpt4 book ai didi

java - 使用二维数组的 TicTacToe

转载 作者:行者123 更新时间:2023-12-01 19:01:48 25 4
gpt4 key购买 nike

我正在尝试创建一个执行 TicTacToe 游戏的程序。我已经创建完毕所有方法,我只需要创建驱动程序。在创建之前驱动程序,我试图只打印电路板和一个字符,但我没有认为我的方法是正确的。这是我的错误:

java.lang.ArrayIndexOutOfBoundsException: 3
at TicTacToeBoard.move(TicTacToeBoard.java:75)
at TicTacToe.main(TicTacToe.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

这是我的两个程序:

这是我的驱动程序,我似乎无法完成。最后显示的是模板,以便您了解每个程序的工作原理。

class TicTacToe
{
public static void main(String [] args)
{
//System.out.println("Welcome! Tic-Tac-Toe is a two player game.");
//System.out.println("Enter player one's name: ");

TicTacToeBoard game = new TicTacToeBoard();
System.out.println(game.toString());

//int count = 0;


game.move('x', 1, 3);
// game.move('o', 1, 1);

/* while (game.gameWon() !true || count != 9)
{
System.out.print(game.move());
System.out.print(game.isEmpty());
}*/
}
}

这是所有方法的所在......

class TicTacToeBoard
{
private char [][] board = new char[3][3];
String b;

// This a new constructor that creates a tic-tac-toe board
public TicTacToeBoard()
{
for (int rows = 0; rows < board.length; rows++)// creates rows
{
for (int columns = 0; columns <board[rows].length;columns++)// creates columns
{
//System.out.println("| ");
board[rows][columns] = ' ';
//System.out.println(" |\n" );
}
}
}

// creates a string form of the tic-tac-toe board and allows the user
// to access it during the game.
public String toString()
{
String b = "";

// creates a vertical bar at the beginning and the end of each row
for (int rows = 0; rows < board.length; rows++)
{
b += "| ";
// adds a space for each row and column character in tic-tac-toe board.
for (int columns = 0; columns < board[rows].length; columns++)
{
b += board[rows][columns] + " ";
}
b += "|\n";// prints a | space space space | and breaks off to create two new lines.
}
return b; // prints the tic-tac-toe board to be accessed by the user.
}

String move(char x, int rows, int columns)
{
String b = "";

// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++)
{
b += "| ";

for (int c = 0; c < board[r].length; c++)
{
b += board[r][c] + " "; //prints 3 spaces on each line.
// prints string character from user input if row and column not equal to zero
if (board[rows - 1][columns - 1] >= 0 && board[rows - 1][columns - 1] <= 2 )
{
board[rows - 1][columns - 1] = x;// prints character in the specified index from user input
b += board[rows - 1][columns - 1];// prints out the board and the new character in specified space.
}
else if (board[rows - 1][columns - 1] < 0) // makes user pick another choice
return "ILLEGAL MOVE, TRY AGAIN!";
// adds a space for each row and column character in tic-tac-toe board.
}
b += "|\n";// prints a | space space space | and breaks off to create two new lines.
}
return b; // prints the tic-tac-toe board to be accessed by the user.

}

// checks if a space character is empty
void isEmpty(char x, int row, int col)
{
if (board [row - 1][col - 1] == ' ')
board[row - 1][col - 1] = x;
else // makes user pick another row and column if space character is not empty
System.out.println("ILLEGAL CHOICE, PICK AGAIN!");
}
// checks if game is won
public boolean gameWon(int row, int col)
{
if ((board[2][0] == board[1][1]) && (board[2][0] == board[0][2]))
return true;
else if ((board[2][0] != board[1][1]) && (board[2][0] != board[0][2]))
return false;

if ((board[2][2] == board[1][1])&& (board[2][2] == board[0][0]))
return true;
else if ((board[2][2] != board[1][1])&& (board[2][2] != board[0][0]))
return false;

if ((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]))
return true;
else if ((board[0][0] != board[1][0]) && (board[0][0] != board[2][0]))
return false;

if ((board[0][1] == board[1][1]) && (board[0][1] == board[2][1]))
return true;
else if ((board[0][1] != board[1][1]) && (board[0][1] != board[2][1]))
return false;

if ((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]))
return true;
else if ((board[0][2] != board[1][2]) && (board[0][2] != board[2][2]))
return false;

if ((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]))
return true;
else if ((board[0][0] != board[0][1]) && (board[0][0] != board[0][2]))
return false;

if ((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]))
return true;
else if ((board[1][0] != board[1][1]) && (board[1][0] != board[1][2]))
return false;

if ((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]))
return true;
else
return false;
}
}

这是整个事情的模板!!!!!!

class TicTacToe
{
public static void main (String [] args)
{
TicTacToeBoard b = new TicTacToeBoard();

while (game not over)
{
swtich player
increment turn counter

until user enters a valid move
{
prompt for move
}

make move
b.makeMove (player, row, col);

print board
System.out.println(b);
}

print outcome
}
}


class TicTacToeBoard
{
private char [][] board = ...;

public TicTacToeBoard()
{
initialize board with spaces
}

public void makeMove (char c, int row, int col)
{
store symbol in specified position
}

public boolean isEmpty(int row, int col)
{
return true if square is unfilled
}

public boolean gameWon()
{
check board for a win
}

public String toString ()
{
return String representation of board
}

}

最佳答案

编程语言不允许出现错误,并迫使我们严格遵守。

您的代码对我们来说很难阅读,因此对我们和您来说都很难调试,从到处都是的缩进开始,但也有粗心的错误,尤其是这个:

for (int r = 0; r < board.length; rows++)

你看出这里出了什么问题吗? rrows 不同,您不能使用一个作为循环的索引,然后递增另一个。您在循环内使用了这两个变量。代码中还存在其他几个粗心错误。

我建议您重新开始,但要更加小心您的代码,尤其是您的缩进。如果您没有正确排列大括号,您将看不到一个代码块何时结束而另一个代码块何时开始(我们也不会!)。

哦,下次,请让我们知道您的代码的哪些行导致了错误。如果我们不必猜测这些信息,那么为您提供帮助就会容易得多。

编辑
您的新代码缩进更好了一些,但仍然关闭。这就是你所拥有的:

String move(char x, int rows, int columns)
{
String b = "";

// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++)
{
b += "| ";

for (int c = 0; c < board[r].length; c++)
{
b += board[rows][columns] + " ";

这就是我的建议:

String move(char x, int rows, int columns) {
String b = "";

// creates a vertical bar at the beginning and the end of each row
for (int r = 0; r < board.length; r++) {
b += "| ";

for (int c = 0; c < board[r].length; c++) {

// let's check to see what the variables hold!
System.out.printf("rows: %d, columns %d, r: %d, c: %d%n", rows, columns, r, c);
b += board[rows][columns] + " "; // **** the offending line ****

更重要的是,请注意紧跟异常的 printf 语句的结果:

rows: 1, columns 3, r: 0, c: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

所以这里是列变量,它的值是 3,导致数组爆炸。您需要回溯以查看如何调用此方法以及为什么它将 3 传递到列参数中。

编辑2
在重新审查您的最新帖子时,您仍在对移动方法进行硬编码以接受 3 作为列参数:

game.move('x', 1, 3);

首先解决这个问题。该参数不能为 3。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com