对不起,我是初学者,仍在学习编码,但我似乎无法找到错误。我的编译器告诉我,方法大小(int)未定义为Board 类型。我在Board中有更多的代码,要求我返回大小,但我无法超越Board.size的障碍。还要感谢到目前为止的所有贡献,它确实有很长的路要走。
public class LOA{
public static void main(String[] args){
//First ensure the correct number of cmd line arguments.
if (args.length < 2) {
throw new IllegalArgumentException("Error: too few arguments");
}
try {
int size = Integer.parseInt(args[0]);
} catch (NumberFormatException nbfe){}
try {
int mode = Integer.parseInt(args[1]);
} catch (NumberFormatException nbfe) {
}
int size = Integer.parseInt(args[0]);
int mode = Integer.parseInt(args[1]);
//Then check to make sure that a legal board has been selected.
if ((size < 4) || (size > 16)){
throw new IllegalArgumentException("Error: illegal size");
}
if(mode != 0){
if(mode != 1){
if( mode != 2){
throw new IllegalArgumentException("Error: illegal mode");
}
}
}
//Test mode
if(mode == 0){
Board.size(size);
Board.newBoard(size);
Board.displayBoard(0);
}
//Single-player mode
//Multiplayer mode
}
}
/*
* This class implements the board. It stores an array that contains the
content
* of the board, and several functions that perform operations such as
moving a
* piece or counting the number of markers in a row, column, or diagonal.
*/
public class Board {
// The size of the board
private static int size;
// The pieces
public static final int INVALID = -1;
public static final int EMPTY = 0;
public static final int WHITE = 1;
public static final int BLACK = 2;
// The board
private static int[][] board;
// Convention: board[0][0] is the southwest square
/*
* Create and set up a new board
*/
public static void newBoard(int newSize) {
/*--------------------------------------------
* FILL IN THE CODE FOR INITIALIZING THE BOARD
*-------------------------------------------*/
//The Blank board
int N = newSize + 1;
int[][] board = new int[N][N];
int i, k;
for (i = 0; i < N; i++){
for( k = 0 ; k < N ; k++){
board[i][k] = 0;
}
}
//The top row of Black pieces.
i = 0;
while(i == 0){
for (k = 1; k < N-1; k++){
board[i][k] = 2;}
i++;
}
//The bottom row of Black pieces.
i = N-1;
while(i == N-1){
for (k = 1; k < N-1; k++){
board[i][k] = 2;}
i++;
}
//The left column of White pieces.
k = 0;
for(i = 1; i < N-1 ; i++){
board[i][k] = 1;}
//The right column of White pieces.
k = N-1;
for(i = 1; i < N-1 ; i++){
board[i][k] = 1;}
}
//print the board
public static void displayBoard(int i){
i = 0;
for (int C = 65; C <= (size+65); C++) {
System.out.print((char)C);}
while(i < size){
int k = 0;
while(k < size){
if(board[i][k] == 0){
System.out.print("*");
System.out.print(" ");}
else if(board[i][k] == 1){
System.out.print("W");
System.out.print(" ");}
else if(board[i][k] == 2){
System.out.print("B");
System.out.print(" ");
}
k++;}
i++;
System.out.println("");}
}
/*
* Function that returns the piece currently on the board at the specified
* row and column.
*/
public static int getPiece(int row, int col) {
if ((row < 0) || (row >= size)) {
return INVALID;
}
if ((col < 0) || (col >= size)) {
return INVALID;
}
return board[row][col];
}
/*
* Make a move. Check that the move is valid. If not, return false. If
* valid, modify the board that the piece has moved from (fromRow, fromCol)
* to (toRow, toCol).
*/
public static boolean makeMove(int player, int fromRow, int fromCol, int toRow, int toCol) {
/*--------------------------------------------
* FILL IN THE CODE FOR MAKING A MOVE
*-------------------------------------------*/
return false;
}
/*
* Return the size of the board.
*/
public static int getSize() {
return size;
}
/*
* Check if the given move is valid. This entails checking that:
*
* - the player is valid
*
* - (fromRow, fromCol) is a valid coordinate
*
* - (toRow, toCol) is a valid coordinate
*
* - the from square contains a marker that belongs to the player
*
* - check that we are moving a "legal" number of squares
*/
public static boolean isValidMove(int player, int fromRow, int fromCol, int toRow, int toCol) {
/*--------------------------------------------
* FILL IN THE CODE FOR CHECKING A MOVE
*-------------------------------------------*/
return false;
}
/*
* Count the number of markers (non-empty squares) in the specified row.
*/
public static int rowCount(int row) {
/*--------------------------------------------
* FILL IN THE CODE FOR COUNTING THE MARKER IN A ROW
*-------------------------------------------*/
int rowCount = 0;
int i = 0;
for (board[row][i] = 0; row < size-1; i++){
if((board[row][i] == 1) || board[row][i] == 2) {
rowCount++;
}
}
return rowCount;
}
/*
* Count the number of markers (non-empty squares) in the specified column.
*/
public static int colCount(int col) {
/*--------------------------------------------
* FILL IN THE CODE FOR COUNTING THE MARKER IN A COLUMN
*-------------------------------------------*/
int k = 0;
int i = col;
int colCount = 0;
while (i < size) {
if ((board[i][k] == 1) || (board[i][k] == 2)){
colCount++;
i++;}
else {i++;
}
}
return colCount;
}
/*
* Count the number of markers (non-empty squares) in the diagonal that runs
* from the north-east corner to the south-west corner of the board, and
* that passes through the specified row and column.
*/
public static int diagNortheastCount(int row, int col) {
/*--------------------------------------------
* FILL IN THE CODE FOR COUNTING THE MARKER IN A DIAGONAL
*-------------------------------------------*/
return 0;
}
/*
* Count the number of markers (non-empty squares) in the diagonal that runs
* from the north-west corner to the south-east corner of the board, and
* that passes through the specified row and column.
*/
public static int diagNorthwestCount(int row, int col) {
/*--------------------------------------------
* FILL IN THE CODE FOR COUNTING THE MARKER IN A DIAGONAL
*-------------------------------------------*/
return 0;
}
public static boolean hasWon(int player) {
return Util.isConnected(board, player);
}
}
答案 0 :(得分:0)
在Board
课程中,size
是变量,而不是方法 - 它被定义为static int size
。要访问它,您只需Board.size
。例如,int x = Board.size
使x等于电路板大小。或者,您可以Board.size = 20
设置尺寸 - 它与说int x = 20
没什么不同。
您的测试模式&#39; Board.size(size)
。括号表示您尝试在Board类中调用函数size()
。并且您将从用户输入获得的变量size
传递给该函数,因此您尝试在Board中调用函数size()
并将其作为输入赋予它。因此,编译器抱怨Board中没有函数称为size,它接受int - &#34;方法size(int)是未定义的。&#34;
此外,董事会的任何领域都没有理由是静态的。
答案 1 :(得分:0)
如果这是您的Board类的所有代码,那么您将大小定义为成员属性,而不是成员函数(方法)。因此,您无法按照自己的方式调用它。
如果你想要一个返回大小的函数,你应该声明 公共类委员会{
//董事会的规模 private static int size;
如果你想要一个返回大小的函数,你应该声明
public class Board {
// The size of the board
private static int size;
int getSize()
{
return(this.size);
}