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 & HelpSyntax Questions › Issue, not really sure what's wrong.
Page Index Toggle Pages: 1
Issue, not really sure what's wrong. (Read 669 times)
Issue, not really sure what's wrong.
Nov 22nd, 2009, 5:22pm
 
well I'm writing towers of hanoi and it's a huge block of code (mainly if statements)

I've got the entire thing to show up and appear, but as soon as I try to control any part of it (using keys 1,2,3 to access the different poles, first key press is the pole you're removing a disc from, second key press is the pole you're putting a disc on) nothing really happens besides the pole (background?) moving up and down a slight amount.


well here's the code:

 Disc disc1=new Disc(200,120,60);
 Disc disc2=new Disc(200,110,70);
 Disc disc3=new Disc(200,100,80);
 Disc disc4=new Disc(200,90,90);
 Disc disc5=new Disc(200,80,100);
 Disc disc6=new Disc(200,70,110);
 Disc disc7=new Disc(200,60,120);
 Disc disc8=new Disc(200,50,130);
 ArrayList discs;
 int level = 1;
 Stack fst;
 Stack snd;
 Stack trd;
 int times=1;
 int choice1 = 0;
 int choice2 = 0;
 int fstcount = 4;
 int sndcount = 0;
 int trdcount = 0;
 int hold = 0;
 
 void setup() {
   size(1000,1000);
   Stack fst=new Stack();
   Stack snd=new Stack();
   Stack trd=new Stack();
   fst.push(disc4);
   fst.push(disc3);
   fst.push(disc2);
   fst.push(disc1);
   discs = new ArrayList();
   discs.add(disc1);
   discs.add(disc2);
   discs.add(disc3);
   discs.add(disc4);
   discs.add(disc5);
   discs.add(disc6);
   discs.add(disc7);
   discs.add(disc8);
   disc1.changePos(200,660);
   disc2.changePos(200,670);
   disc3.changePos(200,680);
   disc4.changePos(200,690);
 }
 
 void draw()
 {
   background(255);
   disc1.display();
   disc2.display();
   disc3.display();
   disc4.display();
   rectMode(CENTER);
   rect(200,450,10,500);
   rect(500,450,10,500);
   rect(800,450,10,500);
   if(keyPressed)
   {
       if(key=='1')
       {
         if(times==1)
         {
         choice1 = 1;
         times = 2;
         }
         else
         {
           choice2 = 1;
           move(choice1,choice2);
         }
       }
       if(key=='2')
      {
       if(times==1)
      {
       choice1 = 2;
       times = 2;
      }
     else
     {
       choice2 = 2;
       move(choice1,choice2);
     }
      }      
      if(key=='3')
      {
        if(times==1)
        {
          choice1= 3;
          times = 2;
        }
        else
        {
          choice2 = 3;
          move(choice1,choice2);
        }
      }
   }
  }
  boolean canMove(Stack a, Stack b)
   {
     float thisone=0;
     float thatone=0;
     if(!a.isEmpty())
     {
        thisone=((Disc)a.peek()).getSize();
        thatone=((Disc)b.peek()).getSize();
        if(thisone>thatone)
        {
          return false;
        }
     }
     return true;
   }
   
   boolean checkWin()
   {
      if((fst.isEmpty()&&snd.isEmpty())||(trd.isEmpty()&&fst.isEmpty()))
      {
        return true;
      }
       return false;
   }
   
   
   void Move(int a, int b)
   {
     if(a==1&&b==2)
     {
       if(canMove(fst,snd))
       {
         hold = discs.indexOf(((Disc)fst.peek()));
         snd.push(fst.pop());
         sndcount++;
         fstcount--;
         ((Disc)discs.get(hold)).changePos(500,950-(sndcount*10));
       }
     }      
     if(a==1&&b==3)
     {
       if(canMove(fst,trd))
       {
         hold = discs.indexOf(((Disc)fst.peek()));
         trd.push(fst.pop());
         trdcount++;
         fstcount--;
         ((Disc)discs.get(hold)).changePos(800,950-(trdcount*10));
       }
     }
     if(a==2&&b==1)
     {
       if(canMove(snd,fst))
       {
         hold = discs.indexOf(((Disc)snd.peek()));
        fst.push(snd.pop());
        fstcount++;
        sndcount--;
        ((Disc)discs.get(hold)).changePos(200,950-(fstcount*10));
       }
     }
     if(a==2&&b==3)
     {
       if(canMove(snd,trd))
       {
         hold = discs.indexOf(((Disc)snd.peek()));
        trd.push(snd.pop());
        trdcount++;
        sndcount--;
        ((Disc)discs.get(hold)).changePos(800,950-(trdcount*10));
       }
     }
     if(a==3&&b==1)
     {
       if(canMove(trd,fst))
       {
         hold = discs.indexOf(((Disc)trd.peek()));
         fst.push(trd.pop());
         fstcount++;
         trdcount--;
         ((Disc)discs.get(hold)).changePos(200,950-(fstcount*10));
       }
     }
     if(a==3&&b==2)
     {
       if(canMove(trd,snd))
       {
         hold = discs.indexOf(((Disc)trd.peek()));
         snd.push(trd.pop());
         sndcount++;
         trdcount--;
         ((Disc)discs.get(hold)).changePos(500,950-(sndcount*10));
       }
     }      
     times = 1;      
   }


EDIT: the disc class is in a separate tab, but I'm pretty sure the error isn't in there...

EDIT2: just noticed that all my calls to the move method were lower case instead of upper case. but now I have a different issue of NullPointerException in the canMove method where it says: if(!a.isEmpty()) (that's after it's compiled and running and I hit 2 keys as entry)
Re: Issue, not really sure what's wrong.
Reply #1 - Nov 22nd, 2009, 5:43pm
 
could you post the Disc Code. Testing is easier if i can try to run it.
Re: Issue, not really sure what's wrong.
Reply #2 - Nov 22nd, 2009, 5:54pm
 
sure:

class Disc {
 float x;
 float y;
 float sze;
 Disc(float xpos, float ypos, float sz)
 {
   x=xpos;
   y=ypos;
   sze=sz;
 }
 void display()
 {
   stroke(0);
   fill(255);
   rectMode(CENTER);
   rect(x,y,sze,10);
 }
 float getSize()
 {
   return sze;
 }
 void changePos(float xpos, float ypos)
 {
   x=xpos;
   y=ypos;
 }
 void hide()
 {
   stroke(255);
   fill(255);
   rectMode(CENTER);
   rect(x,y,sze,10);
   rect(x,y,5,10);
 }
}
Re: Issue, not really sure what's wrong.
Reply #3 - Nov 23rd, 2009, 2:22am
 
Juxe wrote on Nov 22nd, 2009, 5:22pm:
but now I have a different issue of NullPointerException in the canMove method where it says: if(!a.isEmpty()) (that's after it's compiled and running and I hit 2 keys as entry)


When you create your new Stacks (fst,snd,trd) in setup you declare them as 'Stack' again, meaning you're creating new Stack variables which are local to setup and totally separate from the globally declared Stack variables...  So in setup do:

fst=new Stack();

...and not:

Stack fst=new Stack();

It then generates a different bug, but maybe you can fix that yourself...
Page Index Toggle Pages: 1