-4

我正在为一门涉及骑士之旅的启发式编程课程做作业。目前,我有一种方法,旨在用“棋盘”对象填充 8x8 棋盘数组,这些对象知道棋盘上该空间的“可访问性”,以及该棋盘是否曾经被实际访问过。但是,当我创建 8x8 数组并尝试为此数组调用我的“填充”方法时,我收到一条错误消息,告诉我该数组在我正在使用的包中不存在......我在这里做错了什么?不应该像声明数组和调用方法一样简单吗

Board [][] chessboard = new Board [8][8];
chessboard.fill();

还是我的语法错误?这里的参考是我的代码,它创建了我的 Board 对象、我的可访问性矩阵,然后将这些可访问性值复制到我的 8x8 数组上的每个 Board 对象。谢谢!

public class Board {

/*
 * Initialize array that emulates chessboard. Will be 8x8, each space will 
 * contain the number of squares from which that space can be reached. The 
 * knight will start at a new space each tour and choose each move based on
 * the "accessability" of each square within its "move pool". The knight 
 * will move to the square with least accesibility each time. When the 
 * Knight is moved to a square, this square will be marked as visited so 
 * that it cannot be visited again. Also, any space that could have been 
 * moved to but was not will have its accesability reduced by 1.     
 */

private boolean visited;
private int accessValue;
private Board [][] chess = new Board[8][8];

public Board(int acessability, boolean beenVisited)
{
    visited = beenVisited;
    accessValue = acessability;  
}

int [][] accessMatrix = {{2,3,4,4,4,4,3,2},
                        { 3,4,6,6,6,6,4,3 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
            { 4,6,8,8,8,8,6,4 },
            { 3,4,6,6,6,6,4,3 },
            { 2,3,4,4,4,4,3,2}};





public void fill()
{

for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[0][i].changeAccess(accessMatrix[0][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[1][i].changeAccess(accessMatrix[1][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[2][i].changeAccess(accessMatrix[2][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[3][i].changeAccess(accessMatrix[3][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[4][i].changeAccess(accessMatrix[4][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[5][i].changeAccess(accessMatrix[5][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[6][i].changeAccess(accessMatrix[6][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[7][i].changeAccess(accessMatrix[7][i]);
}

}   




public int getAccess()
{
return accessValue;
}

public int changeAccess(int newAccess)
{
int accessNew;
accessNew = newAccess;
return accessNew;    
}
4

1 回答 1

2

在这段代码中:

Board [] chessboard = new Board [8][8];

a 的类型Board[8][8]Board[][]

所以应该这样写来编译:

Board [][] chessboard = new Board [8][8];

(然后你必须在你的数组中创建每个 Board() 对象,我把它留给你作为练习)

在这段代码中:

chessboard.fill();

您正在调用 Board 的方法。您只能在 Board 对象上执行此操作,而不能在数组上执行此操作。如果要在数组中的每个板对象上调用此方法,则必须执行以下操作:

for (int i = 0; i <8; i ++) {
    for (int j = 0; j < 8; j ++){
         chessboard[i][j].fill();
    }
}

但我觉得还有更多,因为有一些混乱。我认为棋盘是棋盘,而不是棋盘阵列。您可能想要创建一个棋盘并填充一次。正确的?然后只需执行以下操作:

 Board chessboard = new Board();
 chessboard.fill();
于 2013-02-10T18:01:38.090 回答