PSET3 : fifteen: how to identify board edges in move function?

Hi I am working on pset3 : fifteen and trying to implement move function. I'm not quite sure how to identify board edges and deal with this. My swap is also not working as the tile I want to move seems to override the blank "0" tile. Please see code below and I appreciate any suggestions on what to work on / why this isn't working...

``````bool move(int tile)
{
int temp;

for (int row = 0; row < d; row++){
for (int col = 0; col < d; col++){
if (board[row][col] == 0){
if (board[row][col-1] == tile || board[row][col+1] == tile || board[row+1][col]==tile || board[row-1][col]==tile) {
// legal move is permitted
temp = board[row][col];
board[row][col] = tile;
tile = temp;
return true;

}
}
}
}

return false;
}
``````
• My updated code is available to view on the following link: pastebin.com/EGAZqh6i I am getting an 'expected expression' error for this. Thanks Nov 4 '16 at 15:19

2 Answers

1. In the interest of efficiency, you might want to store the position of blank tile so you don't need to search the board for it each time you call move().
2. You need to check for the borders of your board through if statements first to determine if your move is legal before doing anything. In your above code, if col=d-1, for example, and you are trying to evaluate board[row][col+1], you will seg fault since col+1=d, but col only runs from 0 to d-1.
• Thank you I'm wondering how to refer to positions of an array. For example board[row][col] will refer to the actual value. How do I refer to the position? Because it is this position I will need to store, rather than the value of 0? Nov 2 '16 at 19:44
• You want to index into a two-dimensional array using `board[row][col]`, so you need somewhere to store values for `row` and `col`. In my answer above, I suggest declaring `int tilerow` and `int tilecol` for the user-entered tile position, and `int blankrow` and `int blankcol` for the blank tile's position. In other news, although the first half of @ronga's answer is a good tip, the second half is actually not true. See my answer for more details. Nov 2 '16 at 21:01
• Ok, apologies for my imprecise answer. You may not necessarily segfault when accessing board[row][col+1] when col=d-1, but you will be accessing garbage data in some other part of memory that wasn't initialized. Remember you only defined row and col from indices 0 to d-1, so row/col=d is undefined. And as @MattObert mentioned, you can just go ahead and store blank tile's row and column as blankrow and blankcol. Nov 3 '16 at 5:43
• so i need to make sure that board[row][col+1] (for example) is not outside of bounds e.g board[row][d-1] how i would do this would be board[row][col+1] == board[row][d-1] but surely this is only comparing the values and not the actual position? how can i compare the pos and not the value (as this seems irrelevant).... Nov 3 '16 at 10:05
• Assuming you stored blank tile's coordinates as blankrow and blankcol, you can just go ahead and check to see if tilerow/tilecol is next to it. But with your code as it stands, where you search for blank tile, then you do need to check its adjacent squares to make sure that its not on the rightmost or leftmost edge (i.e. blankcol-1>=0 and blankcol+1<d) in the process of searching for it. Likewise for checking if it's on topmost or bottom row. I'd go with the former method as it's easier. Nov 3 '16 at 13:44

If you know the position of the blank tile (`blankrow`, `blankcol`) and the position of the user-entered tile (`tilerow`, `tilecol`) and you are confident that neither one of these tiles is off the board or out of bounds, then you do not need to add any logic to test the board edges.

A move will be valid if the user-entered tile is adjacent to the blank tile, which can be expressed in a single compound Boolean expression. In pseudocode:

``````if ((tilerow equals blankrow) and (tilecol equals blankcol plus or minus one)) OR ((tilecol equals blankcol) and (tilerow equals blankcol plus or minus one))
``````

Also, the fact that the blank tile is always `0` should allow you to swap the two values in an unusually efficient manner!

Hope this helps!

NOTE: @ronga is right -- you do need to check the array bounds with your code as it currently stands. My point is just that there's a better way, and you could scrap the dangerous part of your code and use my method instead.

• Hey thank you for this please see updated code on this link: pastebin.com/EGAZqh6i For some reason I am getting an 'expected expression' error with the if statement. Thanks Nov 4 '16 at 15:13
• Try wrapping both of those compound expressions in another layer of parenthesis, making it a compound compound expression! (In other words, the `||` in the middle is outside of parenthesis, even though the expressions on both sides of it are fine.) Also, once you have this test for a valid swap working, you won't need to check the array bounds any more! Nov 4 '16 at 20:51