您好我一直在做家庭作业,在没有Java的GUI的情况下制作3D tic tac toe游戏。我无法检查获胜方法的运行情况。我尝试了很多不同的格式,但我总是收到错误:
method checkForWinner in class TicTacToe3D cannot be applied to given types;
这是我的代码:
// Java 2 Assignment 1 Tic Tac Toe 3D
// Make a 3D tic tac toe game for two players with no GUI
// Check if there is a winner, loser, continue playing, or draw (no more
moves left on board)
import java.util.Scanner;
public class TicTacToe3D
{
String [][][]board = new String [3][3][3]; //3 dimensional array for tic
tac toe board
final static String WIN = "Player 1 has won the game!";
final static String WIN2 = "Player 2 has won the game!";
final static String DRAW = "There are no moves left. It is a draw.";
//final static boolean CONTINUE = true;
final static String CONTINUE = "Next player.";
boolean gameOver = false;
static int player = 0;
// constructor to initialize array and playersChoice
public TicTacToe3D()
{
initializeBoard();
} // end constructor
// Create new board
public void initializeBoard()
{
int i, j, k;
// loop through array rows
for ( i = 0; i < 3; i++)
{
// loop thourgh array columns
for ( j = 0; j < 3; j++)
{
// loop through array diagonally
for ( k = 0; k < 3; k++)
{
board[i][j][k] = " ";
} // end inner for loop
} // end for loop
} // end outer for loop
} // end method initialieBoard
// print tic tac toe board
public void printBoard()
{
// first board
System.out.println("Top Board");
System.out.println(" 1 2 3");
System.out.println("1" + board[0][0][0] + " | " + board[0][0][1] + " | " + board[0][0][2]);
System.out.println("---+----+---");
System.out.println("2" + board[0][1][0] + " | " + board[0][1][1] + " | " + board[0][1][2]);
System.out.println("---+----+---");
System.out.println("3" + board[0][2][0] + " | " + board[0][2][1] + " | " + board[0][2][2]);
System.out.println();
//second board
System.out.println("Middle Board");
System.out.println(" 1 2 3");
System.out.println("1" + board[1][0][0] + " | " + board[1][0][1] + " | " + board[1][0][2]);
System.out.println("---+----+---");
System.out.println("2" + board[1][1][0] + " | " + board[1][1][1] + " | " + board[1][1][2]);
System.out.println("---+----+---");
System.out.println("3" + board[1][2][0] + " | " + board[1][2][1] + " | " + board[1][2][2]);
System.out.println();
// third board
System.out.println("Bottom Board");
System.out.println(" 1 2 3");
System.out.println("1" + board[2][0][0] + " | " + board[2][0][1] + " | " + board[2][0][2]);
System.out.println("---+----+---");
System.out.println("2" + board[2][1][0] + " | " + board[2][1][1] + " | " + board[2][1][2]);
System.out.println("---+----+---");
System.out.println("3" + board[2][2][0] + " | " + board[2][2][1] + " | " + board[2][2][2]);
System.out.println();
} // end method printBoard
// method to make a move
public void makeAMove()
{
Scanner input = new Scanner( System.in );
int row = 0, column = 0, pla = 0;
char piece;
int pieceX = 0;
int pieceO = 0;
System.out.printf("Enter 1 for player 1 or 2 for player 2.: ", player);
player = input.nextInt();
// while( != gameOver)
// {
if (player == 1)
{
System.out.println("Player 1 enter board number (0-2): ");
pla = input.nextInt();
if ( (pla >= 0) || (pla <= 2))
{
System.out.println("Player 1 enter row number (0-2): ");
row = input.nextInt();
if ( (row >= 0) || (row <= 2))
{
System.out.println("Player 1 enter column number (0-2): ");
column = input.nextInt();
}
else if ( ( pla < 0 ) || ( pla > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a board number." );
pla = input.nextInt();
}
else if ( ( row < 0 ) || ( row > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a row number." );
row = input.nextInt();
}
else if ( ( column < 0 ) || ( column > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a column number." );
column = input.nextInt();
}
// piece = "X";
board[pla][row][column] = "X";
printBoard();
// checkForWinner();
} // end if statement
} // end if statement
else if (player == 2)
{
System.out.println("Player 2 enter board number (0-2): ");
pla = input.nextInt();
if ( (pla >= 0) || (pla <= 2))
{
System.out.println("Player 2 enter row number (0-2): ");
row = input.nextInt();
}
else if ( (row >= 0) || (row <= 2))
{
System.out.println("Player 2 enter column number (0-2): ");
column = input.nextInt();
}
else if ( ( pla < 0 ) || ( pla > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a board number." );
pla = input.nextInt();
}
else if ( ( row < 0 ) || ( row > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a row number." );
row = input.nextInt();
}
else if ( ( column < 0 ) || ( column > 2))
{
System.out.println(" Please only enter a number between 0 and 2 for a column number." );
column = input.nextInt();
}
board[pla][row][column] = "O";
printBoard();
// checkForWinner();
} // end else if statement
// } // end while statement
} // end method makeAMove
public String checkForWinner( String[][][]board )
{
// check win for rows, columns, and diagonally
// check diagonals going down left
if ( board[0][0][0] == "X" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[0][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[1][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[0][0][0] == "X" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[0][0][0] == "O" && board[0][0][0] == board[2][1][1] && board[0][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[0][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[1][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[1][0][0] == "X" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[1][0][0] == "O" && board[1][0][0] == board[2][1][1] && board[1][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[0][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[1][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[0][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[1][2][2] )
{
return WIN2;
}
else if ( board[2][0][0] == "X" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN;
}
else if ( board[2][0][0] == "O" && board[2][0][0] == board[2][1][1] && board[2][0][0] == board[2][2][2] )
{
return WIN2;
}
// check diagonals going up right
else if ( board[0][2][0] == "X" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[0][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[1][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[0][2][0] == "X" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[0][2][0] == "O" && board[0][2][0] == board[2][1][1] && board[0][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[0][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[1][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[1][2][0] == "X" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[1][2][0] == "O" && board[1][2][0] == board[2][1][1] && board[1][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[0][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[1][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[0][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[1][0][2] )
{
return WIN2;
}
else if ( board[2][2][0] == "X" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN;
}
else if ( board[2][2][0] == "O" && board[2][2][0] == board[2][1][1] && board[2][2][0] == board[2][0][2] )
{
return WIN2;
}
// check rows
for ( int y = 0; y < 3; y++ )
{
for ( int z = 0; z < 3; z++)
{
if ( board[y][z][0] == "X" && board[y][z][0] == board[y][z][1] && board[y][z][1] == board[y][z][2] )
{
return WIN;
}
else if (board[y][z][0] == "O" && board[y][z][0] == board[y][z][1] && board[y][z][1] == board[y][z][2])
{
return WIN2;
}
} // end for loop
} // end for loop
// check columns
for ( int a = 0; a < 3; a++ )
{
for ( int b = 0; b < 3; b++)
{
for ( int k = 0; k < 3; k++)
{
if ( board[0][a][b] == "X" && board[0][a][b] == board[1][a][b] && board[1][a][b] == board[2][a][b] )
{
return WIN;
}
else if ( board[0][a][b] == "O" && board[0][a][b] == board[1][a][b] && board[1][a][b] == board[2][a][b] )
{
return WIN2;
}
} // end inner for loop
} // end for loop
} // end outer for loop
// check for draw or continue
for ( int d = 0; d < 3; ++d )
{
for ( int c = 0; c < 3; ++c )
{
for ( int z = 0; z < 3; z++)
{
if ( board[d][c][z] == " " )
{
return CONTINUE; // game is not finished
}
} // end inner for loop
} // end for loop
} // outer for loop
return DRAW; // game is a draw
} // end method checkForWinner
} // end class TicTacToe3D
和
public class TicTacToe3DTest
{
public static void main(String [] args)
{
TicTacToe3D game = new TicTacToe3D();
game.makeAMove()
game.printBoard();
game.checkForWinner();
} // end main method
} // end class TicTacToe3DTest
非常感谢有关如何让方法checkForWinner()
与array
的测试类一起运行的任何反馈。
答案 0 :(得分:2)
错误的关键是,在调用方法时,您的方法调用必须匹配其中一个声明的方法签名。
您已声明checkForWinner(...)
采用3D数组参数:
public String checkForWinner( String[][][]board ) {
// .....
但是当你调用它时,你没有传入任何数组就这样做了:
checkForWinner(); // ..... no array passed in!
一种可能的解决方案:既然你只在这里使用一个类,你可以从方法声明中删除数组,并始终测试类中的数组字段。
换句话说,改变这个:
public String checkForWinner( String[][][]board )
到此:
public String checkForWinner()
顺便说一句,我会让这个方法返回boolean
,而不是String
。或者,如果需要返回多个状态,则该方法返回枚举常量。