Results 1 to 5 of 5
  1. #1
    Savantx is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Trouble popping a stack

    wondering if anyone can help me. My class project is to make a program to find the end of a 10x10 maze of 1's(walls) and 0's(path). We need to use a stack and I leave a trail of 2's so each at each choice I just have to search for a 0 or pop. I'm having difficulty with some part of popping the stack. Here's a snippet of some of the code.

    case 3:
    maze.push(coor);
    row = row - 1;
    coor.setR(row);
    break;
    case 4:

    JOptionPane.showMessageDialog(null, coor.getR() +" "+ coor.getC());
    coor = maze.pop();


    row = coor.getR();
    col = coor.getC();
    JOptionPane.showMessageDialog(null, coor.getR()+" "+coor.getC());
    break;

    Coor is a class that just stores int row and int column
    The program keeps popping untill the stack becomes empty but the OptionPane I set up to test shows the same row/column after every single pop.

    Anyone know where my problem is?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Difficult to tell from what you have given us. One thig comes to mind: are you sure you are pushing different coor objects onto the stack and not X references to the same coor object.

  3. #3
    Savantx is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the reply,

    I think each coor object is different, the row/col that is returned is always 8,4 which is the point in the maze where it first needs to pop(the maze always starts at (0,0). Then it keeps returning 8,4 until the stack runs out. Here's the full code(work in progress)

    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {
    int[][] mazeArray = new int[10][10];
    Stack<Coordinates> maze = new ListStack<Coordinates>();

    int x = 0, y = 0, tempInt = 0;
    String tempString = "", path = "", endText = "";
    boolean done = false;

    File file = new File("TestMaze");
    if (file.exists()) {
    Scanner infile = new Scanner(file);
    //load maze into array
    for (int r = 0; r < 10; r++) {
    for (int c = 0; c < 10; c++) {
    tempInt = infile.nextShort();
    mazeArray[r][c] = tempInt;
    tempString = tempString + Integer.toString(tempInt);

    }
    tempString = tempString + "\n";
    infile.nextLine();

    }
    }

    JOptionPane.showMessageDialog(null, tempString);
    Coordinates coor = new Coordinates();
    // maze.push(coor);
    int row = coor.getR();
    int col = coor.getC();
    int nextMove = 0;
    int position = 0;
    while (!done) {


    if (col == 0 && row == 0) {
    position = 0;
    } else if (col == 9 && row == 0) {
    position = 1;
    } else if (col == 0) {
    position = 2;
    } else if (row == 0) {
    position = 3;
    } else if (col == 9) {
    position = 4;
    } else {
    position = 5;
    }

    switch (position) {
    case 0:
    if (mazeArray[row+1][col] == 0) {
    nextMove = 0;
    } else if (mazeArray[row][col+1] == 0) {
    nextMove = 1;
    } else {
    nextMove = 5;
    }
    break;

    case 1:

    if (mazeArray[row + 1][col] == 0) {
    nextMove = 0;
    } else if (mazeArray[row][col - 1] == 0) {
    nextMove = 2;
    } else {
    nextMove = 4;
    }
    break;

    case 2:
    if (mazeArray[row+1][col] == 0) {
    nextMove = 0;
    } else if (mazeArray[row][col+1] == 0) {
    nextMove = 1;
    } else if (mazeArray[row][col - 1] == 0) {
    nextMove = 2;
    } else {
    nextMove = 4;
    }
    break;
    case 3:
    if (mazeArray[row+1][col] == 0) {
    nextMove = 0;
    } else if (mazeArray[row][col+1] == 0) {
    nextMove = 1;
    } else if (mazeArray[row - 1][col] == 0) {
    nextMove = 3;
    } else {
    nextMove = 4;
    }
    break;
    case 4:
    if (mazeArray[row][col+1] == 0) {
    nextMove = 1;
    } else if (mazeArray[row][col - 1] == 0) {
    nextMove = 2;
    } else if (mazeArray[row - 1][col] == 0) {
    nextMove = 3;
    } else {
    nextMove = 4;
    }
    break;
    case 5:
    if (mazeArray[row+1][col] == 0) {
    nextMove = 0;
    } else if (mazeArray[row][col+1] == 0) {
    nextMove = 1;
    } else if (mazeArray[row][col-1] == 0) {
    nextMove = 2;
    } else if (mazeArray[row - 1][col] == 0) {
    nextMove = 3;
    } else {
    nextMove = 4;
    }
    break;

    }
    mazeArray[row][col] = 2;

    switch (nextMove) {
    case 0:
    maze.push(coor);
    row=row+1;
    coor.setR(row);
    break;

    case 1:
    maze.push(coor);
    col = col + 1;
    coor.setC(col);
    break;

    case 2:
    maze.push(coor);
    col = col - 1;
    coor.setC(col);
    break;

    case 3:
    maze.push(coor);
    row = row - 1;
    coor.setR(row);
    break;
    case 4:

    JOptionPane.showMessageDialog(null, row +" "+ col);
    coor = maze.pop();

    row = coor.getR();
    col = coor.getC();
    JOptionPane.showMessageDialog(null, row+" "+col);
    break;

    case 5:
    endText = "No Path!";
    done = true;
    break;


    }
    if (coor.getR() == 9) {
    endText = "Finished!!";
    done = true;
    JOptionPane.showMessageDialog(null, endText);
    }

    }

    }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Savantx View Post
    I think each coor object is different, the row/col that is returned is always 8,4
    Do you realise that those two statements contradict each other. You never create a new Coordinate object, you just keep changing the same one and adding a reference to it to the Stack. Let me demonstrate with an ArrayList.
    Java Code:
    import java.util.ArrayList;
    
    class Test {
        public static void main(String[] args) {
            ArrayList<Foo> list = new ArrayList<Foo>();
            Foo f = new Foo("Fred");
            list.add(f);
            f.setName("Betty");
            list.add(f);
            f.setName("Wilma");
            list.add(f);
            f.setName("Barney");
            list.add(f);
            for(Foo g : list) {
                System.out.println(g);
            }
        }
    }
    
    class Foo {
        private String name;
        
        Foo(String s) {
            name = s;
        }
        
        public void setName(String s) {
            name = s;
        }
        
        public String toString() {
            return name;
        }
    }
    Output is: Barney Barney Barney Barney

  5. #5
    Savantx is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 08:14 PM
  2. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 08:04 AM
  3. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  4. Need help please implementing a stack!
    By sfe23 in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 03:19 PM
  5. Stack not popping
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •