0

I have finished with Pset3 except for a tiny little bug. every move is somehow an Illegal Move. here's my code.

bool move(int tile)
{
// TODO
// find location of specified tile using linear search
int blankrow = d;
int blankcolumn = d;
for (int i =0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        int counter =0;
        if (tile == board[i][j])
        {
            // check if move is legal or illegal
              // so it IS a legal move.
              // now swap it and keep track of da blank


              if ( tile == board[blankrow+1][blankcolumn] || tile == board[blankrow-1][blankcolumn] || tile == board[blankrow][blankcolumn+1] || tile == board[blankrow][blankcolumn -1])
              {
              int exchange;
              exchange = tile;
              board[i][j] = board[blankrow][blankcolumn];
              board[blankrow][blankcolumn] = tile;
              blankrow = i;
              blankcolumn = j;   
              return true;

              }
              else {
                  return false;
              }
        }
        else {
        counter++;
        }
    }
}
return false;
}
1

The initial position of blank tile is board[d-1][d-1], not board[d][d].

Other issues:

  1. When checking tiles around the blank one, you go outside of the board borders, if the blank tile touches the border. For example, for the first move, the blank tile is at the right bottom corner. You check board[blankrow+1][blankcolumn] and board[blankrow][blankcolumn+1] that are outside the board.

  2. You declare int exchange. What for?

UPDATES:

Each time when you call move(tile) you at first put the blank tile in the right bottom corner (reassign blankrow and blankcolumn). You should either keep track of blank tile coordinates outside move function, or use another logic.

| improve this answer | |
  • Thanks!!!! The initial position thing helped! fixed the exchange thing too! I will have to think of a solution for the board border issue... hmmm.... maybe an If statement might help me.... – Ubuntu NutCracker Jul 29 '17 at 9:54
  • Now no illegal move issue is occurring but instead the location isn't changing!!! I will create a new issue now..... – Ubuntu NutCracker Jul 29 '17 at 9:55
  • Answer is updated – obesman Jul 29 '17 at 10:18
  • now I tried doing that (outside move function) but I am getting so many errors. example - – Ubuntu NutCracker Jul 29 '17 at 13:19
  • "initializer element (d) is not compile time constant" – Ubuntu NutCracker Jul 29 '17 at 13:19

You must log in to answer this question.

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