Juxe
YaBB Newbies
Offline
Posts: 3
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)