2016-04-19 9 views
0
import java.io.*; 
import java.util.*; 

public class CookieMonster1 { 

public static int [][] maze; 
public static int counter; 
public static Stack<CookiePosition> path;   
public static CookiePosition posLast2; 

public static void main(String[] args){ 

    path = new Stack<CookiePosition>(); 
    CookiePosition posCurrent = new CookiePosition(); 
    posLast2 = new CookiePosition(); 
    path.push(posCurrent); 
    maze = getMaze(); 
    counter = maze[0][0]; 
    System.out.print(path); 

    while(posCurrent.getX()!=11 && posCurrent.getY()!=11) 
    { 
     posCurrent = move(posCurrent);         // make a move 
               // output the maze-just to check if working   
     for(int row=0; row<maze.length; row++) 
     { 
      for(int col=0; col<maze[row].length; col++) 
      { 
       if(posCurrent.getX()==row && posCurrent.getY()==col) 
        System.out.print("[!]"); 
       else if(maze[row][col]==-1) 
        System.out.print("[*]"); 
       else 
        System.out.print("["+maze[row][col]+"]"); 
      } 
      System.out.println(); 
     } 
    } 

    System.out.println(counter); 
} 

//checks if the pointer is able to move to the next position 
public static boolean canMove(int x, int y) 
{ 
    if(x!=maze.length && y!=maze[x].length && maze[x][y]!=-1) 
     return true; 
    return false; 
} 

//makes cookie position final 
public static CookiePosition lock(CookiePosition x){ 
    final CookiePosition hold = x; 
    return hold; 
} 

public static CookiePosition move(CookiePosition posCurrent1) 
{ 
    System.out.println("Before Move "+path.peek()); 
             // check can move right or down, move down 
    if(canMove((int)posCurrent1.getX()+1, (int)posCurrent1.getY()) && canMove((int)posCurrent1.getX(), (int)posCurrent1.getY()+1)) 
    { 
     System.out.println("Both"); 
     posLast2.setX(posCurrent1.getX());      // remember last spot with both moves 
     posLast2.setY(posCurrent1.getY()); 
             //if can move either way or down always down 
     posCurrent1.setX(posCurrent1.getX()+1); 
     path.push(lock(posCurrent1)); 
     counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()]; 
     System.out.println(counter + " Cookies"); 
     //maze[pos1.getX()][pos1.getY()]=0; 
     System.out.println(path); 

    } 
              // check if can move down 
    else if (canMove((int)posCurrent1.getX()+1, (int)posCurrent1.getY())) 
    { 
     posCurrent1.setX(posCurrent1.getX()+1); 
     path.push(lock(posCurrent1));   
     counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()]; 
     System.out.println(counter + " Cookies"); 
     //maze[pos1.getX()][pos1.getY()]=0; 
     System.out.println(path); 
    }   
              // check can move right 
    else if(canMove((int)posCurrent1.getX(), (int)posCurrent1.getY()+1)) 
    { 
     System.out.println("Right"); 
     posCurrent1.setY(posCurrent1.getY()+1); 
     path.push(lock(posCurrent1)); 
     counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()]; 
     System.out.println(counter + " Cookies"); 
     //maze[pos1.getX()][pos1.getY()]=0; 
     System.out.println(path); 
    } 
              // cannot move, back up 
    else 
    { 
     System.out.println("Go back to last spot where 2 moves were possible."); // test line 
     while(posCurrent1.getX()!=posLast2.getX() || posCurrent1.getY()!=posLast2.getY()) 
     { 
      System.out.println(path); 
      System.out.println("Removed "+ path.pop()); 
      counter-=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()]; 
      System.out.println("Stack empty? "+path.empty()); // test line 
      System.out.println("Current Top "+path.peek()); // test line 
      posCurrent1=path.peek(); 
      System.out.println(counter + "Cookies"); 
     } 
     posCurrent1.setY(posCurrent1.getY()+1); 
     path.push(new CookiePosition((int)posCurrent1.getY(), (int)posCurrent1.getY())); 
     counter+=maze[(int)posCurrent1.getX()][(int)posCurrent1.getY()]; 
     //maze[pos1.getX()][pos1.getY()]=0; 
    } 
    System.out.println("After Move "+path.peek()); // test line 
    return posCurrent1; 
} 

//reads the maze from the text file 
public static int[][] getMaze() 
{ 
    File file = new File("cookies.txt"); 
    Scanner input = null; 
    try 
    { 
     input = new Scanner(file); 
    } 
    catch (FileNotFoundException ex) 
    { 
     System.out.println("Cannot open file"); 
     System.exit(1); 
    } 
    int [][] maze = new int[12][12]; 
    int row, col, temp; 

    for (row = 0; row<maze.length; row++) 
     for (col = 0; col<maze[row].length;col++) 
      maze[row][col] = input.nextInt(); 
    for (row = 0; row<maze.length; row++) 
    { 
     for (col = 0; col<maze[row].length;col++) 
      System.out.print(maze[row][col]+" "); 
     System.out.println(); 

    } 
    return maze; 
} 

} 

このコードをコンパイルして実行すると、すべてが正常に動作し、最後の "else"ブロックがmoveメソッドに渡されます。私は問題をスタックに絞り込んだ。私はそれを印刷すると、同じ座標をすべて印刷します。ただし、すべての座標が異なる必要があります。スタックを修正するには?

これを修正するにはどうすればよいですか?

答えて

0

問題は方法上記の方法は、現在の位置を取り込む

public static CookiePosition move(CookiePosition posCurrent1) 

の戻り値であり、値を修正し、それを返します。 次のメソッドは、内部フィールドが変更されたときに毎回同じ参照を返し続けます。

posCurrent = move(posCurrent); 

実際には、スタックはすべて同じオブジェクト参照を指す一連のオブジェクトで終了します。これは、スタックがそのすべての要素に対して同じ座標値を表示している理由を説明しています。

ソリューション

moveメソッド(posCurrent)はCookiePositionの新しいインスタンスを返すことを確認してください。既存のインスタンスから値を読み取るCookiePosition用の新しいコンストラクタを作成することができます。

E.g. return new CookiePosition(posCurrent); 
関連する問題