# CS50 pset3 game fifteen won funcition

0 down vote favorite

I am doing CS50 course now. I am trying to finish program fifteen. I have no idea what is wrong with won part of code.

I guess the problem is with that part of code:

    if (win[d-1][d-1] == d*d)
{
win[d-1][d-1] = 0;
return true;
}


I am trying to create a win matrix with proper configuration of numbers allowing to win the game and compare the win matrix with board matrix. If two arrays are the same, game is finished.

below I attach all code for won function:

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

int win[DIM_MAX][DIM_MAX];

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{

if (win[d-1][d-1] == d*d)
{
win[d-1][d-1] = 0;
return true;
}

if (board[i][j] != win[i][j])
{
return false;
}

win[i][j] = d*i + j + 1;
}
}
return true;


I cannot figure out why function doesn't give value true for won function to quit the game.

Here is what console prints after using check50:

:( 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
https://sandbox.cs50.net/checks/82c365cf5e614352a8a745b6f246aa93

• How did you populate win matrix? And at what point do you do that? Also, instead of having a different win matrix for all values of d, wouldn't you rather have won() just go through your board checking to see if the numbers are in sequence? Oct 24, 2016 at 1:21

• ok, I moved win[i][j] = d*i + j + 1 on front of the loops as you said and it's working now. But as I understand my code correctly, condition board[d-1][d-1] == 0 is checked by if (board[i][j] != win[i][j]). another question is if instead of int win[DIM_MAX][DIM_MAX]; can I use int win[d-1][d-1] or just fill the matrix with zeros using int win[d-1][d-1] = {{0}}? Oct 24, 2016 at 10:11