Java Maze Program Using Stack

Cannot figure out how to move about the maze.

Page 1 of 1

1 Replies - 8902 Views - Last Post: 03 March 2010 - 12:00 PM Rate Topic: -----

#1 Guest_Andy*


Reputation:

Java Maze Program Using Stack

Posted 03 March 2010 - 10:56 AM

Ok, so I'm having trouble with this maze program, I can generate a maze and all but I cannot figure out how to move around and find empty locations. I made my own class that points to the current location called MazePoint:

public class MazePoint
{

	int row, col;
	
	public MazePoint()
	{
		row = 0;
		col = 0;
		}
	
	public MazePoint(int r, int c)
	{
		this.row = r;
		this.col = c;
		}
		
	public int getRow()
	{
		return this.row;
		}
		
	public int getCol()
	{
		return this.col;
		}
		
	public int moveNorth(int m)
	{
		return this.row + m;
		}
	public int moveSouth(int m)
	{
		return this.row - m;
		}
	public int moveEast(int m)
	{
		return this.col + m;
		}
	public int moveWest(int m)
	{
		return this.col - m;
		}
		
	}


______________________________________________________________________________

And here is the actual maze program...

import java.util.*;
import javax.swing.*;

public class Maze
{

	public static void main(String[] args)
	{
		String[][] maze = new String[20][20];				
		Stack<String> tracking = new Stack<String>(); 
		MazePoint S;
		
		String sc = JOptionPane.showInputDialog("Enter starting point row, values 2 - 19.");
		String sr = JOptionPane.showInputDialog("Enter starting point column, values 2 - 19.");
		int sci = Integer.parseInt(sc);
		int sri = Integer.parseInt(sr);
		
		String ec = JOptionPane.showInputDialog("Enter ending point row, values 2 - 19.");
		String er = JOptionPane.showInputDialog("Enter ending point column,values 2 - 19.");
		int eci = Integer.parseInt(ec);
		int eri = Integer.parseInt(er);
		
		S = new MazePoint(sri-1, sci-1);
		
		fillMaze(maze, tracking, sri, sci, eri, eci);
		displayMaze(maze);
		
		getPossiblePaths(maze, tracking, S);
		
		System.out.println(tracking.peek());

		
		
		}//End Main.

	public static void fillMaze(String[][] a, Stack<String> s, int sci, int sri, int eci, int eri)
	{
		for(int i = 0; i < a.length; i++)
		{
			for(int j = 0; j < a[i].length; j++)
			{
				if(Math.random() < 0.2)
				{
					a[i][j] = "#";
					}
				else
					a[i][j] = " ";
					
				a[i][0] = "#";		//
				a[0][i] = "#";		//Set Border.
				a[19][j] = "#";	//
				a[i][19] = "#";	//
				}
			}
		
		a[sri-1][sci-1] = "S";

		a[eri -1][eci-1] = "E";

		}
		
	public static void displayMaze(String[][] a)
	{
		String m = "";
		
		for(int i = 0; i < a.length; i++)
		{
			for(int j = 0; j < a[i].length; j++)
			{
				m+= a[i][j];

				}
				m += "\n";
			}
			
		System.out.println(m);
	}
	
	public static void getPossiblePaths(String[][] a, Stack<String> s, MazePoint p)
	{
		boolean done = false;
		
		int col = p.getCol();
		int row = p.getRow();
		s.push(a[row][col]);
		
		MazePoint np = new MazePoint(row, col);
		
		int no = 1, so = 1, ea = 1, we = 1;
		
		while(done != true)
		{
			if(s.peek() == "E")
				done = true;
				
			else
			{
			if(a[row][col+ea] == " ")
			{
				s.push(a[row][col+ea]);
				np = new MazePoint(row, col+ea);
				ea++;
				}
			else if(a[np.getRow()][np.getCol()-we] == " ")
			{
				s.push(a[row][col-we]);	
				np = new MazePoint(row, col-we);
				we++;
				}
			else if(a[row+no][col] == " ")
			{
				s.push(a[row+no][col]);
				np = new MazePoint(row+no, col);
				no++;
				}
			else if(a[row-so][col] == " ")
			{
				s.push(a[row-so][col]);
				np = new MazePoint(row-so, col);
				so++;
				}
				
			else
			{
				done = true;
				System.out.println("Dead End.");
				}
				
			}
		
		}
		
		


		}//End Method.
		
	}//End Class.



I have no idea if I'm overcomplicating this or what, I keep getting and indexoutofbounds error that says -2. Any help in the right direction would be appreciated!

This post has been edited by pbl: 03 March 2010 - 07:41 PM
Reason for edit:: Code tags added


Is This A Good Question/Topic? 0

Replies To: Java Maze Program Using Stack

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Java Maze Program Using Stack

Posted 03 March 2010 - 12:00 PM

the problem is that you try to compare String Objects by using the "==" operator.
you cannot use it on Strings.
the == operator in that case, checks if two references are to the same object.
you want to use equals method, since it compares values for equality.
so change your code whenever you try to use if(str1 == str2) to:
if(str1.equals(str2))

for example this line in your code:
else if(a[row+no][col] == " ")

should be:
else if(a[row+no][col].equals(" "))


also, for next time, please post your code between code tags
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1