0

I am having problem with my move function in the game of fifteen. I don't know what do. I need someone to point out what I'm doing wrong. Thanks in anticipation.

//To create rows (i) and columns (j) for board. int blank = 0;

for (i = 0; i < d -1; i++)
{
    for (j = 0; j < d -1; j++)
  {

      //To make sure the values of i and j are within allowed values.
      if ((i < 0 || i > d - 1) || (j < 0 || j > d - 1))
      {

    //checks for leftward moves and moves if possible

     if ((board[d-1][d-1] = blank) && (board[i][j] = tile))
     {

     if (board[i][j+1] == blank)
    {
        Swap (&board[i][j-1], &blank);
    }
    if (board[i][j-1] == blank)
    {
        Swap (&board[i][j-1], &blank);
    }
   //checks for rightward moves and moves if possible
    if (board[i+1][j] == blank)
    {
       Swap (&board[i+1][j], &blank);
    }
    if (board[i-1][j] == blank)
    {
       Swap (&board[i-1][j], &blank);
    }
     }
        return true;
    }
  }
}
return false;

}

I defined a swap function appropriately with the prototype placed accordingly in the right place.

0

Outermost condition ((i < 0 || i > d - 1) || (j < 0 || j > d - 1)) is never true in the loops. Looks like you should revise all your conditions.

Try following the code step by step (not necessarily executing it, can be done with pen&paper, or just in mind), and determine what the code should do each time.

You must log in to answer this question.

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