0

I hope all is well.

I am blocked on the game of Fifteen section of Pset 3.

I am taking this one function at a time and I am on the move function.

I am able to find the tile that the user would like to move. However, at that point I am basically blocked on how to find the location of the desired tile in relation to the blank tile.

I know that I could potentially use an if statement with multiple conditions. With that said I am stuck because I don't know how to access that element(or in my case the element of the board array containing the int 0 contains).

The other option that I have is using a pointer, but a pointer won't do because it will only work with one element (not the whole 2D array).

Would someone be able to give me some pointers in the right direction please (without giving the game away)?

I have put my code below this message

Please reply as soon as possible.

Kind regards,

Adi Khajuria A CS50 student from London, UK

bool move(int tile)
{


for(int i = 0; i < d; i++)
{
    for(int j = 0; j < d; j++)
    {
        if(tile == board[i][j])
        {
            printf("tile: %i was found. Moving it...", tile);
        }
    }
}

return false;
}
0

One thing you can do is check the adjacent tiles of tile, namely the one below, above, as well as those to the left and right against the X-Y coordinates of the blank space which you should keep track of every time you call move().

You don't have to use multiple if-elseif statements, though, keeping everything clustered inside one if statement is enough, just remember to break everything up into multiple lines so that you don't have one line that is 120+ characters.

Feel free to comment if there's anything you would like me to expand on.

2
  • Update: After plugging away at the issue for about a week and going to office hours, I finally figured it out. Without giving the answer to this one away I just figured out the location of the 0 in relation to the desired tile using I and J for a few if statements. I was also able to figure out the won function as well. Thanks for your help anyway. – AdiKhajuria Sep 24 '16 at 17:56
  • Nice work! And good luck on pset4, you'll need it! – kluvin Sep 24 '16 at 18:11

You must log in to answer this question.

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