TicTacToe.java checkWinner 方法存在问题
TicTacToe.java issues with checkWinner method
所以我创建了一个 checkWinner 方法,使用 'row' 和 'col' 私有变量,这样我就可以找到二维数组中的 'curPlayer' 位置。
import java.util.Scanner;
public class TicTacBoard
{
private char[][] board; // 2-D array of characters
private char curPlayer; // the player whose turn it is (X or O)
// added so I can locate the current player location in the board
private int row;
private int col;
// Constructor: board will be size x size
public TicTacBoard(int size)
{
board = new char[size][size];
// initialize the board with all spaces:
for(row=0; row < board.length; row++)
for(col=0; col < board[row].length; col++)
board[row][col] = ' ';
curPlayer = 'X'; // X gets the first move
}
public void playGame()
{
display();
do
{
takeTurn();
display();
}while(!checkWinner(row, col));
}
/////// display ////////
// Display the current status of the board on the
// screen, using hyphens (-) for horizontal lines
// and pipes (|) for vertical lines.
public void display()
{
System.out.println();
dispRow(0);
System.out.println("-----");
dispRow(1);
System.out.println("-----");
dispRow(2);
System.out.println();
}
// Display the current status of row r of the board
// on the screen, using hyphens (-) for horizontal
// lines and pipes (|) for vertical lines.
private void dispRow(int r)
{
System.out.println(board[r][0] + "|" + board[r][1]
+ "|" + board[r][2]);
}
/////// takeTurn ////////
// Allow the curPlayer to take a turn.
// Send output to screen saying whose turn
// it is and specifying the format for input.
// Read user's input and verify that it is a
// valid move. If it's invalid, make them
// re-enter it. When a valid move is entered,
// put it on the board.
public void takeTurn()
{
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
do{
invalid = false; // assume correct entry
System.out.println("It is now " + curPlayer + "'s turn.");
System.out.println("Please enter your move in the form row column.");
System.out.println("So 0 0 would be the top left, and 0 2 would be the top right.");
row = scan.nextInt();
col = scan.nextInt();
if(row < 0 || col < 0 || row > 2 || col > 2)
{
System.out.println("Invalid entry: row and column must both be between 0 and 2 (inclusive).");
System.out.println("Please try again.");
invalid = true;
}
else if(board[row][col] != ' ')
{
System.out.println("Invalid entry: Row " + row + " at Column " + col
+ " already contains: " + board[row][col]);
System.out.println("Please try again.");
invalid = true;
}
}while(invalid);
// Now that input validation loop is finished, put the move on the board:
board[row][col] = curPlayer;
// Switch to the other player (take turns):
if(curPlayer == 'X')
curPlayer = 'O';
else
curPlayer = 'X';
}
// If the game is over, print who won (if anyone),
// and return true. If the game is not over, return false.
public boolean checkWinner(int row, int col)
{
// YOUR CODE GOES HERE
int x = row;
int y = col;
// board length is always 3 here
// check winner on column
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
//check winner on row
for (int i = 0; i < board.length; i++) {
if (board[i][y] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
// checks winner on diagonal up
if (x == y) {
for (int i = 0; i < board.length; i++) {
if (board[i][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// check winner on diagonal down
if (x + y == board.length - 1){
for (int i = 0; i < board.length; i++) {
if (board[i][(board.length-1)-i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// checks if board is full
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == '-')
System.out.println("Nobody won, game ends in a draw!");
return true;
}
}
return false;
}
}
代码有效,但我在检查时得到了这个:
| |
-----
| |
-----
| |
It is now X's turn.
Please enter your move in the form row column.
So 0 0 would be the top left, and 0 2 would be the top right.
2 0
| |
-----
| |
-----
X| |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at TicTacBoard.checkWinner(TicTacBoard.java:126)
at TicTacBoard.playGame(TicTacBoard.java:43)
at Main.main(Main.java:14)
我以为板子的长度总是3,位置在0到3之间。这个错误有什么解决办法吗?有没有更有效的方法来做到这一点?请告诉我!
您有一个“隐藏”问题 - 也就是说,您在 takeTurn
方法中使用局部变量隐藏实例字段 row
和 col
。
在当前状态下...
// Constructor: board will be size x size
public TicTacBoard(int size) {
board = new char[size][size];
// initialize the board with all spaces:
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
curPlayer = 'X'; // X gets the first move
}
在构造函数有运行后,row
和col
将是3
,但在takeTurn
中,你定义row
和col
作为局部变量...
public void takeTurn() {
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
这意味着,当您在 playGame
方法中调用 checkWinner
时...
public void playGame() {
display();
do {
takeTurn();
display();
} while (!checkWinner(row, col));
}
您正在传递实例字段值(3
/3
)并且一切都中断了。
因此,“快速”解决方案是从 takeTurn
中删除 row
/col
的本地声明
public void takeTurn() {
Scanner scan = new Scanner(System.in);
//int row, col;
boolean invalid;
您也可以在构造函数中修复此问题,但要制作 row
/col
局部变量
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
但在某些时候,您需要更新播放器的 row
/col
值,但我可能会考虑从 takeTurn
传回此信息而不是尝试使用实例字段。
您的 if
语句中还有一个微妙但常见的错误。如果没有方括号、{
和}
,当上面的条件语句为真时,只会执行紧跟在if
语句之后的行。但是,您的缩进表示您期望不同的行为。
例如,您的第一个 for
循环是:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
这里,当if
语句为真时,只执行System.out.println()
行。 return true;
语句的缩进表示您希望它仅在 println()
中 运行,仅当条件为真时。
return true;
行不依赖于前面的 if
语句,因为它不在括号内并且 if
语句仅 运行 行紧随其后。这意味着 for
循环只会 运行 一次迭代,因为 return
行是 STAND-ALONE 并且每次都执行,而不管那些 if
语句求值。
您应该始终,始终,始终在您的 if
语句中添加方括号,即使它们是“one-liners”。考虑到这一点,我希望它看起来更像:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer) {
break;
}
if (i == board.length - 1) {
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
现在 return
行仅在前面的 if
语句为真时执行。
所以我创建了一个 checkWinner 方法,使用 'row' 和 'col' 私有变量,这样我就可以找到二维数组中的 'curPlayer' 位置。
import java.util.Scanner;
public class TicTacBoard
{
private char[][] board; // 2-D array of characters
private char curPlayer; // the player whose turn it is (X or O)
// added so I can locate the current player location in the board
private int row;
private int col;
// Constructor: board will be size x size
public TicTacBoard(int size)
{
board = new char[size][size];
// initialize the board with all spaces:
for(row=0; row < board.length; row++)
for(col=0; col < board[row].length; col++)
board[row][col] = ' ';
curPlayer = 'X'; // X gets the first move
}
public void playGame()
{
display();
do
{
takeTurn();
display();
}while(!checkWinner(row, col));
}
/////// display ////////
// Display the current status of the board on the
// screen, using hyphens (-) for horizontal lines
// and pipes (|) for vertical lines.
public void display()
{
System.out.println();
dispRow(0);
System.out.println("-----");
dispRow(1);
System.out.println("-----");
dispRow(2);
System.out.println();
}
// Display the current status of row r of the board
// on the screen, using hyphens (-) for horizontal
// lines and pipes (|) for vertical lines.
private void dispRow(int r)
{
System.out.println(board[r][0] + "|" + board[r][1]
+ "|" + board[r][2]);
}
/////// takeTurn ////////
// Allow the curPlayer to take a turn.
// Send output to screen saying whose turn
// it is and specifying the format for input.
// Read user's input and verify that it is a
// valid move. If it's invalid, make them
// re-enter it. When a valid move is entered,
// put it on the board.
public void takeTurn()
{
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
do{
invalid = false; // assume correct entry
System.out.println("It is now " + curPlayer + "'s turn.");
System.out.println("Please enter your move in the form row column.");
System.out.println("So 0 0 would be the top left, and 0 2 would be the top right.");
row = scan.nextInt();
col = scan.nextInt();
if(row < 0 || col < 0 || row > 2 || col > 2)
{
System.out.println("Invalid entry: row and column must both be between 0 and 2 (inclusive).");
System.out.println("Please try again.");
invalid = true;
}
else if(board[row][col] != ' ')
{
System.out.println("Invalid entry: Row " + row + " at Column " + col
+ " already contains: " + board[row][col]);
System.out.println("Please try again.");
invalid = true;
}
}while(invalid);
// Now that input validation loop is finished, put the move on the board:
board[row][col] = curPlayer;
// Switch to the other player (take turns):
if(curPlayer == 'X')
curPlayer = 'O';
else
curPlayer = 'X';
}
// If the game is over, print who won (if anyone),
// and return true. If the game is not over, return false.
public boolean checkWinner(int row, int col)
{
// YOUR CODE GOES HERE
int x = row;
int y = col;
// board length is always 3 here
// check winner on column
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
//check winner on row
for (int i = 0; i < board.length; i++) {
if (board[i][y] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
// checks winner on diagonal up
if (x == y) {
for (int i = 0; i < board.length; i++) {
if (board[i][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// check winner on diagonal down
if (x + y == board.length - 1){
for (int i = 0; i < board.length; i++) {
if (board[i][(board.length-1)-i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
// checks if board is full
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == '-')
System.out.println("Nobody won, game ends in a draw!");
return true;
}
}
return false;
}
}
代码有效,但我在检查时得到了这个:
| |
-----
| |
-----
| |
It is now X's turn.
Please enter your move in the form row column.
So 0 0 would be the top left, and 0 2 would be the top right.
2 0
| |
-----
| |
-----
X| |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at TicTacBoard.checkWinner(TicTacBoard.java:126)
at TicTacBoard.playGame(TicTacBoard.java:43)
at Main.main(Main.java:14)
我以为板子的长度总是3,位置在0到3之间。这个错误有什么解决办法吗?有没有更有效的方法来做到这一点?请告诉我!
您有一个“隐藏”问题 - 也就是说,您在 takeTurn
方法中使用局部变量隐藏实例字段 row
和 col
。
在当前状态下...
// Constructor: board will be size x size
public TicTacBoard(int size) {
board = new char[size][size];
// initialize the board with all spaces:
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
curPlayer = 'X'; // X gets the first move
}
在构造函数有运行后,row
和col
将是3
,但在takeTurn
中,你定义row
和col
作为局部变量...
public void takeTurn() {
Scanner scan = new Scanner(System.in);
int row, col;
boolean invalid;
这意味着,当您在 playGame
方法中调用 checkWinner
时...
public void playGame() {
display();
do {
takeTurn();
display();
} while (!checkWinner(row, col));
}
您正在传递实例字段值(3
/3
)并且一切都中断了。
因此,“快速”解决方案是从 takeTurn
row
/col
的本地声明
public void takeTurn() {
Scanner scan = new Scanner(System.in);
//int row, col;
boolean invalid;
您也可以在构造函数中修复此问题,但要制作 row
/col
局部变量
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
但在某些时候,您需要更新播放器的 row
/col
值,但我可能会考虑从 takeTurn
传回此信息而不是尝试使用实例字段。
您的 if
语句中还有一个微妙但常见的错误。如果没有方括号、{
和}
,当上面的条件语句为真时,只会执行紧跟在if
语句之后的行。但是,您的缩进表示您期望不同的行为。
例如,您的第一个 for
循环是:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer)
break;
if (i == board.length - 1)
System.out.println("Player " + curPlayer + " wins!");
return true;
}
这里,当if
语句为真时,只执行System.out.println()
行。 return true;
语句的缩进表示您希望它仅在 println()
中 运行,仅当条件为真时。
return true;
行不依赖于前面的 if
语句,因为它不在括号内并且 if
语句仅 运行 行紧随其后。这意味着 for
循环只会 运行 一次迭代,因为 return
行是 STAND-ALONE 并且每次都执行,而不管那些 if
语句求值。
您应该始终,始终,始终在您的 if
语句中添加方括号,即使它们是“one-liners”。考虑到这一点,我希望它看起来更像:
for (int i = 0; i < board.length; i++) {
if (board[x][i] != curPlayer) {
break;
}
if (i == board.length - 1) {
System.out.println("Player " + curPlayer + " wins!");
return true;
}
}
现在 return
行仅在前面的 if
语句为真时执行。