• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Block Sliding Puzzle

 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to modify my block sliding puzzle program so the user can solve it, to do this i need to create three methods, one to return if the problem is solved, one to return if the block can move and one to move the block, this program is completely terminal based and I AM NOT ALLOWED TO USE GUI (stupid i know) below is my code (three differnt classes)







As you can see i have a long way to go but i am completely stuck, i know to see if the problem is solved i have to see if the A block (thats a 2 by 2 block) is next to the exit which has a start and finish variable and a side varible, so i just need to check either the row or the coloum next to the start and finish variables for the block with id A (easier said then done atm). For the move method i am thinking of using a swtich statment for the direction. As for the canMove method i have no idea what to do, do i look for a null id varaible to the direction of what the user wants it to move?

As always guys i apreciate the help in a huge way you guys rock
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i have done a bit more in the program however now i am getting errors such as null pointer exception and array out of bounds index, i believe the problem is within my solved method here it is below:



I can move some blocks around and other ones i cant eg in a problem with just one 2x2 block i can't move it at all :S
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,

I doubt you're going to get much help if you post masses of code like that. If you post a small snippet of code clearly highlighting a problem that you're really struggling with (including the exact error you're getting) I might be inclined to help. Personally I'm not prepared to read your whole assignment.

Jules
 
Peter Shipway
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for the advice i have norrwed my problem to move method the i need to be reading the grid backwords instead of forwards because it reads each block then moves it in the direction but only if it is null eg

....
.AA.
.AA.
....
....

becomes

.AA.
.AA.
....
....
....

when moved up but when moved down it bcomes

....
.AA.
....
.AA.
....

because it sees that there isnt a null value below the first set of A's but there is below the second one



That is the code to move the block up and down the up statment works fine but as i said the down dosent i know i need to go through the array backwards but i dont understand how to do it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't really look at the code, but here's some things to think about...

if you go forward through an array like this:



then you can go backwards through an array like this:



in other words, start at the other end and count down.


now, a null pointer error means that you have a reference to an object, but never CREATED an object. the error message should tell you at the least what line the error happened, which should help you figure out what reference is causing the problem.

In other words, somewhere in your code you have a line like

Foo myObject;

then you try and use a method in myObject, with the dot operator:

myObject.printMe();

note that you may even have something in your code that you THINK will assign an object to this reference, but it might fail and return a null...

myObject = helperObject.getNextWord();

if there is no next word, this method returns a null, so now myObject refers to a null... you see the problem? You need to look at where you're being told the nullPointer exception is.

as to the array out of bounds, again, look at the line where you are being told this happens. how big IS the array? maybe you could print out the real size and what your index is at each step. remember that arrays are 0 indexed, so if the size is 10, you want to run from i = 0 to i < 10
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic