1

Please, I spent so many hours on this and I can't find the mistake I am doing. From the start, there are some numbers still move when they are illegal move.

See description from check50:

:( 3x3 board, move blank left then right
   \ expected "1" character 3 of line 11, not "0":) 3x3 board, move blank up then down
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
   \ expected "3" character 3 of line 51, not "0":( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8
   \ expected "2" character 1 of line 7, not "0":( 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
   \ expected "1" character 5 of line 15, not "0":( 3x3 board, move blank up (tile 3), then try tile 2

here is my move code:

bool move(int tile)
//int tile is number choosen to move by the player 
{
    int i = 0;  // row  
    int j = 0;  // column 


 // find the location of the number choosen by player 
for (i = 0; i < d; i++) 
{
    for (j = 0; j < d; j++)
    {
        if (board[i][j] == tile)
        { 

             //check if tile's location is a legal move from the blank's tile
            if (i - 1 >= 0 && board[i - 1][j] == 0) // if blank is above tile 
            {
                 // swap the blank's tile and the tile 
                int tmp = board[i][j]; 
                board[i][j] = board[i - 1][j];
                board[i - 1][j] = tmp;
                return true;
            }

            else if (i + 1 < d && board[i + 1][j] ==  0) // if blank is top tile 
            {
                int tmp = board[i][j];
                board[i][j] = board[i + 1][j];
                board[i + 1][j] = tmp;
                return true;
            }

            else if (j + 1 >= 0 && board[i][j + 1] == 0) // if blank is above left to
            {
                int tmp = board[i][j];
                board[i][j] = board[i][j + 1];
                board[i][j + 1] = tmp;
                return true;
            }

            else if (j - 1 < d && board[i][j - 1] == 0) // if blank is right to tile 
            {
                int tmp = board[i][j];
                board[i][j] = board[i][j - 1];
                board[i][j - 1] = tmp; 
                return true;
            }
            return false;
        }
    }
}
return 0;
}
3
  • @dumbitdownjr : tks for the comment, but I not allowed to accept your nice editing. Someone told me to use the ' ' to do it. Please tell me how to present it nicely as you suggested to be easy to read ?
    – Genevie
    Jul 15, 2017 at 20:21
  • To space out text you can press enter (usually twice) so that it is a bit easier to read. Sometimes if you copy and past code, the beginning and the end of it will be formatted as plain text, so to let the computer know that you want it to be formatted as code, type four spaces in front of any line that you want to be formatted that way (tab doesn't work since it just moves you on to the next box). That's pretty much all I did, and maybe there are better ways to format code, but I had this same problem when I asked my first questions, and I found that most of it is ok at the start anyway. Jul 15, 2017 at 20:31
  • Genevieve, it looks like you've found a solution. If so, would you please post an answer and accept it? Otherwise, it will remain in the unanswered question pool forever. thanks.
    – Cliff B
    Jul 24, 2017 at 21:32

1 Answer 1

0

The line board[i][j] = board[i - 1][j]; in your swap code is unnecessary, because you know that this tile is zero. The code would look cleaner if you just write board[i - 1][j] = board[i][j]; board[i][j] = 0; return true;

You must log in to answer this question.

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