0

Check50 gives me this error:

:( 3x3.txt wins the 3x3 board
\ expected an exit code of 0, not a prompt for input
:( 4x4.txt wins the 4x4 board
\ expected an exit code of 0, not a prompt for input

I'm assuming the problem is with my won function, please check it out and let me know which part you think is the problem

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{

int counter = 1;

for (int i = 0; i < d; i++)
    {
    for (int j = 0; j < d; j++)
        {
        if (counter < d * d - 1)
        counter++;

        if (board[d - 1][d - 1] != 0)
        return false;

        if(board[i][j] > board[i + 1][j] && i + 1 < d)
        return false;
        }
    }

 return true; 
}

This is my move function:

bool move(int tile)
{
// TODO

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (tile == board[i][j]) 
        {
            board[i][j] = tile;
            {
                if (board[i - 1][j] == 0 && i - 1 >= 0)
                {
                    tile = board[i - 1][j];
                    board[i - 1][j] = board[i][j];
                    board[i][j] = tile;
                    return true;
                }
                if (board[i + 1][j] == 0 && i + 1 <= d - 1)
                {
                    tile = board[i + 1][j];
                    board[i + 1][j] = board[i][j];
                    board[i][j] = tile;
                    return true;
                }
                if (board[i][j - 1] == 0 && j - 1 >= 0)
                {
                    tile = board[i][j - 1];
                    board[i][j - 1] = board[i][j];
                    board[i][j] = tile;
                    return true;
                }
                else if (board[i][j + 1] == 0 && j + 1 <= d - 1) 
                {
                    tile = board[i][j + 1];
                    board[i][j + 1] = board[i][j];
                    board[i][j] = tile;
                    return true;
                }
            }

        }
    }
}


return false;
}

Many thanks

1 Answer 1

0

You check whether the numbers in each column are increasing. That's true for the solution, but that being true does not make it a solution.

Note that the number of the current board[i][j] should match (1 + d*i + j) % (d*d) (starting with 1, adding 1 per step right, adding d per step down, with 0 instead of d*d).

bool won(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] != (i*d + j + 1) % (d*d))
            {
                return false;
            }
        }
    }
    return true; 
}

Alternatively, you could solve that with a counter, initially 1, compared to the current tile, and incremented right after the comparison.

bool won(void)
{
    int counter = 1;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] != 0 && board[i][j] != counter)
            {
                return false;
            }
            counter++;
        }
    }
    return true; 
}

The %(d*d) here has the same function as board[i][j] != 0 &&, the first changes value d*d to 0, the blank tile's value, the second ignores the blank tile on checking (assuming it has to be in the right place if all other tiles are).

8
  • Thank you, I have updated my code above, does this make sense now? @Blauelf
    – Sofia
    Commented Feb 3, 2017 at 15:26
  • I still get the illegal move at the end of the game instead of ftw, I'll post my move function, maybe that has flaws aswell, would really appreciate you taking a look at that aswell please
    – Sofia
    Commented Feb 3, 2017 at 15:28
  • You don't use counter. It should be like if (board[i][j]!=counter) {return false;} counter = (counter+1) % (d*d); (without the %(d*d), you'd have to add extra conditions for the blank tile)
    – Blauelf
    Commented Feb 3, 2017 at 17:00
  • I'm sorry I just don't understand this concept: counter = (counter+1) % (d*d). I did use this code now and perhaps I used this code incorrectly, I'm still getting the illegal move statement when the game has finished
    – Sofia
    Commented Feb 3, 2017 at 20:03
  • The %(d*d) is there for wrapping the counter around, once it reaches value d*d, it returns to 0 instead, the value of the blank tile. I've added two examples on how to write the function, both could be written using %(d*d), or using board[i][j]!=0 &&.
    – Blauelf
    Commented Feb 4, 2017 at 18:07

You must log in to answer this question.

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