CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Apr 2009
    Posts
    2

    Check code please?

    Hi,

    I am new to Java coding (familiar with simple C++ though) and don't have a Java compiler. Tried looking for one but it there doesn't sem to be a visuall C++ type compiler for Java, i.e. I couldn't find one that seemed easy to use.

    Could you guys check this function for me and tell me if I've done anything wrong or very innefficient please?

    I am NOT allowed to use libraries or anything but the basic datatypes...

    Code:
    static int[] speedroute( boolean speedmap[][] , int cx , int cy , int dx , int dy )
    
    {
    	/* My code begins here. */
    
    	int route[] = null;
    
    	/* Using "A star" algorithm to find shortest path. Assuming we cannot move diagonally.
    	Using the Manhattan heuristic method to estimate shortest path length */
    
    	int maxx=128;
    	int maxy=128;
    
    	int listdepth = 4; /* {list?, parent x, parent y, g} */
    
    	int open[][][] = new int[maxx][maxy][listdepth];
    	int closed[][][] = new int[maxx][maxy][listdepth];
    
    	open[cx][cy] = {1,cx,cy,0};
    
    	int isfirststep=1;
    	
    	int ex=cx; int ey=cy;	
    
    	int lowestopenf=maxx+maxy;
    
    	int neighbours[][] = new int[4][2];
    
    	for(;;)
    	{		
    		for(int i=0;i<maxx;i++)
    		{
    			for(int j=0;<maxy;j++)
    			{
    				if(open[i][j][0]&&(abs(i-dx)+abs(j-dy)+open[open[i][j][1]][open[i][j][2]][3]+1)<lowestopenf)
    				{
    					ex=i; ey=j;
    				}
    			}
    		}
    		if(lowestopenf==maxx+maxy)
    		{	
    			return route;
    		}
    		else if(ex==dx&&ey==dy)
    		{
    			break;
    		}
    		else
    		{
    			open[ex][ey][0]=0;
    			closed[ex][ey]={1,open[ex][ey][1],open[ex][ey][2],open[open[ex][ey][1]][open[ex][ey][2]][3]+1)};
    
    			neighbours={{ex,ey+1},{ex+1,ey},{ex,ey-1},{ex-1,ey}};
    
    			for(int i=0;i<4;i++)
    			{
    				if(neighbours[i][0]<maxx&&neighbours[i][0]>=0
    				&&neighbours[i][1]<maxy&&neighbours[i][1]>=0
    				&&!speedmap[neighbours[i][0]][neighbours[i][1]])
    				{
    					if(closed[neighbours[i][0]][neighbours[i][1]][0]&&
    					closed[ex][ey][3]+1<closed[neighbours[i][0]][neighbours[i][1]][1])
    					{
    						closed[neighbours[i][0]][neighbours[i][1]][3]
    						=closed[ex][ey][3]+1;
    						closed[neighbours[i][0]][neighbours[i][1]][1]=ex;
    						closed[neighbours[i][0]][neighbours[i][1]][2]=ey;
    
    						open[neighbours[i][0]][neighbours[i][1][3]]
    						=open[ex][ey][3]+1;
    						open[neighbours[i][0]][neighbours[i][1]][1]=ex;
    						open[neighbours[i][0]][neighbours[i][1]][2]=ey;
    					}
    					else if(open[neighbours[i][0]][neighbours[i][1]][0]&&
    					open[ex][ey][3]+1<open[neighbours[i][0]][neighbours[i][1]][1])
    					{
    						open[neighbours[i][0]][neighbours[i][1][3]]
    						=open[ex][ey][3]+1;
    						open[neighbours[i][0]][neighbours[i][1]][1]=ex;
    						open[neighbours[i][0]][neighbours[i][1]][2]=ey;
    
    						closed[neighbours[i][0]][neighbours[i][1]][3]
    						=closed[ex][ey][3]+1;
    						closed[neighbours[i][0]][neighbours[i][1]][1]=ex;
    						closed[neighbours[i][0]][neighbours[i][1]][2]=ey;
    					}
    					else
    					{
    						open[neighbours[i][0]][neighbours[i][1]]={1,ex,ey,open[ex][ey][3]+1};
    					}		
    				}
    			}
    		}
    	}
    
    	
    	/* Having found the shortest path we now follow the trail of parent squares starting from
    	the target square until we get to the element who's parent square is the starting square.*/
    
    	int step[] = {dx,dy};
    
    	for(int i=0;i<maxx+maxy;i++)
    	{
    		if(open[step[0]][step[1]][1]==cx&&open[step[0]][step[1]][2]==cy)
    		{
    			nextstep={step[0]+1,step[1]+1};
    			break;
    		}
    		else
    		{
    			step[0]=open[step[0]][step[1]][1]; step[1]=open[step[0]][step[1]][2];
    		}
    	}
    
    	/* My code ends here */
    	
    	return nextstep;	
    }
    Last edited by Subversion; April 12th, 2009 at 10:06 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured