|
-
April 12th, 2009, 10:03 AM
#1
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.
-
April 12th, 2009, 03:44 PM
#2
Re: Check code please?
 Originally Posted by Subversion
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.
When you say 'a Visual C++ type compiler', do you mean a Visual C++ type IDE (Integrated Development Environment) ? If so, you couldn't have looked very hard, because there are several good free Java IDEs out there, including Eclipse and Sun's own Netbeans (Google is your friend).
As with C++, the compiler is independent of any IDE - if you download the JDK (Java Development Kit), you get the compiler and all the tools, libraries, and instructions. You should know how to run the Java compiler and runtime - it's not difficult, and there's plenty of documentation and tutorials available. See the Java Tutorials.
If you want help with your code, explain what it is supposed to do, what the problem is (what is and isn't happening) or where you are stuck.
Could you guys check this function for me and tell me if I've done anything wrong or very innefficient please?
If you've done anything wrong, the code either won't compile, or won't run, or won't do what you want it to do. That's up to you to find out. If you get a compiler or runtime error you don't understand, post up the full error message text and explain what happened.
As far as I can see, you haven't done anything inefficient in Java coding terms, but as I don't know what the algorithm is, for all I know it may be very inefficient - that's not really a Java problem.
It is easier to measure something than to understand what you have measured...
Anonymous
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 13th, 2009, 12:04 AM
#3
Re: Check code please?
You can just download and set up JDK and compile with the command prompt.
-
April 20th, 2009, 09:53 AM
#4
Re: Check code please?
Thank you very much for the replies guys, especially dlorde for his detailed post.
I wanted to know if I had made any obvious semantic or technical mistakes in my code, and from your reply I take it that no.
I will certainly check out those compilers that were mentioned.
Thanks again.
-
April 20th, 2009, 10:39 AM
#5
Re: Check code please?
 Originally Posted by Subversion
I wanted to know if I had made any obvious semantic or technical mistakes in my code, and from your reply I take it that no.
You didn't read my post very carefully - I didn't say you hadn't made any mistakes, simply that I couldn't see anything I could easily spot as inefficient - with caveats.
What I did say was: "If you've done anything wrong, the code either won't compile, or won't run, or won't do what you want it to do. That's up to you to find out".
I think if you compile it you'll find errors. Fixing errors is a large part of software development.
You still need to distinguish between compilers and IDEs.
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
T. Cargill
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|