Skip to main content
added 164 characters in body
Source Link
obesman
  • 1.1k
  • 7
  • 9

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:) 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
:(void 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

{ //represent the total numbers of tiles which correspond the variable d entered int total_tiles = (d * d) And here are my codes - 1;:

    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;

} /**

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

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

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

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;
       }
   }
} 
deleted 1432 characters in body
Source Link
Genevie
  • 30
  • 1
  • 12

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 :

:) fifteen.c exists
:) fifteen.c compiles
:) init initializes 3x3 board correctly
:)void init initializes 4x4 board correctly
:( 3x3 board, move blank up twice
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left twice
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank left then right
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
   \ expected prompt for input, not exit code of 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 7, not "0":( 3x3 board, move blank up (tile 3void), then try tile 2
   \ expected prompt for input, not exit code of 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{ //represent the total numbers of tiles which correspond the variable d entered int total_tiles = :(d * d) - 1;

void init(void)
{
     //represent the total numbers of tiles which correspond the variable d entered 
    intset total_tilesa =value (dfor *each d)tile - 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 
  int blank = 0; // blank's position
  
  
   reverse when //d findis theeven, locationreverse ofnumbers the2 numberand choosen1 by player 
    forif (i = 0;d i% <2 d;== i++0) 
    {
        for (j = 0; j < d; j++)
        {
             //find blank's tile location 
            if (board[i][j] == 0)   
            {
                blank = board[i][j];
            }

            if (board[i][j] == tile)
            { 
         
                 //check if tile's location is a legal move from the blank's tile
                if (i - 1 >= 0)
                {
                    if (board[i - 1][j] == blank) // 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;
                    }
                }
                    
                if (i + 1 < d)
                {
                    if (board[i + 1][j] == blank) // if blank is top tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i + 1][j];
                        board[i + 1][j] = tmp;
                        return true;
                    }
                }
                            
                if (j + 1 >= 0)
                {
                    if (board[i][j - 1] == blank) // 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;
                    }
                }
                            
                if (jboard[d - 1 < d)
                {
                    if (board[i1][d - 1][j] == 0) // if blank is right to tile 
                    {
                        int tmp3] = board[i][j];1;
                        board[i][j] = board[iboard[d - 1][j];
                        board[i1][d - 1][j]2] = tmp; 
                        return true;
                    }
                }
            }
        }2;
    }
return false;
}


/**
 * Returns true if game is won

} /**

  • 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 (i.e., board is in winningint configuration),i 
 = *0; elsei false.{
< */
boold; won(voidi++)
{
    for (int ij = 0; ij < d; i++j++)
   
   {
       forcount++;
 (int j = 0; j < d -if(board[i][j] 1;!= j++count++) 
       {
          if (board[d - 1][d -1] == 0)
           {
                if (board[i][j + 1] < board[i][j])
                return false;
           }
       }
    } 
return true;    
} 

return true;
}

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
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left twice
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank left then right
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
   \ expected prompt for input, not exit code of 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 7, not "0":( 3x3 board, move blank up (tile 3), then try tile 2
   \ expected prompt for input, not exit code of 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 
  int blank = 0; // blank's position
  
  
     // find the location of the number choosen by player 
    for (i = 0; i < d; i++) 
    {
        for (j = 0; j < d; j++)
        {
             //find blank's tile location 
            if (board[i][j] == 0)   
            {
                blank = board[i][j];
            }

            if (board[i][j] == tile)
            { 
         
                 //check if tile's location is a legal move from the blank's tile
                if (i - 1 >= 0)
                {
                    if (board[i - 1][j] == blank) // 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;
                    }
                }
                    
                if (i + 1 < d)
                {
                    if (board[i + 1][j] == blank) // if blank is top tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i + 1][j];
                        board[i + 1][j] = tmp;
                        return true;
                    }
                }
                            
                if (j + 1 >= 0)
                {
                    if (board[i][j - 1] == blank) // 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;
                    }
                }
                            
                if (j - 1 < d)
                {
                    if (board[i - 1][j] == 0) // if blank is right to tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i - 1][j];
                        board[i - 1][j] = tmp; 
                        return true;
                    }
                }
            }
        }
    }
return false;
}


/**
 * Returns true if game is won (i.e., board is in winning configuration), 
  * else false.{
 */
