We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Re: help with my maze please
Page Index Toggle Pages: 1
Re: help with my maze please (Read 890 times)
Re: help with my maze please
Apr 18th, 2010, 9:25pm
 
It's a simple problem, but you won't like what it is.
The problem is that YOUR CODE IS A MESS!  Shocked
At the top of the list of problems is that you have unmatched opening parenthesis. Then there's for loops that should be in setup that aren't. There are strings being compared with ==, not .equals(), and then there are pet peeves of mine, like lack of nice indenting, {'s being on the next line instead of at the end of a line, spaces between if and for and their (....

Anyway, here's the cleaned up, at-least-there-are-no-more-syntax-errors version. Now you can start to address the actual functionality issues. See next post.
Re: help with my maze please
Reply #1 - Apr 18th, 2010, 9:26pm
 
class Grid {
 float x, y, w, h, v;
 Grid(float tempX, float tempY, float tempW, float tempH, int tempValue){
   v = tempValue;
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
 }
 void display(){
   noStroke();
   if(v == 0){
     fill(55);
   }
   if(v == 1){
     fill(255, 255, 0);  
   }
   rectMode(CORNER);
   rect(x, y, w, h);  
 }
} // End of class Grid

class Maze {
 float x, y, w, h;
 color c;
 float s;
 Maze(float tempX, float tempY, float tempW, float tempH, color tempColor, float tempSpeed){
   s = tempSpeed;
   x = tempX;
   y = tempY;
   w = tempW;
   h = tempH;
   c = tempColor;
 }
 void display(){
   fill(c);
   noStroke();
   ellipseMode(CORNER);
   ellipse(x, y, w, h);  
 }
 void move( String direction ){
   if(direction.equals("up")){
     for(int n = 0; n < gridlines.length; n++){
       if((maze.x == gridlines[n].x)&&(maze.y == gridlines[n].y)){
         if(gridlines[n-10].v == 0){
           maze.y = maze.y - s;
           return;
         }
       }
     }
   }
   if(direction.equals("down")){
     for(int n = 0; n < gridlines.length; n++){
       if((maze.x == gridlines[n].x)&&(maze.y == gridlines[n].y)){
         if(gridlines[n + 10].v == 0.0){
           maze.y = maze.y + s;
           return;
         }
       }
     }
   }
   if(direction.equals("right")){
     for(int n = 0; n < gridlines.length; n++){
       if((maze.x == gridlines[n].x)&&(maze.y == gridlines[n].y)){
         if(gridlines[n+1].v == 0.0){
           maze.x = maze.x + s;
           return;
         }
       }
     }
   }
   if(direction.equals("left")){
     for(int n = 0; n < gridlines.length; n++){
       if((maze.x == gridlines[n].x)&&(maze.y == gridlines[n].y)){
         if(gridlines[n-1].v == 0.0){
           maze.x = maze.x - s;
           return;
         }
       }
     }
   }
 } // End of move()
} // End of class Maze

Maze maze;
Grid[] gridlines = new Grid [0] ;

int[][] mymazegrid = {
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,1,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0 },
{ 1,1,1,1,1,1,1,1,1,1 }
};

// Grid size
int gridW = 15;
int gridH = 15;


void setup(){
 size(320,320);
 smooth();
 background(80,100,100);
 for(int n = 0; n <= 9; n++){
   for(int m = 0; m <= 9; m++){
     Grid a = new Grid(n*gridW, m*gridH, gridW, gridH, mymazegrid[n][m] );
     gridlines = (Grid[]) append (gridlines, a);
   }
 }
 maze = new Maze (15,15,15,15, color(100,100,100), 15);
} // End setup()

void draw(){
 background(210);
 for(int n = 0; n < gridlines.length; n++){
   gridlines[n].display();
 }
 maze.display();
} // End draw()

void keyPressed(){
 if( key == CODED ){
   if(keyCode == UP){
     maze.move("up");
   }  
   if(keyCode == DOWN){
     maze.move("down");
   }  
   if(keyCode == LEFT)
   {
     maze.move("left");
   }  
   if(keyCode == RIGHT)
   {
     maze.move("right");
   }
 }
} // End keyPressed()






Re: help with my maze please
Reply #2 - Apr 18th, 2010, 10:20pm
 
TfGuy44 wrote on Apr 18th, 2010, 9:25pm:
{'s being on the next line instead of at the end of a line, spaces between if and for and their (....

Yeah, that's nice rules indeed...  Tongue
I like when braces align, I find this more readable. I use this in C, C++, JavaScript, PHP and some other languages and won't change because Sun created some arbitrary rules well beyond its scope of power...  Grin
And I always hated when people omit space between keywords (if, for, while...) and the opening parenthesis, making that looking like function calls, which they aren't. Idem for omitting space before opening braces when using the K&R style...

Just to say that I don't (or rarely...) lecture on coding style (capitalization, etc.), taste and preferences in matter of code presentation are too varied and have been already discussed to death in various places on the Net.

That said, there is still one important rule: readability (even if this is still subjective). So I fully agree on the importance of proper indenting, having clear variable names, using space wisely (after comma, around operators), etc.
Proper commenting (which at least I see in original code!) is important too.

For the anecdote: the usageof == instead of equals is... here  (probably) OK!
I don't recommend it, because if it too easy to get bad habits and use it when it won't work, hence the multiple threads on why sketches doesn't work when comparing strings taken from a file with those hard-coded.
But here, Java compiler will collapse all the "left" strings it finds into one in the string pool, and the == makes comparison on the references, which are therefore equal. It is even faster than equals() (although marginally if the latter first does a ==, which is likely).

But unless you are familiar with Java internals, just stick to equals() to compare strings! I do...

Side note: when doing successive comparisons (if (x == a) / if (x == b)), I recommend either switch when possible (integers, chars), or using else if for further tests. It won't change a thing in speed (or marginally) but it conveys the idea to stop tests as soon as one is OK: no need to test for "left" if "up" is found.
Re: help with my maze please
Reply #3 - Apr 19th, 2010, 3:14am
 
TfGuy44, please don't count personal coding style as "doing it wrong". I for one prefer

Code:
if(thing)
{
doSomething();
}


Over your style, since the braces line up vertically making it much easier to spot if you've not opened or closed one.  Both have their proponents and detractors, but neither is "wrong"
Re: help with my maze please
Reply #4 - Apr 19th, 2010, 5:52pm
 
Pet peeves are just peeves and not problems.
The original code was doing it like this:

if (condition)
{if (secondCondition)
{statement;
  statement2;
}}

While valid, highly discouraged.
Re: help with my maze please
Reply #5 - Apr 19th, 2010, 10:38pm
 
I agree with that, at least! Smiley

And I wish people refrain from deleting their own message when it is answered...
Page Index Toggle Pages: 1