1

My won() function works properly when the board dimensions are 3 x 3. However, it doesn't work properly when the board dimensions are 4 x 4. What could possibly be wrong?

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

I'm not quite sure how that works with any size.

First time through the loop:

say d = 4
i = 0
j = 0

if 0 == 3  //no
else if 0 > 0  //no
else
   if board[0][0] < board[-1][3]  //board[-1][3] ?? is off the grid

Consider the winning position:

01 02 03 04
05 06 07 08
09 10 11 12
13 14 15

board[0][0] is 1  (i is 0, j is 0)
board[0][1] is 2  (i is 0, j is 1)
board[0][2] is 3  (i is 0, j is 2)
board[0][3] is 4  (i is 0, j is 3)
board[1][0] is 5  (i is 1, j is 0)
etc.

Notice how I'm just looping through i and then j? What's happening to the value? Can you think of an easy way to test whether your tiles are in order?

Brenda.

You must log in to answer this question.

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