我必须将这个迷宫保存在一个数组中,它会检查单元格中是否有两位数的数字,该数字包含指向下一个单元格的线索。包含宝藏的单元格是拥有自己坐标的单元格。
在我的例子中,它是单元格 52,因为它保存在 (5, 2) 中,或者在这种情况下,它保存在 (4, 1) 中,因为数组从 0 开始。
我的问题是,我的程序正在读取数字 1。在第一条线索中,它通向 34,这应该将我带到单元格 (2, 3)。而是将我带到 (1, 2)。
我认为这是因为 -1,但由于我正在处理人类数字,我需要减去 1,因此它对应于 0 作为第一个而不是 1。
public class MazeTest
{
public static void main(String args[])
{
int initRow = 1;
int initCol = 1;
Maze myMaze = new Maze();
myMaze.SetCoordOne(initRow);
myMaze.SetCoordTwo(initCol);
System.out.println("Checking the initial cell");
myMaze.CheckCell();
while (myMaze.GetFound() == false)
{
System.out.println("Checking the next cell.");
myMaze.ReadClue(myMaze.GetCoordOne() - 1, myMaze.GetCoordTwo() - 1);
myMaze.NextCell(myMaze.GetClueOne() - 1, myMaze.GetClueTwo() - 1);
myMaze.CheckCell();
}
}
}
public class Maze
{
private int[][] mazeCell = {{34, 21, 32, 41, 25},
{14, 42, 43, 14, 31},
{54, 45, 52, 42, 23},
{33, 15, 51, 31, 35},
{21, 52, 33, 13, 23} };
private int coordOne;
private int coordTwo;
private int clueOne;
private int clueTwo;
private boolean found = false;
public void ReadClue(int row, int col)
{
clueOne = mazeCell[row][col] / 10;
clueTwo = mazeCell[row][col] % 10;
}
public void NextCell(int rowNum, int colNum)
{
coordOne = rowNum;
coordTwo = colNum;
}
public void CheckCell()
{
System.out.printf("Checking for treasure in %d\n",
mazeCell[coordOne - 1][coordTwo - 1]);
if (coordOne == clueOne && coordTwo == clueTwo)
{
TreasureFound();
}
}
public void TreasureFound()
{
System.out.println("Congratulations, you found the treasure!");
found = true;
}
public int GetMazeCell(int row, int col)
{
return mazeCell[row][col];
}
public int GetCoordOne()
{
return coordOne;
}
public void SetCoordOne(int num)
{
coordOne = num;
}
public int GetCoordTwo()
{
return coordTwo;
}
public void SetCoordTwo(int num)
{
coordTwo = num;
}
public int GetClueOne()
{
return clueOne;
}
public void SetClueOne(int num)
{
clueOne = num;
}
public int GetClueTwo()
{
return clueTwo;
}
public void SetClueTwo(int num)
{
clueTwo = num;
}
public boolean GetFound()
{
return found;
}
}