Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: AI Search Simulator Assistance

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AI Search Simulator Assistance

    I am trying to make a A-Star, Breadth-First, and Depth-First simulator for a homework assignment. I'm not sure what I am doing wrong as the search always tries to go out of bounds of the environment. I even had to add a temporary overload cheack to break out of the while statement I am using as it nevers reaches its conclusion.

    Additionally I might have the canvas upside down, not sure how I did that.

    I have included my entire Eclipse Project in the attachment. I would appreciate any assistance I can get since it is due tomorrow. I have been programming for 24+ hours straight now and need help, and yes sleep. Thank you in advance.
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    You should ask specific questions about your problem and post the code that is giving you troubles.
    If you are getting error messages, copy and paste the full text here.

    Cross posted at http://forums.devshed.com/java-help-...ml#post2691316
    Last edited by Norm; September 17th, 2011 at 07:45 AM.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: AI Search Simulator Assistance

    This sounds interesting, and I would have loved to help had you not broken pretty much every unwritten rule about forum posting.

    I recommend you read this: How to Program (stuck on homework? READ THIS BEFORE POSTING) as well as the link to asking questions the smart way in my signature.

    If you still have questions, you should post the specific piece of code giving you trouble in SSCCE form.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AI Search Simulator Assistance

    I apologize and Norm is right about the other forum, I will stop updating there. I updated the attachment with comments in the NavigationBot class since it is most definitely the source of my problem.

    Here is the discription of the classes and the purpose:
    The assignment involves a simulated 2D area with polygons whose points were given to me in the assignment.

    The bot is given a start and end point and is to navigate through them each time using whichever of the three algorithms that was specified.

    The four core classes and they purposes are that I have above are as follows:

    PathCoord: Represents any coordinate in the 2D space. That space is 300 wide by 200 height.

    Environment: Creates the polygons from the points I was given and loads them into an object array for later use. It also has two helper functions that are supposed to caclusate out the distance from the start, and the other from the end, based upon the current location.

    GUIManager: Just takes the information and displays it on a canvas. It gives a starting menu for the user to choose which of the three algorithms to use.

    NavigationBot: The core class and the source of my problems I am cerrtain. Based upon the selected algorithm, it is supposed to navigate from start to end, around the polygons.

    I have not even began the code for Breadth-First and Depth-First other than a very basic selection system. That means my code I have trouble with is for A-Star. I have updated the NavigationBot class with comments and will provide a sample of the output in my next post.

    Here is the sample of the output:
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 21
    next Y: 151
    Direction Change
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 22
    next Y: 152
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 23
    next Y: 153
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 24
    next Y: 154
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 25
    next Y: 155
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 26
    next Y: 156
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 27
    next Y: 157
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 28
    next Y: 158
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 29
    next Y: 159
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 30
    next Y: 160
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 31
    next Y: 161
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 32
    next Y: 162
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 33
    next Y: 163
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 34
    next Y: 164
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 35
    next Y: 165
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 36
    next Y: 166
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 37
    next Y: 167
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 38
    next Y: 168
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 39
    next Y: 169
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 40
    next Y: 170
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 41
    next Y: 171
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 42
    next Y: 172
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 43
    next Y: 173
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 44
    next Y: 174
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 45
    next Y: 175
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 46
    next Y: 176
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 47
    next Y: 177
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 48
    next Y: 178
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 49
    next Y: 179
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 50
    next Y: 180
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 51
    next Y: 181
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 52
    next Y: 182
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 53
    next Y: 183
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 54
    next Y: 184
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 55
    next Y: 185
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 56
    next Y: 186
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 57
    next Y: 187
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 58
    next Y: 188
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 59
    next Y: 189
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 60
    next Y: 190
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 61
    next Y: 191
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 62
    next Y: 192
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 63
    next Y: 193
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 64
    next Y: 194
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 65
    next Y: 195
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 66
    next Y: 196
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 67
    next Y: 197
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 68
    next Y: 198
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 69
    next Y: 199
    Not shorter distance
    In History
    Not shorter distance
    Not shorter distance
    Not shorter distance
    Not shorter distance
    next X: 70
    next Y: 200
    Not In Bounds
    Not shorter distance
    In History
    Not In Bounds
    Not shorter distance
    Not shorter distance
    Not In Bounds
    next X: 71
    next Y: 201

    Here is a picture of the program in action:
    problem.jpg
    Attached Files Attached Files

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AI Search Simulator Assistance

    I beleive the problem lies within this function:

    	//Find the next coordinate for the bot to travel to
    	public Point AStarFindNext(Point now) 
    	{
    		Point next = now; //So we don't send back a zeroed coordinate
     
    		Point test = new Point(); //For use in calculations
     
    		int dist = NavArea.GetDistFromEnd(now); //get current distance from end location
     
    		//Cycle through each possible direction to determine the best choice
    		for(int x = -1; x < 2; x++)
    		{
    			for(int y = -1; y < 2; y++)
    			{
    				//the current position plus the directional change to create a coordinate to test
    				test.x = now.x + x;
    				test.y = now.y + y;
     
    				//Get the test coordinate's distance from the end location
    				int testDist = NavArea.GetDistFromEnd(test);
     
    				//To prevent traveling backwards we check if the test location
    				if(InHistory(test) == false)
    				{
    					//Check if the test location is closer or at least equal to the current distance from
    					//the end location
    					if(testDist >= dist)
    					{
    						//Check if the test location is within the bounds of the environment
    						if(IsInBounds(test.x, test.y) == true)
    						{
    							//check to see if the test location is not within the bounds of one of the polygons
    							if(IsClear(test.x, test.y) == true)
    							{
    								//If all of those tests pass, then store the test location as the best candidate to be 
    								//the next coordiante the bot will travel.
    								next = test;
    								dist = testDist;
    							}
    							else
    								System.err.println("Not Clear"); //for testing
    						}
    						else
    							System.err.println("Not In Bounds"); //for testing
    					}
    					else
    						System.err.println("Not shorter distance"); //for testing
    				}
    				else
    					System.err.println("In History"); //for testing
    			}
    		}
     
    		System.err.println("next X: " + next.x); //for testing
    		System.err.println("next Y: " + next.y); //for testing
    		return next;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    You have a reasonable tracing print out from the code. Where in that print out are the values going wrong?

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AI Search Simulator Assistance

    It goes out of bounds and stays there. I ended the tracing print at that point. Sorry I should have stated that. It should also be making some sort of attempt to head in the direction of the end coordinate which is toward the upper right of the area, but instead it just tries to head downward at an angle.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    The method that detects "Not In Bounds".
    What can it do to tell another method about that situation?
    It prints out the next X and Y.
    Should it tell the caller there is a problem?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    Without comments I don't know why you are doing this but it could be a problem:

    	Point next = now; //So we don't send back a zeroed coordinate
    This sets a new, 2nd pointer to the point ,it does not create a new object

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AI Search Simulator Assistance

    I changed that to something a bit more reliable as well as removing the code in the AStar function that preloaded the start location. It seems to work much batter and is actually trying to navigate past the first shape now.

    Now I just get an "ArrayIndexOutOfBoundsException: 60000" issue because I am trying to add too many objects to the PathCoordArray array. This tells me that my code for determining if there is a direction change is totally wrong as it is calling it all of the time. I have included my updated code.
    Attached Files Attached Files

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    All I can suggest is to keep adding printlns to show what is happening, look at what is printed out and see if the code is doing as you want it to do.
    Add the following to your PathCoord class for better debug printouts.
          public String toString() {
              return Coord.toString();
          }

    Then you can use it in the paint method:
          System.out.println("paint i=" + i + " " + pathCoordArray[i]);
    Last edited by Norm; September 17th, 2011 at 01:50 PM.

  12. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AI Search Simulator Assistance

    Well after improving my history functions and some little tweaks to the next coordinate function I have made progress, but now it is getting stuck on one shape and I have no idea why. Any suggestions?

    Here is the updated NavigationBot code:

    import javax.swing.JFrame;
    import java.awt.Point;
     
     
    public class NavigationBot
    {
    	private static NavigationBot NavBot;
    	private static Environment NavArea;
    	private static GUIManager GMan;
    	PathCoord[] PathCoordArray;  //PathCoord Array
    	PathCoord[] HeuristicArray;  //Heuristic Array
     
    	static Point Start;
    	static Point End;
    	static Point OldStep;
    	static Point NewStep;
    	static Point Vector;
    	static Point[] History;
    	static int availLocs;
    	static int eWidth;
    	static int eHeight;
    	static int Area;
     
    	public NavigationBot() 
    	{		
    		//Environment variables
    		availLocs = 0;
    		eWidth = 300;
    		eHeight = 200;
    		Area = eWidth * eHeight;
     
    		//Actual size of the area in the assignment is only 300 X 200 and I adjusted the values to match
    		//Start Coordinates
    		Start = new Point();
    		Start.setLocation(20, 150);
     
    		//End Coordinates
    		End = new Point();
    		End.setLocation(280, 60);
     
    		PathCoordArray = new PathCoord[Area];
    		HeuristicArray = new PathCoord[Area];
     
    		OldStep = Start;
    		NewStep = new Point();		
    		Vector = new Point();	
     
    		History = new Point[12];
    	}
     
    	public static void main(String[] args) throws Exception 
    	{
    		NavBot = new NavigationBot();
    		NavArea = new Environment(Start, End, eWidth, eHeight);
    		NavArea.InitializePolygonArray();
     
    		CalculateArea();
     
    		InitMenu();
    	}
     
    	//For displaying the number of polygons in the area that are not inside the 
    	//bounds of the polygons
    	public static void CalculateArea() 
    	{
    		int counter = 0;
    		int index = 0;
     
    		for(int w = 0; w < eWidth; w++)
    		{
    			for(int h = 0; h < eHeight; h++)
    			{
    				index++;
    				for(int n = 0; n < NavArea.PolygonArray.length; n++)
    				{
    					if(NavArea.PolygonArray[n].contains(w, h) == true)
    					{
    						counter++;
    					}
    				}
    			}
    		}
     
    		availLocs = index - counter;
     
    		//System.err.println("Counter: " + counter); //for testing
    	}
     
    	//Removes the null coordinates from the array to compact the size	
    	public void CleanUpPath()
    	{
    		int Index = 0;
     
    		for(int i = 0; i < PathCoordArray.length; i++)
    		{
    			if(PathCoordArray[i] != null)
    				Index++;
    		}
    		PathCoord[] TempCoordArray = new PathCoord[Index];
     
    		for(int i = 0; i < Index; i++)
    		{
    			TempCoordArray[i] = PathCoordArray[i];
    		}
    		PathCoordArray = TempCoordArray;
    	}
     
    	//Creates a GUI Manager instance to display the startup menu where the 
    	//user can select what algorithm they want to use
    	public static void InitMenu() 
    	{
    		GMan = new GUIManager("Choose Algorithm", eWidth, eHeight, NavBot, NavArea);
    		GMan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GMan.setSize(300, 74);
    		GMan.setLocationByPlatform(true);
    		GMan.setLocationRelativeTo(null);
    		GMan.setVisible(true);
     
    		GMan.AlgorithmMenu();
    	}
     
    	//For the button in the sim area that returns the user to the startup menu
    	public void Reset() 
    	{
    		//Remove any existing GUIManager
    		GMan.dispose();
     
    		NavBot = new NavigationBot();
    		NavArea = new Environment(Start, End, eWidth, eHeight);
    		NavArea.InitializePolygonArray();
     
    		CalculateArea();
     
    		InitMenu();
    	}
     
    	//The A-Star algorith simulation
    	public void AStar() 
    	{
    		//Remove any existing GUIManager
    		GMan.dispose();
     
    		//Create GUIManager instance for displaying simulation
    		GMan = new GUIManager("A* Algorithm Simulation", eWidth, eHeight, NavBot, NavArea);
    		GMan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GMan.setSize(340, 300);
    		GMan.setLocationByPlatform(true);
    		GMan.setLocationRelativeTo(null);
    		GMan.setVisible(true);
     
    		//The beginning of the simulation
    		boolean found = false;
    		int Index = 0;
     
    		UpdateHistory(Start);
     
    		//Loops until the end coordinate is reached
    		while(found == false)
    		{
    			//Temporarily added to prevent locks created from the while statement never reaching completion
    			if(Index > 100)
    				break;
     
    			//Call function to select a new coordinate basedupon the current location
    			//The intial value is the Start location
    			NewStep = AStarFindNext(OldStep);
     
    			//Add the new coordinate to history
    			UpdateHistory(NewStep);
     
    			//To reduce the number of coordinates stored, we only add the coordinate 
    			//when the bot has had to change direction. This will provide longer line segments
    			if(AStarChangedDirection(OldStep, NewStep))
    			{
    				System.err.println("Direction Change: " + Index); //for testing
     
    				//Update the directional vector value
    				Vector.x = OldStep.x - NewStep.x;
    				Vector.y = OldStep.y - NewStep.y;
     
    				//Add the coordinate at the directional change to the array
    				PathCoord newCoord = new PathCoord();
    				newCoord.Coord = OldStep;
    				//The coordinate cost is the distance from the start
    				newCoord.Cost = NavArea.GetDistFromStart(OldStep);
    				PathCoordArray[Index] = newCoord;
    				Index++;
    			}
     
    			//Check if the new coordinate is the end location
    			if(NewStep.equals(End) == true)
    			{
    				System.err.println("End Found"); //for testing
     
    				//Add the final coordinate to the array
    				PathCoord lastCoord = new PathCoord();
    				lastCoord.Coord = NewStep;
    				lastCoord.Cost = NavArea.GetDistFromStart(NewStep);
    				PathCoordArray[Index] = lastCoord;
     
    				//set the flag to break the while statement
    				found = true;
    			}
    			//Save the new step as the old step
    			OldStep = NewStep;
    		}
     
    		CleanUpPath();//Remove nulls spaces from the array for memory
     
    		//Send the number of locations not within one of the polygons for reference use only
    		//Also send the path so it can be displayed 
    		GMan.DisplaySimulation(availLocs, PathCoordArray);
    	}
     
    	//Find the next coordinate for the bot to travel to
    	public Point AStarFindNext(Point now) 
    	{
    		//System.err.println("AStarFindNext"); //for testing
    		Point next = now; //So we don't send back a zeroed coordinate
     
    		Point test = new Point(); //For use in calculations
     
    		PathCoord[] TestCoordArray;  //Testing Array
    		TestCoordArray = new PathCoord[8]; //only 8 possible directions
    		int index = 0;
     
    		//Cycle through each possible direction and creat an array of PathCoords
    		for(int x = -1; x < 2; x++)
    		{
    			for(int y = -1; y < 2; y++)
    			{
    				//the current position plus the directional change to create a coordinate to test
    				test.x = now.x + x;
    				test.y = now.y + y;
     
    				if(test.equals(now) == false)
    				{
    					//Get the test coordinate's distance from the end location
    					int testDist = NavArea.GetDistFromEnd(test);
     
    					PathCoord testCoord = new PathCoord();
    					testCoord.Coord.setLocation(test);
    					testCoord.Cost = testDist;
    					TestCoordArray[index] = testCoord;
    					index++;
    				}
    			}
    		}
     
    		int lastCost = Area;
    		for(int i = 0; i < index; i++)
    		{
    			//Check if the test location is within the bounds of the environment
    			if(IsInBounds(TestCoordArray[i].Coord) == true)
    			{
    				//check to see if the test location is not within the bounds of one of the polygons
    				if(IsClear(TestCoordArray[i].Coord) == true)
    				{
    					if(InHistory(TestCoordArray[i].Coord) == false)
    					{
    						//System.err.println("Cost: " + TestCoordArray[i].Cost); //for testing
    						if(TestCoordArray[i].Cost < lastCost)
    						{
    							//If all of those tests pass, then store the test location as the best candidate to be 
    							//the next coordiante the bot will travel.
    							next = TestCoordArray[i].Coord;
    							lastCost = TestCoordArray[i].Cost;
    						}
    					}
    					//else
    						//System.err.println("In History"); //for testing
    				}
    				//else
    					//System.err.println("Not Clear"); //for testing
    			}
    			//else
    				//System.err.println("Not In Bounds"); //for testing			
    		}
    		System.err.println("next X: " + next.x); //for testing
    		System.err.println("next Y: " + next.y); //for testing
    		return next;
    	}
     
    	//Compare the difference between  the old step and new step to the directional
    	//vector from the last step to determine if the bot is still traveling in the 
    	//same direction
    	public boolean AStarChangedDirection(Point old, Point now) 
    	{
    		int CurrX = OldStep.x - NewStep.x;
    		int CurrY = OldStep.y - NewStep.y;
    		System.err.println("CurrX: " + CurrX); //for testing
    		System.err.println("CurrY: " + CurrY); //for testing
    		System.err.println("Vector X: " + Vector.x); //for testing
    		System.err.println("Vector Y: " + Vector.y); //for testing
     
    		if(CurrX != Vector.x)
    			return true;
     
    		if(CurrY != Vector.y)
    			return true;
     
    		return false;
    	}
     
    	//Checks if the coordinate is in history
    	public boolean InHistory(Point pnt) 
    	{
    		for(int i = 0; i < 12; i++)
    		{
    			if(History[i] != null)
    				if(History[i].equals(pnt))
    					return true;
    		}
     
    		return false;
    	}
     
    	//Adds a new coordinate to historyv at location 0 and pushes the rest back, dropping the last
    	public void UpdateHistory(Point pnt) 
    	{
    		Point[] temp = new Point[12];
    		for(int i = 0; i < 12; i++)
    		{
    			temp[i] = History[i];
    		}
    		History[0] = pnt;
    		for(int i = 0; i < 11; i++)
    		{
    			History[i+1] = temp[i];
    		}
    	}
     
    	//Check if the coordinate is within the environment boundries
    	public boolean IsInBounds(Point test) 
    	{
    		boolean clear = true;
    		if(test.x < 0)
    		{
    			clear = false;
    		}
    		if(test.x > eWidth)
    		{
    			clear = false;
    		}
     
    		if(test.y < 0)
    		{
    			clear = false;
    		}
    		if(test.y > eHeight)
    		{
    			clear = false;
    		}
    		return clear;
    	}
     
    	//Check if the coordinate is not within one of the polygons
    	public boolean IsClear(Point test) 
    	{
    		boolean clear = true;
    		for(int n = 0; n < NavArea.PolygonArray.length; n++)
    		{
    			if(NavArea.PolygonArray[n].contains(test) == true)
    			{
    				clear = false;
    			}
    		}
     
    		return clear;		
    	}
     
    	public void BreadthFirst() 
    	{
    		GMan.dispose();
     
    		GMan = new GUIManager("Breadth-First Algorithm Simulation", eWidth, eHeight, NavBot, NavArea);
    		GMan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GMan.setSize(340, 300);
    		GMan.setLocationByPlatform(true);
    		GMan.setLocationRelativeTo(null);
    		GMan.setVisible(true);
     
    		GMan.DisplaySimulation(availLocs, PathCoordArray);
    	}
     
    	public void DepthFirst() 
    	{
    		GMan.dispose();
     
    		GMan = new GUIManager("Depth-First Algorithm Simulation", eWidth, eHeight, NavBot, NavArea);
    		GMan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		GMan.setSize(340, 300);
    		GMan.setLocationByPlatform(true);
    		GMan.setLocationRelativeTo(null);
    		GMan.setVisible(true);
     
    		GMan.DisplaySimulation(availLocs, PathCoordArray);
    	}
    }

    Here is a picture of the new problem:
    problem2.jpg
    Last edited by GeekWarth; September 17th, 2011 at 03:25 PM. Reason: Horrible Typing

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: AI Search Simulator Assistance

    Some comments on your coding style
    There are too many static variables and methods.
    I change your main to:
           public static void main(String[] args) throws Exception 
    	{
    		new NavigationBot();
    	}
    and moved the rest of what was in your main to the end of the constructor.
    I removed all static modifiers from the variables.

    and added this to the constructor vs changing the code to use this:
    NavBot = this;

    You don't follow the java naming standards: Lowercase first letters for variable and method names

Similar Threads

  1. Creating a Simulator
    By william in forum Java Theory & Questions
    Replies: 4
    Last Post: June 16th, 2011, 10:11 AM
  2. NEED ASSISTANCE W/ JAVA PROGRAM
    By Neddrick in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 29th, 2010, 08:20 PM
  3. array program assistance needed
    By JavaNoob82 in forum Collections and Generics
    Replies: 4
    Last Post: December 14th, 2009, 04:49 AM
  4. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 12:12 AM
  5. JAVA simulator
    By YAS218 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 20th, 2009, 09:57 AM