0

Check50 keeps giving me these errors, I have checked log files to make sure output is correct, and running it myself gives correct output. I am completely stumped about why I am getting the following errors. Any feedback will be appreciated!

Check50 output is:

:) fifteen.c exists.
:) fifteen.c compiles.
:) 3x3 board: init initializes board correctly
:) 3x3 board: catches moving 8 an illegal move
:) 3x3 board: catches moving 1 as a legal move
:( 3x3 board: move blank up twice
    did not find output "8-7-0|5-4-6|2-1..."
:) 3x3 board: move blank left twice
:) 3x3 board: move blank left then right
:) 3x3 board: move blank up then down
:( 3x3 board: move up-up-left-down-down-left-up-up-right-down-down-right
    did not find output "4-1-7|8-2-6|5-3..."
:( 3x3 board: make sure none of 2, 4, 5, 6, 7, 8 move tile
    did not find output "Illegal move."
:) 3x3 board: move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
:( 3x3 board: make sure game is winnable
    did not find output "1-2-3|4-5-6|7-8..."
:( 4x4 board: make sure game is winnable
    did not find output "1-2-3-4|5-6-7-8..."

Code:

/**
 * fifteen.c
 *
 * Implements 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 [DIM_MIN,DIM_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>

// constants
#define DIM_MIN 3
#define DIM_MAX 9

// board
int board[DIM_MAX][DIM_MAX];

// dimensions
int d;

//tiles
int tiles;

//blank tile location
int blank_row;
int blank_col;

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

int main(int argc, string argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: fifteen d\n");
        return 1;
    }

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

    // open log
    FILE *file = fopen("log.txt", "w");
    if (file == NULL)
    {
        return 3;
    }

    // greet user with instructions
    greet();

    // initialize the board
    init();

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

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

        // log the current state of the board (for testing)
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                fprintf(file, "%i", board[i][j]);
                if (j < d - 1)
                {
                    fprintf(file, "|");
                }
            }
            fprintf(file, "\n");
        }
        fflush(file);

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

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

        // quit if user inputs 0 (for testing)
        if (tile == 0)
        {
            break;
        }

        // log move (for testing)
        fprintf(file, "%i\n", tile);
        fflush(file);

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

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

    // close log
    fclose(file);

    // success
    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("WELCOME TO GAME OF FIFTEEN\n");
    usleep(2000000);
}

/**
 * Initializes the game's board with tiles numbered 1 through d*d - 1
 * (i.e., fills 2D array with values but does not actually print them).
 */
void init(void)
{
    //row is I and column J

    tiles = d*d - 1;
    int k = tiles;
    for (int i = 0; i < d; i++)
    {

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


               board[i][j] = k;
               k--;
               //printf("%i\n", board[i][j]);

       }
    }

    if (tiles%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)
            {
                printf(" __ ");
                blank_row = i;
                blank_col = j;
            }
            else
            {
                printf(" %2i ", board[i][j]);
            }
       }
       printf("\n\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false.
 */
bool move(int tile)
{
    int target_row;
    int target_col;

    //find user input in 2D array
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (tile == board[i][j])
            {
                target_row = i;
                target_col = j;
            }
        }
    }
    // booleans that will check if moves are possible
    bool UP    =    blank_row - 1 == target_row && blank_col == target_col;
    bool DOWN  =    blank_row + 1 == target_row && blank_col == target_col;
    bool RIGHT =    blank_col + 1 == target_col && blank_row == target_row;
    bool LEFT  =    blank_col - 1 == target_col && blank_row == target_row;
    //if any booleans above evaluate to true, tile will move
    if(UP || DOWN || RIGHT || LEFT)
    {
        board[blank_row][blank_col] = board[target_row][target_col];
        board[target_row][target_col] = 0;
        return true;
    }

    else
    {
        return false;
    }

    return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration),
 * else false.
 */
bool won(void)
{
    int check_num = 1;

    if (board[0][0] == 1 && board[d-1][d-1] == 0)
    {
        for (int i = 0; i < d; i++)
        {
            for (int j = 0; j < d; j++)
            {
                if(check_num == board[i][j])
                {
                    check_num++;

                }
            }
        }
        if (check_num == d*d)
        {
            return true;
        }
    }

    else
    {
        return false;
    }


    return false;
}
1

The checker does not use your draw function. As you might add any kind of border or colours to your board representation, it's unreliable for testing, so the checker replaces it with a minimal implementation.

Instead of setting blank_row and blank_col in draw, initialise them to d-1 in init, and update them whenever you swap a tile.

| improve this answer | |

You must log in to answer this question.

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