bool won(void)
{
    for (int i = 0; i < d; i++)
     {
       for (int j = 0; j < d - 1; j++) 
       {
          if (board[d - 1][d -1] == 0)
           {
                if (board[i][j + 1] < board[i][j])
                return false;
           }
       }
    } 
return true;    
}

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

added 688 characters in body; edited tags
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

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 \ expected prompt for input, not exit code of 0 :( 3x3 board, move blank left twice \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank left then right \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down \ expected prompt for input, not exit code of 0 :( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right \ expected prompt for input, not exit code of 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 7, not "0":( 3x3 board, move blank up (tile 3), then try tile 2 \ expected prompt for input, not exit code of 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;

:) //setfifteen.c aexists
:) valuefifteen.c forcompiles
:) eachinit tileinitializes 3x3 
 board correctly
:) //iterateinit forinitializes each4x4 rowboard andcorrectly
:( column3x3 board, move blank up twice
   \ expected prompt for input, not exit code of 0
:(int i=3x3 0;board, imove <blank d;left i++)twice
{ 
   \ expected for"1" character 5 of line 7, not "0":(int j3x3 =board, 0;move jblank <left d;then j++)right
   \ {expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down
   \ expected prompt for input, not //exit setcode valueof in0
:( descending3x3 wayboard, move up-up-left-down-down-left-up-up-right-down-down-right
 to right \ expected prompt for input, topnot toexit bottomcode of 0
:( 3x3 board, from start try to move board[i][j]tile =2, total_tiles;4, 5, 6, 7, 8
   \ expected "2" character 1 total_tilesof --;
line 7, not "0":( }
}

3x3 //reverseboard, whenmove dblank isleft even(tile 1) then up (tile 4), reversethen numberstry 2to andmove tiles 1, 2, 6, 8
if   \ expected "1" character 5 of line 7, not "0":(d %3x3 2board, ==move 0blank up (tile 3), then try tile 2
   \ {expected prompt for input, not exit code of 0
:| 3x3.txt wins the 3x3 board
   board[d\ -can't 1][dcheck -until 3]a =frown 1;turns upside down
:| 4x4.txt wins the 4x4 board
   board[d\ -can't 1][dcheck -until 2]a =frown 2;turns upside down.
    }                    

} /**

  • 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 int blank = 0; // blank's position

    // find the location of the number choosen by player for (i = 0; i < d; i++) { for (j = 0; j < d; j++) { //find blank's tile location if (board[i][j] == 0)
    { blank = board[i][j]; }

         if (board[i][j] == tile)
         { 
    
              //check if tile's location is a legal move from the blank's tile
             if (i - 1 >= 0)
             {
                 if (board[i - 1][j] == blank) // 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;
                 }
             }
    
             if (i + 1 < d)
             {
                 if (board[i + 1][j] == blank) // if blank is top tile 
                 {
                     int tmp = board[i][j];
                     board[i][j] = board[i + 1][j];
                     board[i + 1][j] = tmp;
                     return true;
                 }
             }
    
             if (j + 1 >= 0)
             {
                 if (board[i][j - 1] == blank) // 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;
                 }
             }
    
             if (j - 1 < d)
             {
                 if (board[i - 1][j] == 0) // if blank is right to tile 
                 {
                     int tmp = board[i][j];
                     board[i][j] = board[i - 1][j];
                     board[i - 1][j] = tmp; 
                     return true;
                 }
             }
         }
     }
    

    } return false; }

/** And here are my codes :

  • Returns true if game is won (i.e., board is in winning configuration),
  • else false.{ */ bool won(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d - 1; j++) { if (board[d - 1][d -1] == 0) { if (board[i][j + 1] < board[i][j]) return false; } } } return true;
    }`
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 
  int blank = 0; // blank's position
  
  
     // find the location of the number choosen by player 
    for (i = 0; i < d; i++) 
    {
        for (j = 0; j < d; j++)
        {
             //find blank's tile location 
            if (board[i][j] == 0)   
            {
                blank = board[i][j];
            }

            if (board[i][j] == tile)
            { 
         
                 //check if tile's location is a legal move from the blank's tile
                if (i - 1 >= 0)
                {
                    if (board[i - 1][j] == blank) // 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;
                    }
                }
                    
                if (i + 1 < d)
                {
                    if (board[i + 1][j] == blank) // if blank is top tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i + 1][j];
                        board[i + 1][j] = tmp;
                        return true;
                    }
                }
                            
                if (j + 1 >= 0)
                {
                    if (board[i][j - 1] == blank) // 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;
                    }
                }
                            
                if (j - 1 < d)
                {
                    if (board[i - 1][j] == 0) // if blank is right to tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i - 1][j];
                        board[i - 1][j] = tmp; 
                        return true;
                    }
                }
            }
        }
    }
return false;
}


/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.{
 */
bool won(void)
{
    for (int i = 0; i < d; i++)
    {
       for (int j = 0; j < d - 1; j++) 
       {
          if (board[d - 1][d -1] == 0)
           {
                if (board[i][j + 1] < board[i][j])
                return false;
           }
       }
    } 
return true;    
}

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 \ expected prompt for input, not exit code of 0 :( 3x3 board, move blank left twice \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank left then right \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down \ expected prompt for input, not exit code of 0 :( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right \ expected prompt for input, not exit code of 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 7, not "0":( 3x3 board, move blank up (tile 3), then try tile 2 \ expected prompt for input, not exit code of 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 int blank = 0; // blank's position

    // find the location of the number choosen by player for (i = 0; i < d; i++) { for (j = 0; j < d; j++) { //find blank's tile location if (board[i][j] == 0)
    { blank = board[i][j]; }

         if (board[i][j] == tile)
         { 
    
              //check if tile's location is a legal move from the blank's tile
             if (i - 1 >= 0)
             {
                 if (board[i - 1][j] == blank) // 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;
                 }
             }
    
             if (i + 1 < d)
             {
                 if (board[i + 1][j] == blank) // if blank is top tile 
                 {
                     int tmp = board[i][j];
                     board[i][j] = board[i + 1][j];
                     board[i + 1][j] = tmp;
                     return true;
                 }
             }
    
             if (j + 1 >= 0)
             {
                 if (board[i][j - 1] == blank) // 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;
                 }
             }
    
             if (j - 1 < d)
             {
                 if (board[i - 1][j] == 0) // if blank is right to tile 
                 {
                     int tmp = board[i][j];
                     board[i][j] = board[i - 1][j];
                     board[i - 1][j] = tmp; 
                     return true;
                 }
             }
         }
     }
    

    } return false; }

/**

  • Returns true if game is won (i.e., board is in winning configuration),
  • else false.{ */ bool won(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d - 1; j++) { if (board[d - 1][d -1] == 0) { if (board[i][j + 1] < board[i][j]) return false; } } } return true;
    }`

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
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move blank left twice
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank left then right
   \ expected "1" character 5 of line 7, not "0":( 3x3 board, move blank up then down
   \ expected prompt for input, not exit code of 0
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
   \ expected prompt for input, not exit code of 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 7, not "0":( 3x3 board, move blank up (tile 3), then try tile 2
   \ expected prompt for input, not exit code of 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 
  int blank = 0; // blank's position
  
  
     // find the location of the number choosen by player 
    for (i = 0; i < d; i++) 
    {
        for (j = 0; j < d; j++)
        {
             //find blank's tile location 
            if (board[i][j] == 0)   
            {
                blank = board[i][j];
            }

            if (board[i][j] == tile)
            { 
         
                 //check if tile's location is a legal move from the blank's tile
                if (i - 1 >= 0)
                {
                    if (board[i - 1][j] == blank) // 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;
                    }
                }
                    
                if (i + 1 < d)
                {
                    if (board[i + 1][j] == blank) // if blank is top tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i + 1][j];
                        board[i + 1][j] = tmp;
                        return true;
                    }
                }
                            
                if (j + 1 >= 0)
                {
                    if (board[i][j - 1] == blank) // 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;
                    }
                }
                            
                if (j - 1 < d)
                {
                    if (board[i - 1][j] == 0) // if blank is right to tile 
                    {
                        int tmp = board[i][j];
                        board[i][j] = board[i - 1][j];
                        board[i - 1][j] = tmp; 
                        return true;
                    }
                }
            }
        }
    }
return false;
}


/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.{
 */
bool won(void)
{
    for (int i = 0; i < d; i++)
    {
       for (int j = 0; j < d - 1; j++) 
       {
          if (board[d - 1][d -1] == 0)
           {
                if (board[i][j + 1] < board[i][j])
                return false;
           }
       }
    } 
return true;    
}
Source Link
Genevie
  • 30
  • 1
  • 12
Loading