2011-12-12 13 views
0

我必须使这个迷宫保存在一个数组中,并且它检查单元格是否有一个两位数的数字,它包含下一个单元格的线索。包含宝藏的单元格是拥有自己的坐标的单元格。Java程序检查数组迷宫的线索 - 答案总是1关闭

在我的情况,它的电池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; 
    } 
} 

回答

0

你有决定是否要保持线索和协调为零或一为主。

ReadClue您将基于一个值分配给线索。

当致电myMaze.NextCell(myMaze.GetClueOne() - 1, myMaze.GetClueTwo() - 1);时,您将使坐标为零。

CheckCell()中,您可以比较基于一条的线索和从零开始的坐标。这就是为什么它不起作用。

将您的来电改为NextCell:myMaze.NextCell(myMaze.GetClueOne(), myMaze.GetClueTwo());,这将使坐标和线索都基于一个,然后您的程序应该可以工作。

0

我想,你的代码中有太多的减法操作。

当你调用NextCell()时,你减少了坐标,所以你的索引从0开始。 在下一步中,CheckCell()会打印一个mazeCell,coords会再次递减,这可能看起来像你需要1,2)而不是(2,3);

另一件事是,在CheckCell中,您将coords与线索进行比较,coords已经递减,但线索不是。