I'm still at the first move to understand what is going wrong. If my first tile to move is the number work, it does work, so 3 and - swap, but the game show me ftw! ??? and when I move for the first time.... some numbers just become - without swap and some legal move are saying illegal and some illegal are swtiching -. I spend many hours changes things to try to find the source of the problem, please help me to understand when is or are my mistakes ? tks. Here are the results when I run the check50 :
:) fifteen.c exists
:) fifteen.c compiles
:) init initializes 3x3 board correctly
:) init initializes 4x4 board correctly
:) 3x3 board, move blank up twice
:) 3x3 board, move blank left twice
:( 3x3 board, move blank left then right
\ expected "1" character 3 of line 11, not "0":) 3x3 board, move blank up then down
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
\ expected "3" character 3 of line 51, not "0":( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8
\ expected "2" character 1 of line 7, not "0":( 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
\ expected "1" character 5 of line 15, not "0":( 3x3 board, move blank up (tile 3), then try tile 2
\ expected "2" character 1 of line 11, not "0":| 3x3.txt wins the 3x3 board
\ can't check until a frown turns upside down
:| 4x4.txt wins the 4x4 board
\ can't check until a frown turns upside down
And here are my codes :
void init(void)
{
//represent the total numbers of tiles which correspond the variable d entered
int total_tiles = (d * d) - 1;
//set a value for each tile
//iterate for each row and column
for (int i= 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
// set value in descending way, left to right, top to bottom
board[i][j] = total_tiles;
total_tiles --;
}
}
//reverse when d is even, reverse numbers 2 and 1
if (d % 2 == 0)
{
board[d - 1][d - 3] = 1;
board[d - 1][d - 2] = 2;
}
}
/**
* Prints the board in its current state.
*/
void draw(void)
{
// iterate for each row and column
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
//print the value of each tile with a space before each numbers
if (board[i][j] > 0)
{
printf("%2d", board[i][j]);
}
// print blank space with a symbol
if (board[i][j] == 0)
{
printf(" -");
}
}
printf("\n");
}
}
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
//int tile is number choosen to move by the player
{
int i = 0; // row
int j = 0; // column
// find the location of the number choosen by player
for (i = 0; i < d; i++)
{
for (j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
//check if tile's location is a legal move from the blank's tile
if (i - 1 >= 0 && board[i - 1][j] == 0) // if blank is above tile
{
// swap the blank's tile and the tile
int tmp = board[i][j];
board[i][j] = board[i - 1][j];
board[i - 1][j] = tmp;
return true;
}
else if (i + 1 < d && board[i + 1][j] == 0) // if blank is top tile
{
int tmp = board[i][j];
board[i][j] = board[i + 1][j];
board[i + 1][j] = tmp;
return true;
}
else if (j + 1 >= 0 && board[i][j + 1] == 0) // if blank is above left to
{
int tmp = board[i][j];
board[i][j] = board[i][j + 1];
board[i][j + 1] = tmp;
return true;
}
else if (j - 1 < d && board[i][j - 1] == 0) // if blank is right to tile
{
int tmp = board[i][j];
board[i][j] = board[i][j - 1];
board[i][j - 1] = tmp;
return true;
}
return false;
}
}
}
return 0;
}
/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.{
*/
bool won(void)
{
//first number is 1
int count = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
count++;
if(board[i][j] != count++)
{
return false;
}
}
}
return true;
}