Skip to main content
added 76 characters in body
Source Link
void init(void)

// define a counter and populate the array
int c = 1;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
    // fill the tiles in descending order
    board[i][j] = d * d - c;
    c++;
    }
}
//make the last tile blank
board[d-1][d-1] = 0;
// if there are an odd number of tiles, switch 1 and 2
if ((d * d)%2 == 0)
{
    board[d-1][d-2] = 2;
    board[d-1][d-3] = 1;
}

void draw(void)

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++) 
        {if (board[i][j] == 0)
        // print the blank tile
            printf(" _");
        else
        // the tiles in descending order
            printf("%2d", board[i][j]);
    }
    printf("\n");
} 

Ignore the brackets part if considering that a mistake. I did it here on purpose so as to upload the code properly. And the errors I get are as follows:

:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 26

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 48

Output in log.txt

{{15,14,13,12},{11,10,9,8},{7,6,5,4},{3,1,2,0}}

void init(void)

// define a counter and populate the array
int c = 1;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
    // fill the tiles in descending order
    board[i][j] = d * d - c;
    c++;
    }
}
//make the last tile blank
board[d-1][d-1] = 0;
// if there are an odd number of tiles, switch 1 and 2
if ((d * d)%2 == 0)
{
    board[d-1][d-2] = 2;
    board[d-1][d-3] = 1;
}

void draw(void)

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++) 
        {if (board[i][j] == 0)
        // print the blank tile
            printf(" _");
        else
        // the tiles in descending order
            printf("%2d", board[i][j]);
    }
    printf("\n");
} 

Ignore the brackets part if considering that a mistake. I did it here on purpose so as to upload the code properly. And the errors I get are as follows:

:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 26

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 48

void init(void)

// define a counter and populate the array
int c = 1;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
    // fill the tiles in descending order
    board[i][j] = d * d - c;
    c++;
    }
}
//make the last tile blank
board[d-1][d-1] = 0;
// if there are an odd number of tiles, switch 1 and 2
if ((d * d)%2 == 0)
{
    board[d-1][d-2] = 2;
    board[d-1][d-3] = 1;
}

void draw(void)

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++) 
        {if (board[i][j] == 0)
        // print the blank tile
            printf(" _");
        else
        // the tiles in descending order
            printf("%2d", board[i][j]);
    }
    printf("\n");
} 

Ignore the brackets part if considering that a mistake. I did it here on purpose so as to upload the code properly. And the errors I get are as follows:

:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 26

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 48

Output in log.txt

{{15,14,13,12},{11,10,9,8},{7,6,5,4},{3,1,2,0}}

added 6633 characters in body
Source Link
/**
  • fifteen.c
  • Computer Science 50
  • Problem Set 3
  • Implements the Game of Fifteen (generalized to d x d).
  • Usage: ./fifteen d
  • whereby the board's dimensions are to be d x d,
  • where d must be in [MIN,MAX]
  • Note that usleep is obsolete, but it offers more granularity than
  • sleep and is simpler to use than nanosleep; man usleep for more. */

#define _XOPEN_SOURCE 500

#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>

// board's minimal dimension #define MIN 3

// board's maximal dimension #define MAX 9

// board, whereby board[i][j] represents row i and column j int board[MAX][MAX];

// board's dimension int d;

// prototypes void clear(void); void greet(void); void init(void); void draw(void); bool move(int tile); bool won(void); void save(void);

