0

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;
}

1 Answer 1

1

Thanks to @Cliff B for editing. It was impossible to read before.

To @Genevie. Move().

if (board[i][j] == 0)   
{
    blank = board[i][j];
}

What it is for? blank is already equals 0. This part of the code makes no harm, however. It is just useless.

Then next two conditions (for up and down tiles) are absolutely correct. And in the following two (for left and right tiles) you should have just mirror the former, swapping i and j.

Won()

Just think what happens when board[d - 1][d -1] != 0, that is not won yet for sure. Nothing. You just pass both loops without any action and return true in the end. Also, note that board[i][j + 1] becomes board[i][d] when j == d - 1, that is outside your board border.

I would advise you to use a bit different method: set count at 1 and then check if each tile == count, doing count ++ after each check.

13
  • tks a lot for help ! blank = board[i][j] I wanted to do as Zamyla said to search the blank at the beginning to become a constant so I don't have to look for 0 at each time - than how can I do it ? Move : I found it ! for left, I correct my mistake the sign + 1. But what do you mean by swapping i and j (i can still move number 8 which become a zero, having two 0) PS: what was the problem from editing, to post correctly a next question ?
    – Genevie
    Commented Jul 14, 2017 at 13:13
  • I correct my mistakes i and j for left and right and also remove the blank term, but all those numbers are changing as 0 insteand of being illegal move on the firt move : 8,6,5,2. What is my mistake ??
    – Genevie
    Commented Jul 15, 2017 at 19:14
  • Update your code in the question pls
    – obesman
    Commented Jul 15, 2017 at 20:04
  • i edit the code and also the new message from check50
    – Genevie
    Commented Jul 15, 2017 at 20:10
  • i found out the mistake for move, but still have a mistake from Won check 50 : ) 3x3 board, move blank up (tile 3), then try tile 2 :( 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
    – Genevie
    Commented Jul 15, 2017 at 20:31

You must log in to answer this question.

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