私はtic tac toeゲームクラスを作成しようとしています。問題の1つは、スイッチメソッドを使用しなければならないmakeMoveメソッドです。私がテスタークラスを使用するとき、それは動作しません。誰かが答え/問題が何であるべきかを私に見せてもらえますか?ここで は、チックタックつま先のクラスはテスタークラスは誰かがmakeMoveメソッドで私を助けることができます
ある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");
}
}
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;
}
}
、ここでまた、私はこれを行うためにmutliple時間と曜日を試してみましたが、コードは、それがすることになっているものをしていません
あなたは何を期待していますか?結果は私にとっては公正だと思われます。あなたのプリントアウトに基づいて予期せぬことを見ることはできません – smttsp