0

In Game of Fifteen, I am able to store the positional indeces of the blank tile at init; I know the blank tile is the last one, therefore the position is board[y-1][x-1]. What I am struggling with is updating the y/x values once the game is afoot. Try as I might, I can only seem to retrieve the value of the element stored at any given position, not the indeces themslves.

In init, the following gives me the initial coordinates:

blankX = d-1;
blankY = d-1;

As I say, I can't track these coordinates at each subsequent draw, all my wildly ill-informed attempts at using pointers and so on yield nothing helpful, e.g.

if (board[row][col] == 99) // where 99 represents the blank tile { printf("| %c ", _blank_); blankY = *board[row]; blankX = *board[col]; }

As far as I understand it and from what I can make out in gdb, I am merely assigning the first value in the row and column respectively.

Hopefully this is one of those 'Of course!!!' moments; I did read somewhere that it was easy to over think this one..

1
  • This has been resolved by breaking up the search for the tile to be moved into a series of if/else statements and calculating the moves relative to the tile's current position. Just posting here concentrates the mind it seems. – Andrew Smith Jan 14 '16 at 10:44
1

Perhaps you should think about coming at it from a different direction. The user enters the number that is in the tile to be moved to the empty space. A simple linear search of the board[][] array using a nested pair of for loops will make quick work of finding a specific number. Once you find it, the current indices of the for loops will give you the coordinates.

Once you have those coordinates, all you need to do is check the 4 spaces around it, i.e., +/- 1 from each of those coordinates. If those 4 spaces don't find the blank tile (or 0 ), then it's not a valid move. If it does find it, then all you need do is swap the contents.

The only other thing you need to do is to make sure that the +/- 1 coordinate is valid, i.e., not off the edge of the board.

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

0

The solution that works for the pset has been posted in the comments, but I would love to know how to do what I originally asked..

You must log in to answer this question.

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