0

My move function is only moving right and also not replacing my tile with the switched tile. The relevant code looks like:

    bool move(int tile)
{
    for (int row=0; row < d; row++ )
    {
        for (int col=0; col<d; col++)
        {
        if(board[row][col]==tile)
        {
            if (board[row-1][col]==0  && row>0) //block above
            {
                int tile2 = board[row-1][col];
                board[row-1][col]=tile;
                board[row][col]= tile2;
            }

            else if (board[row+1][col]==0  && row<d-1) //block below
            {
                int tile2 = board[row+1][col];
                board[row+1][col]=tile;
                board[row][col]= tile2;
            }
             else if (board[row][col-1]==0  && col>0) //block to left
            {
                int tile2 = board[row][col-1];
                board[row][col-1]=tile;
                board[row][col]= tile2;
            }

            else if (board[row][col+1]==0  && col<d-1) //block to right
            {
                int tile2 = board[row+1][col];
                board[row+1][col]=tile;
                board[row][col]= tile2;
            }

If my output starts:

24 23 22 21 20 
19 18 17 16 15 
14 13 12 11 10 
 9  8  7  6  5 
 4  3  2  1 _

I can pick 1 and it returns "illegal move" and the output:

24 23 22 21 20 
19 18 17 16 15 
14 13 12 11 10 
9  8  7  6  5 
4  3  2 __

The 1 disappears. If I press 2, that will disappear and the underscore will move to the left. If I pick a number above, it will just say illegal move and not move it at all. What am I missing?

0

First, sounds like the move function always returns false. That part of the code is not included in the post, but there is no return true evident; and that would explain the "illegal move" behaviour.

Second, looks like a copy/paste accident in the last else if block. It is testing the "block right", but it is moving the "block below". That would explain the "disappearing" tile.

You must log in to answer this question.

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