0

I have been working on the game of fifteen problem and seem to be having an issue. I keep getting an "Illegal Move" when it is a legal move. I think I have found part of the issue and I would just like some guidance on why this is happening.

Here is my code to get the tile location:

//Find tile location
for(int i = 0; i < d; i++)
{
    for(int j = 0; j < d; j++)
    {
        if(board[i][j] == tile)
        {
            x = i;
            y = j;
        }
    }
}

and my board would look something like this

8 7 6
5 4 3
2 1 _

I when the tile = 3 I would expect the x,y to be 2,1 correct? But when I use debug50 to check it out, it gives me 1,2 every time.

However, when I use this to get my blank space:

//Find blank location
for(int i = 0; i < d; i++)
{
    for(int j = 0; j < d; j++)
    {
        if(board[i][j] == blankval)
        {
            blankx = i;
            blanky = j;
        }
    }
}

And it gives me the correct coordinates of 2,2.

Do I have this backwards and can't tell because the blank coordinates would be 2,2 either way?

Any help is appreciated, thank you!

EDIT: Init()

void init()
{
//get max tile value
int value = (d*d) - 1;

//check if board is even
if((d % 2) == 0)
{
    //create board
    //for each row
    for(int i = 0; i < d; i++)
    {
        //for each column
        for(int j = 0; j < d; j++)
        {
            //if value is 2 and board is even, then the 2 and 1 values need swapped
            if(value == 2) 
            {
                board[i][j] = 1;
                value--;
            }
            else if(value == 1)
            {
                board[i][j] = 2;
                value--;
            }
            //else fill board normally
            else
            {
                //set value for tile
                board[i][j] = value;
                value--;
            }
        }
    }
}
else //board is odd
{
    //create board
    //for each row
    for(int i = 0; i < d; i++)
    {
        //for each column
        for(int j = 0; j < d; j++)
        {
            //set value for tile
            board[i][j] = value;
            value--;
        }
    }
}
    board[d - 1][d - 1] = d*d; //set blank value
}

1 Answer 1

1

(new reply as I misread!)

You would expect when tile==3, that x,y is 1,2 :

0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2

Remember, it's row,col so what you are seeing is correct.

(Your init is doing the right thing).

5
  • Thanks for the response, I added the init function.
    – ddrossi93
    Commented Apr 6, 2017 at 2:49
  • I edited my reply as I'd misread your question.
    – curiouskiwi
    Commented Apr 6, 2017 at 3:21
  • Gotcha! Thank you for your help, I'm so used to thinking as up/down as Y and X as left/right and that seems to be what confused me. I'm gonna give my move function another look through
    – ddrossi93
    Commented Apr 6, 2017 at 3:34
  • Could you please accept my answer so this will be marked as answered/closed? Thanks.
    – curiouskiwi
    Commented Apr 6, 2017 at 5:46
  • There you go! @curiouskiwi
    – ddrossi93
    Commented Apr 6, 2017 at 21:34

You must log in to answer this question.

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