I dont even know how to begin the move function, can somebody give me some ideas please? Probably by searching the tiles position, but how can i do that?
2 Answers
You have a multidimensional array of size [x][y] (I know that in this case x = y... it's just to make it more clear), so, you need to look for the tile you want to swap simply going through the array:
for (row = 0 to row < x)
{
for (column = 0 to column < y)
{
if board[row][column] is the one I'm looking for...
....
}
}
-
Ok, thank you, those for loops one inside another are exactally yhe same i used in the two functions before right? Just didnt figure out that if there, ill try it Commented Jun 16, 2014 at 16:59
-
Yes, like when you init your board, but in this case it's just to find the one we look for and then check if the blank tile it's up, down, left, or right to our tile and in that case, swap them. Commented Jun 16, 2014 at 18:28
If you keep track of the location of the blank tile, you can check based on its location and the coordinates of the number to be moved if they're swappable, at which point you just have to set the value at the blank tile to the number being moved, and the coordinates of that numbers orginal spot get saved as blank tile. This one is very easy to over think.
-
but, I dont know to to keep track of the blank space, they say using a global variable, but still, I dont know, I already stored the i and j (cordinates) of the tile to make +1 -1 and check if it is up down left or right, but I think i have to make it equals to the blank space, wich I dont have... Commented Jun 17, 2014 at 13:37
-
Like any other programming challenge there are multiple ways to accomplish, such as a 2 element array to hold the x and y coordinates of the blank. Commented Jun 17, 2014 at 16:37
-
I did it, but i still have to search for the blank space every time to store the i in a variable and j in another one... Commented Jun 17, 2014 at 17:09
-
Think about it this way... lets say that the blank square is in position [2][2], and you have that stored so you don't even have to look it up. Your linear search for the number the user is trying to move finds the number in position [3][3]. Can you make the move? How can you tell, without having to draw out the board? Commented Jun 18, 2014 at 6:43