0

My Game of Fifteen seems to work when I try to move in the four basic cardinal directions, but when I use check50, the game seems to brake in unexpected places (such as after several moves). For example, one of the requirements on check50 was to move up-up-left-down-down-left-up-up-right-down-down-right.

I manually followed the instructions until I got to the first up of the up-up-right. When I entered 5, I got:

8   4   7
_   1   6
_   2   3

instead of:

8   4   7
_   1   6
5   2   3

Another example is the check50 requirement of move blank up (tile 3), then try tile 2. Using a 3x3 board, I tried to move 3:

8   7   6
5   4   _
2   1   3

then 2:

8   7   6
5   4   _
_   1   3

Why did the 2 become an underscore? From my code, 2 is supposed if it's next to a blank tile. I believe that this issue is caused by the code thinking that 2 is next to a blank tile (ignoring the end of the row), but I added y+1 <= d-1 and such to make sure that my code doesn't go out of bounds.

Here's my code: http://pastebin.com/zsN1h6aa

2
  • Thank you for this post and the answer. First time dealing with 2d array and I had the same problem Now the board works. Mar 27 '17 at 8:28
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – MARS
    Mar 28 '17 at 20:05
1

Right idea, wrong test. Look at the following:

            } else if (board[x][y-1] == 0 && y-1 <= d-1) { // left of tile
                ...
            } else if (board[x-1][y] == 0 && x-1 <= d-1) { // up of tile

What is y-1 when y=0? or x-1 when x=0?

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

3
  • Do you have any tips on implementing this? I've tried to add 0 <= to the front first on all of my if statements, then on my left and up if statements, but there are a few cases where moving up or down creates duplicate underscores. Otherwise, moving left and right seems to work fine.
    – Lyxpudox
    Jul 13 '16 at 20:28
  • You don't apply it to all, just to the two above. Also, you should do the range test first so that the second test is only done if the first passes. ` } else if ( y-1 >= 0 && board[x][y-1] == 0 ) {` Of course, that won't fix the problems with won(), but that's a new question. ;-)
    – Cliff B
    Jul 13 '16 at 20:47
  • Thanks for your help! I'm off to figure out won() now.
    – Lyxpudox
    Jul 13 '16 at 21:17

You must log in to answer this question.

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