# Having trouble with board[i][j] = {8,7,6,5,4,3,2,1,0};

First int board[i][j] = { (board_num - j) }; Will not work and I don't know why. Here is an example of the board below.

``````  012
0 876
1 543
2 210
``````

I think it should take 8-0, 8 -1, 8-2, 8-3, 8-4, 8-4, 8-5, 8-6, 8-7 8-8 and then you get the following numbers board[i][j] = {8,7,6,5,4,3,2,1,0}; Can someone help fix the problem? Also will the other parts of the code work? Most of the time I would just use check50 but the problem is the program won't run because of the problem above.

``````      /**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
int board_num = (d * d) - 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{

//set vallue of tiles
board[i][j] =  board_num - j-i*d;
if (d % 2 == 0)
{   //switch  2 and 1
int temp = board[d-3][d-1];
board[d-3][d-1] = board[d-2][d-1];
board[d-2][d-1] = temp;
}
printf ("\n");
}
}

}

/**
* Prints the board in its current state.
*/
void draw(void)
{
int board_num = (d * d) - 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
//set value of tiles
board[i][j] = board_num - j-i*d ;
//replace 0 with _
if (board[i][j] == 0)
{
printf("_");
}
else
{
printf ("%2i", board[i][j]);
}
}
printf ("\n");
}

}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
int board_min = 0;
int board_max = (d*d)-1;
// tile between 12-0
if ((tile <= board_max) && (tile >= board_min))
{
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++ )
{
if(tile == board[i][j])
{
//move up
if (j>0 && board[i][j-1] == 0)
{
//switch
int temp = board[i][j];
board[i][j] =  board[i][j-1];
board[i][j-1] = temp;
return true;
}
//move down
else if (j<d-1 && board[i][j+1] == 0)
{
//switch
int temp = board[i][j];
board[i][j] =  board[i][j+1];
board[i][j+1] = temp;
return true;
}
//move  left
else if (i > 0 && board[i-1][j] == 0)
{
//switch
int temp = board[i][j];
board[i][j] =  board[i-1][j];
board[i-1][j] = temp;
return true;
}
//move  right
else if ( i < d-1 && board[i+1][j] == 0 )
{
//switch
int temp = board[i][j];
board[i][j] =  board[i+1][j];
board[i+1][j] = temp;
return true;
}
else
{
return false;
}
}
}

}

}
return false;

}

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

bool won(void)
{

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

if (board[i][j] == j+i*d)
{

if( board[i][j] == max)
{
return true;

}
}
else
{
return false;
}
}
}
}
``````

The line

``````int board[i][j] = { (board_num - j) };
``````

declares a new 2D array variable `board` with `i` elements for the first index and `j` elements for the second, and tries to initialise it to a list consisting of one number, which is incompatible to a 2D array.

None of that is what you are meant to do.

First, `board` already has been declared globally. Do not re-declare using `int` in front.

Second, `i` and `j` are indices, not dimensions, so not to be used with an array declaration.

Third, even if you meant an array declaration, 2D arrays would be initialised using 2D lists, like `int magic[3][3] = {{2,9,4},{7,5,3},{6,1,8}};`. But you are not meant to do that.

What you really meant was probably something like

``````board[i][j] = board_num - j;
``````

(which is missing a `-i*d` to account for the row)

The `if (d-1 % 2 == 0)` should be after the nested for loops.

Note that the first index is the row, and second index is the column.

`draw` should not assign anything to elements of `board`. You cannot assign a `char*` to an `int`, and you aren't supposed to in that case. Use an if-else with two printf statements, one printing underscore(s) or whatever you consider appropriate for an empty tile, the other printing the actual value of `board[i][j]` as a number.

In `move`, you assume that the maximum tile is 15. The maximum tile number depends is `d*d-1`, not your arbitrarily chosen 15!

You access `board[i][j-1]` without testing whether that field is on the board, potentially accessing memory outside the variable. Use some guard like `if (j>0 && board[i][j-1] == 0)`. If the left side of an `&&` is false, it won't evaluate the right side as that won't change the result. In this case, the `j>0` ensures that `j-1` is still on the board. For `[j+1]`, it would be `j<d-1`.

Use an if-elseif chain, and have only that last else return `false`. Otherwise, you'd always return after checking move left, and never test for the other three.

Your `won` function does not always return anything. But I'm not sure how it is supposed to work.

• I am having trouble connecting the dots on how to make board[i][j] = {8,7,6,5,4,3,2,1,0}; Maybe You stated it in the post but I am not comprehending. You did mention board[i][j] = board_num - j and -i*d but I am not getting it. Can you help? – user20282 Oct 29 '17 at 4:23
• Let me explain the won function. First `if the board[i][j] == board_min` `board[0][0]==0` then `board[1][0]==1` to `board[2][2]==8` assume the numbers are from 8-0. This will be caused by the next line `if (board_min <= board_max)` board_min is 0 to 8 because of `board_min++.` return true when true. else return false. So `if board[0][0] == 1 return false.` @Blauelf can you explain how this is wrong. Thanks for your help. – user20282 Oct 29 '17 at 4:27
• Basically, inside the loop in `init`, use something like `board[i][j] = board_num - j - d * i;` without an `int`. Also note that the swap code meant to go under the loops will perform the remainder operation first, as it has same precedence as multiplication, higher than minus. Parentheses help with that. – Blauelf Oct 29 '17 at 4:48
• I modified some of the code. I assume won will not work, I will attempt to fix that. Also I am getting an error. error: control may reach end of non-void function [-Werror,-Wreturn-type] } This is in reference for the move function and represents the last bracket. Any help is appreciated @Blauelf Thanks – user20282 Oct 31 '17 at 22:49
• In `init`, move the `if (d % 2 == 0)` (no `-1`) below the loops. Also note that first index is row, not column! In `move`, where do you test for the tile to move? And `return false;` after the loop to fix the error on `move`. `won` might be a candidate for complete rewrite. It's far more complex than necessary and I totally not get how it is meant to work, so no idea if it is "fixable". – Blauelf Nov 1 '17 at 0:11