java - How to fix stack? (more specific in description) -


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 move                                                 // output maze-just 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 pointer able move 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 both moves         poslast2.sety(poscurrent1.gety());                                         //if can move either way or down 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,     else     {         system.out.println("go last spot 2 moves 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 maze 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;      (row = 0; row<maze.length; row++)         (col = 0; col<maze[row].length;col++)             maze[row][col] = input.nextint();     (row = 0; row<maze.length; row++)     {         (col = 0; col<maze[row].length;col++)             system.out.print(maze[row][col]+"  ");         system.out.println();      }     return maze; }  } 

when compile , run code, works fine until final "else" block in move method. have narrowed problem down stack. when print it, prints of same coordinate. however, coordinates should different.

how fix this?

issue

the issue return value of method

public static cookieposition move(cookieposition poscurrent1) 

the above method takes in current position, modifies values , returns it. following method keeps returning same reference each time internal fields changed.

poscurrent = move(poscurrent);  

so in effect stack ends set of objects pointing same object reference. explains why stack displaying same coordinate values of elements.

solution

make sure method move(poscurrent) returns new instance of cookieposition. may create new constructor cookieposition read values existing instance.

e.g. return new cookieposition(poscurrent); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -