2011-12-12 12 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已經遞減,但線索不是。