我正在尝试创建一个tic tac toe游戏类,其中一个问题是makeMove方法,我必须使用switch方法。当我使用测试器类时,它不起作用。有人能告诉我答案/问题应该是什么。 这是tic tac toe class
public class TicTacToe
{
private String[][] board;
private int turn;
private boolean hasWinner;
private boolean bale;
/**
* Constructs an empty game board. Sets the turn to zero.
*/
public TicTacToe()
{
board = new String[][] {
{"1", " | ", "2", " | ", "3"},
{"-", "-|-", "-", "-|-", "-"},
{"4", " | ", "5", " | ", "6"},
{"-", "-|-", "-", "-|-", "-"},
{"7", " | ", "8", " | ", "9"},
};
turn = 0;
}
/**
* Prints the two dimensional array gameboard to the console with an empty line before and after
*/
public void drawBoard()
{
for(int rows = 0; rows < board.length; rows++)
{
for( int cols = 0; cols < board[0].length; cols++)
{
System.out.print(board[rows][cols] );
}
System.out.println();
}
}
/**
* Precondition: the location will be a number 1 through 9
* Hint: Loop through the array and see if the number still exists there.
* @param location the numeric location according to the initial configuration
* @return true if the space is unoccupied and legal, false otherwise
*/
public boolean validMove(int location)
{
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length; col++) {
if( board[row][col].equals( location))
{
bale = false;
}
else
{
bale = true;
}
}
}
return bale;
}
/**
* Preconditions: the location is valid, the symbol is a single character: X or O.
* @param location the numeric location according to the initial configuration
* @param symbol the player's symbol
*/
public void makeMove(int location, String symbol)
{
int row = 0;
int col = 0;
//map the numeric location to the row and column on the game board
switch(location)
{
case 1: row = 0; col = 0; break;
case 2: row = 0; col = 2; break;
/*#
* Insert the rest of the cases below. More info on switch statements here:
* http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
* One use is to replace a long series of if/else if/else statements.
*/
case 3: row = 0; col = 1; break;
case 4: row = 2; col = 0; break;
case 5: row = 2; col = 2; break;
case 6: row = 2; col = 1; break;
case 7: row = 1; col = 0; break;
case 8: row = 1; col = 2; break;
case 9: row = 1; col = 1; break;
}
/*#
* Insert the symbol in the appropriate location below
*/
location = symbol;
}
/**
* Checks if the winning condition has been met for either player.
* @return true if there is a winner, false otherwise
*/
public boolean hasWinner()
{
/*#
* if there is a winner from hasWinner("X") or hasWinner("O"), return true
* otherwise false
*/
hasWinner = false;
for (int i = 0; i < board.length; i+=2)
{
if ((board[i][0].equals("X") == true) && (board[i][2].equals("X") == true) && (board[i][4].equals("X") == true))
{
hasWinner = true;
}
else if ((board[i][0].equals("O") == true) && (board[i][2].equals("O") == true) && (board[i][4].equals("O") == true))
{
hasWinner = true;
}
}
for (int i = 0; i < board.length; i+=2)
{
if ((board[0][i].equals("X") == true) && (board[2][i].equals("X") == true) && (board[4][i].equals("X") == true))
{
hasWinner = true;
}
else if ((board[0][i].equals("O") == true) && (board[2][i].equals("O") == true) && (board[4][i].equals("O") == true))
{
hasWinner = true;
}
}
if( (board[0][0].equals("X")==true) && (board[2][2].equals("X")==true) && (board[4][4].equals("X") == true))
{
hasWinner = true;
}
else if( (board[0][0].equals("O")==true) && (board[2][2].equals("O")==true) && (board[4][4].equals("O") == true))
{
hasWinner = true;
}
if( (board[4][4].equals("X")==true) && (board[2][2].equals("X")==true) && (board[0][0].equals("X") == true))
{
hasWinner = true;
}
else if( (board[4][4].equals("O")==true) && (board[2][2].equals("O")==true) && (board[0][0].equals("O") == true))
{
hasWinner = true;
}
return hasWinner;
}
/**
* @param symbol the symbol of the player to check for a win condition
* @return true if there is a winner, false otherwise
*/
private boolean hasWinner(String symbol)
{
/*#
* return true if there are three of symbol in a row vertically, horizontally, or diagonally
* hint: there is a gap of one string between each actual occupiable space, so when looping,
* you can change your updater from i++ to i+=...well, I'll leave it to you to figure out what
* number to use after the +=.
*/
return true;
}
/**
* For debugging and testing only.
* @param state a valid board configuration
*/
public void setGameState(String[][] state)
{
board = state;
}
}
这是测试人员类
public class TicTacToeTester
{
public static void main(String[] args)
{
TicTacToe t = new TicTacToe();
t.drawBoard();
System.out.println("Expected: \n\n1 | 2 | 3\n--|---|--\n4 | 5 | 6\n--|---|--\n7 | 8 | 9\n\n");
System.out.println(t.validMove(4));
System.out.println("Expected: true");
t.makeMove(4, "X");
t.drawBoard();
System.out.println("Expected: \n\n1 | 2 | 3\n--|---|--\nX | 5 | 6\n--|---|--\n7 | 8 | 9\n\n");
System.out.println(t.validMove(4));
System.out.println("Expected: false");
t.makeMove(9, "O");
t.drawBoard();
System.out.println("Expected: \n\n1 | 2 | 3\n--|---|--\nX | 5 | 6\n--|---|--\n7 | 8 | O\n\n");
}
}
此外,我尝试多次和多天来做这件事,但是代码并没有按预期的那样做
答案 0 :(得分:0)
错误来自location=symbol;
,你试图将字符串转换为int。如果你想这样做,那么使用Integer.parseInt() or Integer.valueOf()
,但是即使这样,也要警告你,{{1} }}
答案 1 :(得分:0)
首先,这条线没有编译,因为你要将一个字符串赋给一个int:
location = symbol;
我认为你打算在这里这样做?
board[row][col] = symbol;
然后,switch语句中的索引关闭。例如,9
应转换为第4行和第4列:
最后一点,你要写的东西......
if(board[i][0].equals("X") == true)
...你可以把它们写成......
if(board[i][0].equals("X"))
...并获得相同的行为。如果语句中的表达式的计算结果为true,则语句执行正文中的代码。你正在做一个没有做任何事情的额外步骤,因为将任何布尔值与true
进行比较只会返回你所拥有的布尔值。