int main(int argc, string argv[]) { // greet player greet();

// ensure proper usage
ifvoid init(argc != 2void)
{
 // define a printf("Usage:counter ./fifteenand d\n");
populate the array
int c return= 1;
}

// ensurefor valid(int dimensions
di = atoi(argv[1]);
if0; (di < MIN || d >d; MAXi++)
{
    printffor ("Boardint mustj be= between0; %ij x< %id; andj++)
 %i x %i, inclusive.\n",{
     // fill the MIN,tiles MIN,in MAX,descending MAX);order
    return 2;
}

//board[i][j] initialize= thed board
init();

//* acceptd moves- untilc;
 game is won
while (true)
{c++;
    }
}
// clearmake the screen
last tile blank
board[d-1][d-1] = clear();
0;
// if there are //an drawodd thenumber currentof statetiles, ofswitch the1 boardand 2
if ((d * d)%2 draw(== 0);
{
    // savesboard[d-1][d-2] the= current2;
 state of the boardboard[d-1][d-3] (for= testing)1;
   }

void draw(void)

for save();

int i = 0; //i check< ford; wini++)
{
    iffor (won())
 int j = {
0; j < d; j++)  
   printf("ftw!\n");
     {if (board[i][j] == break;0)
    }

    // prompt forprint move
the blank tile
  printf("Tile to move: ");
    int tile = GetIntprintf(" _");
 
    // move if possible, else report illegality
    if (!move(tile))
    {
// the tiles in descending order
   printf("\nIllegal move.\n");
        usleepprintf(500000"%2d", board[i][j]);
    }

    // sleep for animation's sake
    usleepprintf(500000"\n");
}

// that's all folks
return 0;

}

/**

  • Clears screen using ANSI escape sequences. */ void clear(void) { printf("\033[2J"); printf("\033[%d;%dH", 0, 0); }

/**

  • Greets player. */ void greet(void) { clear(); printf("GAME OF FIFTEEN\n"); usleep(2000000); }

/**

  • Initializes the game's board with tiles numbered 1 through d*d - 1,
  • (i.e., fills board with values but does not actually print them),
  • whereby board[i][j] represents row i and column j. */ void init(void) { // define a counter and populate the array int c = 1; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // fill the tiles in descending order board[i][j] = d * d - c; c++; } } //make the last tile blank board[d-1][d-1] = 0; // if there are an odd number of tiles, switch 1 and 2 if ((d * d)%2 == 0) { board[d-1][d-2] = 2; board[d-1][d-3] = 1; } }

/**

  • Prints the board in its current state. */ void draw(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) {if (board[i][j] == 0) // print the blank tile printf(" _"); else // the tiles in descending order printf("%2d", board[i][j]); } printf("\n"); } }

/**

  • If tile borders empty space, moves tile and returns true, else

  • returns false. */ bool move(int tile) { // establish algorithm for checking bordering tiles for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { if (board[i][j] == tile) { //check if the blank is to the right if (j+1 <= d-1 && board[i][j+1] == 0) { board[i][j+1] = tile; board[i][j] = 0; return true; } // check if the blank is to the left else if (j-1 >= 0 && board[i][j-1] == 0) { board[i][j-1] = tile; board[i][j] = 0; return true; } // check if the blank is above else if (i-1 >= 0 && board[i-1][j] == 0) { board[i-1][j] = tile; board[i][j] = 0; return true; } // check if the blank is below else if (i+1 <= d-1 && board[i+1][j] == 0) { board[i+1][j] = tile; board[i][j] = 0; return true; }

         }
     }
    

    } return false; }

/**

  • Returns true if game is won (i.e., board is in winning configuration),

  • else false. */ bool won(void) { // check if board is sorted int n = 1;

    // check to see if last tile is blank and return false if it is not if (board[d-1][d-1] != 0) return false;

    // set up for loops to check if numbers are in ascending order beginning with 1 for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // check last grid position first, if blank, return true if (i == d - 1 && j == d - 1) return true;

         // check the numbers on the rest of the tiles
         if (n != board[i][j])
             return false;
         n++;                        
     }
    

    } return false; }

/**Ignore the brackets part if considering that a mistake. I did it here on purpose so as to upload the code properly. And the errors I get are as follows:

  • Saves the current state of the board to disk (for testing). */ void save(void) { // log const string log = "log.txt";

    // delete existing log, if any, before first save static bool saved = false; if (!saved) { unlink(log); saved = true; }

    // open log FILE* p = fopen(log, "a"); if (p == NULL) { return; }

    // log board fprintf(p, "{"); for (int i = 0; i < d; i++) { fprintf(p, "{"); for (int j = 0; j < d; j++) { fprintf(p, "%i", board[i][j]); if (j < d - 1) { fprintf(p, ","); } } fprintf(p, "}"); if (i < d - 1) { fprintf(p, ","); } } fprintf(p, "}\n");

    // close log fclose(p); }

**:) fifteen.c exists

\ expected file to be of length 38, not 48**48

/**
  • fifteen.c
  • Computer Science 50
  • Problem Set 3
  • Implements the Game of Fifteen (generalized to d x d).
  • Usage: ./fifteen d
  • whereby the board's dimensions are to be d x d,
  • where d must be in [MIN,MAX]
  • Note that usleep is obsolete, but it offers more granularity than
  • sleep and is simpler to use than nanosleep; man usleep for more. */

#define _XOPEN_SOURCE 500

#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>

// board's minimal dimension #define MIN 3

// board's maximal dimension #define MAX 9

// board, whereby board[i][j] represents row i and column j int board[MAX][MAX];

// board's dimension int d;

// prototypes void clear(void); void greet(void); void init(void); void draw(void); bool move(int tile); bool won(void); void save(void);

int main(int argc, string argv[]) { // greet player greet();

// ensure proper usage
if (argc != 2)
{
    printf("Usage: ./fifteen d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < MIN || d > MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        MIN, MIN, MAX, MAX);
    return 2;
}

// initialize the board
init();

// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // saves the current state of the board (for testing)
    save();

    // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    }

    // prompt for move
    printf("Tile to move: ");
    int tile = GetInt();
 
    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");
        usleep(500000);
    }

    // sleep for animation's sake
    usleep(500000);
}

// that's all folks
return 0;

}

/**

  • Clears screen using ANSI escape sequences. */ void clear(void) { printf("\033[2J"); printf("\033[%d;%dH", 0, 0); }

/**

  • Greets player. */ void greet(void) { clear(); printf("GAME OF FIFTEEN\n"); usleep(2000000); }

/**

  • Initializes the game's board with tiles numbered 1 through d*d - 1,
  • (i.e., fills board with values but does not actually print them),
  • whereby board[i][j] represents row i and column j. */ void init(void) { // define a counter and populate the array int c = 1; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // fill the tiles in descending order board[i][j] = d * d - c; c++; } } //make the last tile blank board[d-1][d-1] = 0; // if there are an odd number of tiles, switch 1 and 2 if ((d * d)%2 == 0) { board[d-1][d-2] = 2; board[d-1][d-3] = 1; } }

/**

  • Prints the board in its current state. */ void draw(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) {if (board[i][j] == 0) // print the blank tile printf(" _"); else // the tiles in descending order printf("%2d", board[i][j]); } printf("\n"); } }

/**

  • If tile borders empty space, moves tile and returns true, else

  • returns false. */ bool move(int tile) { // establish algorithm for checking bordering tiles for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { if (board[i][j] == tile) { //check if the blank is to the right if (j+1 <= d-1 && board[i][j+1] == 0) { board[i][j+1] = tile; board[i][j] = 0; return true; } // check if the blank is to the left else if (j-1 >= 0 && board[i][j-1] == 0) { board[i][j-1] = tile; board[i][j] = 0; return true; } // check if the blank is above else if (i-1 >= 0 && board[i-1][j] == 0) { board[i-1][j] = tile; board[i][j] = 0; return true; } // check if the blank is below else if (i+1 <= d-1 && board[i+1][j] == 0) { board[i+1][j] = tile; board[i][j] = 0; return true; }

         }
     }
    

    } return false; }

/**

  • Returns true if game is won (i.e., board is in winning configuration),

  • else false. */ bool won(void) { // check if board is sorted int n = 1;

    // check to see if last tile is blank and return false if it is not if (board[d-1][d-1] != 0) return false;

    // set up for loops to check if numbers are in ascending order beginning with 1 for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // check last grid position first, if blank, return true if (i == d - 1 && j == d - 1) return true;

         // check the numbers on the rest of the tiles
         if (n != board[i][j])
             return false;
         n++;                        
     }
    

    } return false; }

/**

  • Saves the current state of the board to disk (for testing). */ void save(void) { // log const string log = "log.txt";

    // delete existing log, if any, before first save static bool saved = false; if (!saved) { unlink(log); saved = true; }

    // open log FILE* p = fopen(log, "a"); if (p == NULL) { return; }

    // log board fprintf(p, "{"); for (int i = 0; i < d; i++) { fprintf(p, "{"); for (int j = 0; j < d; j++) { fprintf(p, "%i", board[i][j]); if (j < d - 1) { fprintf(p, ","); } } fprintf(p, "}"); if (i < d - 1) { fprintf(p, ","); } } fprintf(p, "}\n");

    // close log fclose(p); }

**:) fifteen.c exists

\ expected file to be of length 38, not 48**

void init(void)

// define a counter and populate the array
int c = 1;
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
    // fill the tiles in descending order
    board[i][j] = d * d - c;
    c++;
    }
}
//make the last tile blank
board[d-1][d-1] = 0;
// if there are an odd number of tiles, switch 1 and 2
if ((d * d)%2 == 0)
{
    board[d-1][d-2] = 2;
    board[d-1][d-3] = 1;
}

void draw(void)

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)  
        {if (board[i][j] == 0)
        // print the blank tile
            printf(" _");
        else
        // the tiles in descending order
            printf("%2d", board[i][j]);
    }
    printf("\n");
} 

Ignore the brackets part if considering that a mistake. I did it here on purpose so as to upload the code properly. And the errors I get are as follows:

:) fifteen.c exists

\ expected file to be of length 38, not 48

added 6633 characters in body
Source Link
void/**
  • fifteen.c
  • Computer Science 50
  • Problem Set 3
  • Implements the Game of Fifteen (generalized to d x d).
  • Usage: ./fifteen d
  • whereby the board's dimensions are to be d x d,
  • where d must be in [MIN,MAX]
  • Note that usleep is obsolete, but it offers more granularity than
  • sleep and is simpler to use than nanosleep; man usleep for more. */

#define _XOPEN_SOURCE 500

#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>

// board's minimal dimension #define MIN 3

// board's maximal dimension #define MAX 9

// board, whereby board[i][j] represents row i and column j int board[MAX][MAX];

// board's dimension int d;

// prototypes void clear(void); void greet(void); void init(void); void draw(void); bool move(int tile); bool won(void); void save(void);

int main(int argc, string argv[]) { // greet player greet();

// initensure proper usage
if (void)argc != 2)
{
    intprintf("Usage: fulld./fifteen =d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < MIN || d for> MAX)
{
    printf(int"Board imust =be 0;between i%i <x d;%i i++)and %i 
 x %i, inclusive.\n",
      {
  MIN, MIN, MAX, MAX);
    return 2;
}

// initialize forthe board
init(int);

// jaccept =moves 0;until jgame <is d;won
while j++(true)
{
    // clear the screen
    clear();

    // draw the current {state of the board
    draw();

    // saves the current state of the board board[i][j](for =testing)
 d*d- fulld;  save();

    // check for win
    if (won())
    {
     fulld ++;  printf("ftw!\n");
        break;
    } 

    // prompt for move
  }  printf("Tile to move: ");
    board[d-1][d-1]int tile = 0;GetInt();

    // move if possible, else report illegality
    if (d*d!move(tile)%2)
 == 0)  {
    {
    printf("\nIllegal move.\n");
   board[d-1][d-2] = 2;   usleep(500000);
    }

    board[d-1][d-3]// =sleep 1;for animation's sake
    }usleep(500000);
}

// that's all folks
return 0;

}

/**

  • Clears screen using ANSI escape sequences. */ void clear(void) { printf("\033[2J"); printf("\033[%d;%dH", 0, 0); }

/**

  • Greets player. */ void greet(void) { clear(); printf("GAME OF FIFTEEN\n"); usleep(2000000); }

/**

  • Initializes the game's board with tiles numbered 1 through d*d - 1,
  • (i.e., fills board with values but does not actually print them),
  • whereby board[i][j] represents row i and column j. */ void init(void) { // define a counter and populate the array int c = 1; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // fill the tiles in descending order board[i][j] = d * d - c; c++; } } //make the last tile blank board[d-1][d-1] = 0; // if there are an odd number of tiles, switch 1 and 2 if ((d * d)%2 == 0) { board[d-1][d-2] = 2; board[d-1][d-3] = 1; } }

/**

  • Prints the board in its current state. */ void draw(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) {if (board[i][j] == 0) // print the blank tile printf(" _"); else // the tiles in descending order printf("%2d", board[i][j]); } printf("\n"); } }

/**

  • If tile borders empty space, moves tile and returns true, else

  • returns false. */ bool move(int tile) { // establish algorithm for checking bordering tiles for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { if (board[i][j] == tile) { //check if the blank is to the right if (j+1 <= d-1 && board[i][j+1] == 0) { board[i][j+1] = tile; board[i][j] = 0; return true; } // check if the blank is to the left else if (j-1 >= 0 && board[i][j-1] == 0) { board[i][j-1] = tile; board[i][j] = 0; return true; } // check if the blank is above else if (i-1 >= 0 && board[i-1][j] == 0) { board[i-1][j] = tile; board[i][j] = 0; return true; } // check if the blank is below else if (i+1 <= d-1 && board[i+1][j] == 0) { board[i+1][j] = tile; board[i][j] = 0; return true; }

         }
     }
    

    } return false; }

/**

  • Returns true if game is won (i.e., board is in winning configuration),

  • else false. */ bool won(void) { // check if board is sorted int n = 1;

    // check to see if last tile is blank and return false if it is not if (board[d-1][d-1] != 0) return false;

    // set up for loops to check if numbers are in ascending order beginning with 1 for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // check last grid position first, if blank, return true if (i == d - 1 && j == d - 1) return true;

         // check the numbers on the rest of the tiles
         if (n != board[i][j])
             return false;
         n++;                        
     }
    

    } return false; }

/**

  • Saves the current state of the board to disk (for testing). */ void save(void) { // log const string log = "log.txt";

    // delete existing log, if any, before first save static bool saved = false; if (!saved) { unlink(log); saved = true; }

    // open log FILE* p = fopen(log, "a"); if (p == NULL) { return; }

    // log board fprintf(p, "{"); for (int i = 0; i < d; i++) { fprintf(p, "{"); for (int j = 0; j < d; j++) { fprintf(p, "%i", board[i][j]); if (j < d - 1) { fprintf(p, ","); } } fprintf(p, "}"); if (i < d - 1) { fprintf(p, ","); } } fprintf(p, "}\n");

    // close log fclose(p); }

**:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 26

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 48**

void init(void)  
{
    int fulld = 1;  
        for (int i = 0; i < d; i++)  
         {
            for (int j = 0; j < d; j++)  
            {
                board[i][j] = d*d- fulld;  
                fulld ++;  
            }
        }  
    board[d-1][d-1] = 0;  
    if(d*d)%2 == 0)  
    {
        board[d-1][d-2] = 2;  
        board[d-1][d-3] = 1;  
    }
}
/**
  • fifteen.c
  • Computer Science 50
  • Problem Set 3
  • Implements the Game of Fifteen (generalized to d x d).
  • Usage: ./fifteen d
  • whereby the board's dimensions are to be d x d,
  • where d must be in [MIN,MAX]
  • Note that usleep is obsolete, but it offers more granularity than
  • sleep and is simpler to use than nanosleep; man usleep for more. */

#define _XOPEN_SOURCE 500

#include <cs50.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h>

// board's minimal dimension #define MIN 3

// board's maximal dimension #define MAX 9

// board, whereby board[i][j] represents row i and column j int board[MAX][MAX];

// board's dimension int d;

// prototypes void clear(void); void greet(void); void init(void); void draw(void); bool move(int tile); bool won(void); void save(void);

int main(int argc, string argv[]) { // greet player greet();

// ensure proper usage
if (argc != 2)
{
    printf("Usage: ./fifteen d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < MIN || d > MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        MIN, MIN, MAX, MAX);
    return 2;
}

// initialize the board
init();

// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // saves the current state of the board (for testing)
    save();

    // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    } 

    // prompt for move
    printf("Tile to move: ");
    int tile = GetInt();

    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");
        usleep(500000);
    }

    // sleep for animation's sake
    usleep(500000);
}

// that's all folks
return 0;

}

/**

  • Clears screen using ANSI escape sequences. */ void clear(void) { printf("\033[2J"); printf("\033[%d;%dH", 0, 0); }

/**

  • Greets player. */ void greet(void) { clear(); printf("GAME OF FIFTEEN\n"); usleep(2000000); }

/**

  • Initializes the game's board with tiles numbered 1 through d*d - 1,
  • (i.e., fills board with values but does not actually print them),
  • whereby board[i][j] represents row i and column j. */ void init(void) { // define a counter and populate the array int c = 1; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // fill the tiles in descending order board[i][j] = d * d - c; c++; } } //make the last tile blank board[d-1][d-1] = 0; // if there are an odd number of tiles, switch 1 and 2 if ((d * d)%2 == 0) { board[d-1][d-2] = 2; board[d-1][d-3] = 1; } }

/**

  • Prints the board in its current state. */ void draw(void) { for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) {if (board[i][j] == 0) // print the blank tile printf(" _"); else // the tiles in descending order printf("%2d", board[i][j]); } printf("\n"); } }

/**

  • If tile borders empty space, moves tile and returns true, else

  • returns false. */ bool move(int tile) { // establish algorithm for checking bordering tiles for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { if (board[i][j] == tile) { //check if the blank is to the right if (j+1 <= d-1 && board[i][j+1] == 0) { board[i][j+1] = tile; board[i][j] = 0; return true; } // check if the blank is to the left else if (j-1 >= 0 && board[i][j-1] == 0) { board[i][j-1] = tile; board[i][j] = 0; return true; } // check if the blank is above else if (i-1 >= 0 && board[i-1][j] == 0) { board[i-1][j] = tile; board[i][j] = 0; return true; } // check if the blank is below else if (i+1 <= d-1 && board[i+1][j] == 0) { board[i+1][j] = tile; board[i][j] = 0; return true; }

         }
     }
    

    } return false; }

/**

  • Returns true if game is won (i.e., board is in winning configuration),

  • else false. */ bool won(void) { // check if board is sorted int n = 1;

    // check to see if last tile is blank and return false if it is not if (board[d-1][d-1] != 0) return false;

    // set up for loops to check if numbers are in ascending order beginning with 1 for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { // check last grid position first, if blank, return true if (i == d - 1 && j == d - 1) return true;

         // check the numbers on the rest of the tiles
         if (n != board[i][j])
             return false;
         n++;                        
     }
    

    } return false; }

/**

  • Saves the current state of the board to disk (for testing). */ void save(void) { // log const string log = "log.txt";

    // delete existing log, if any, before first save static bool saved = false; if (!saved) { unlink(log); saved = true; }

    // open log FILE* p = fopen(log, "a"); if (p == NULL) { return; }

    // log board fprintf(p, "{"); for (int i = 0; i < d; i++) { fprintf(p, "{"); for (int j = 0; j < d; j++) { fprintf(p, "%i", board[i][j]); if (j < d - 1) { fprintf(p, ","); } } fprintf(p, "}"); if (i < d - 1) { fprintf(p, ","); } } fprintf(p, "}\n");

    // close log fclose(p); }

**:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 26

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 48**

added 72 characters in body
Source Link
MARS
  • 5.2k
  • 3
  • 14
  • 23
Loading
Source Link
Loading