Hi please see code on this link: http://pastebin.com/wJy0PWZ7

Every move is illegal and I don't know why...



There are about 3 major issues in the code. First, the closing curly brace of the outer for loop is in the wrong place. It follows the actual swap code, so the earlier code that locates the blank and tile entries never executes more than the top row. Because of the placement of return statements, it never gets to the second row.

Second, in the code block that's commented as // determine if tile move is out of bounds, there is a return true; statement. Because this code block must execute a return, either true or false, no further code will ever execute.

Finally, when it actually performs the 'swap', it never sets the location of the tile is never reset to the blank. Having said that, the code here can be simplified. Since you know the values to be placed in board[][] for both tile and blank, you could simply assign board[blankrow][blankcol]=tile and board[tilerow][tilecol]=0.

This should get you going. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • The "swap for 0" tip is exactly what I was alluding to here when I said: "Also, the fact that the blank tile is always 0 should allow you to swap the two values in an unusually efficient manner!" More generally, swapping an arbitrary or user-defined value for any known constant does not require a temp variable, so it's more efficient. :) Nov 7 '16 at 21:22
  • Thank you so much for your help, now solved :) Nov 8 '16 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .