0

Can someone tell me what exactly going on with my code and what does this error mean ?

And what does this mean too -> "can't check until a frown turns upside down" ?

    /**
 * fifteen.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * 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
#define BLANK 82

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

// dimensions
int d;

// 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 j, i = 0; i < d; i++)
        {
            for (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 = GetInt();

        // 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)
{
    int count = d*d - 1;
    for (int i = 0; i < d; i++)
    {
        for ( int j = 0; j < d; j++, count--)
        {
            board[i][j] = count;
        }
    }
    if (board[d-1][d-1] == 0)
    {
        board[d-1][d-1] = BLANK;
    }
    if (d % 2 == 0)
    {
        int temp = board[d-1][d-2];
        board[d-1][d-2] = board[d-1][d-3];
        board[d-1][d-3] = temp;
    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    printf("\n\n");
    for (int i = 0; i < d; i ++)
    {
        for ( int j = 0; j < d; j ++)
        {
            if (board[i][j] == BLANK && j != d-1)
            {
                printf("   _");
            }
            else if (board[i][j] == BLANK && j == d-1)
            {
                printf("   _\n");
            }
            else if (j == d - 1)
            {
                printf("  %2d\n", board[i][j]);
            }
            else 
            {
                printf("  %2d", board[i][j]);
            }
        }
        printf("\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    for (int i = 0; i < d; i ++)
    {
        for ( int j = 0; j < d; j ++)
        {
            if (board[i][j] == tile)
            {
                if (board[i + 1][j] == BLANK)
                {
                    int temp = board[i][j];
                    board[i][j] = board[i + 1][j];
                    board[i + 1][j] = temp;
                    return true;
                }
                else if (board[i - 1][j] == BLANK)
                {
                    int temp = board[i][j];
                    board[i][j] = board[i - 1][j];
                    board[i - 1][j] = temp;
                    return true;
                }
                else if (board[i][j + 1] == BLANK)
                {
                    int temp = board[i][j];
                    board[i][j] = board[i][j + 1];
                    board[i][j + 1] = temp;
                    return true;
                }
                else if (board[i][j - 1] == BLANK)
                {
                    int temp = board[i][j];
                    board[i][j] = board[i][j - 1];
                    board[i][j - 1] = temp;
                    return true;
                }
            }
        }
    }

    return false;
}

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

                if (total == d * d)
                {
                    return true;
                }
            }
            else
            {
                return false;
            }

        }
    }
    return false;
}
1

check50 routes the output of fifteen into a file and checks the file size first. If the file size is off, it gives that error. The cause is usually that the output is not right - maybe printing extra spaces or characters, or something else? check50 usually expects very precise output. Something that may look right at first glance may have some very subtle error that isn't obvious.

"can't check until a frown turns upside down" means that the current test is dependent on a previous test. If the previous test failed, the current test cannot be done.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

10
  • Can't find the problem in my code...I decreased the size of the file by removing \n from fprintf in main function to 16 but it wants only 18....Is there anything wrong with my code ? – Priyanshu Kumawat Feb 20 '16 at 14:32
  • Undoubtedly there's something wrong with your code, but it's probably in draw(). – Cliff B Feb 20 '16 at 16:21
  • 'void draw(void) { printf("\n\n"); for (int i = 0; i < d; i ++) { for ( int j = 0; j < d; j ++) { if (board[i][j] == BLANK && j != d-1) { printf(" _"); } else if (board[i][j] == BLANK && j == d-1) { printf(" _\n"); } else if (j == d - 1) { printf(" %2d\n", board[i][j]); } else { printf(" %2d", board[i][j]); } } printf("\n"); } }' – Priyanshu Kumawat Feb 21 '16 at 13:34
  • OK, I went back and looked at how the log file is populated and I was wrong. Since the log file is populated directly from the board array, that tells me that there's something about the array that check50 doesn't like. You should start your program, let it initialize the table, and look at the contents of log.txt. That should give you a clue. – Cliff B Feb 21 '16 at 16:51
  • That's what I've already did...i guess it says to remove the "enter" or "\n" just after board[2][3] place number...this \n is causing the extra byte in my log file...but when i remove it by if statement in while() in the main() since fprint is finally making the log file, it again says me that you can't edit anything in main function... – Priyanshu Kumawat Feb 21 '16 at 18:12

You must log in to answer this question.

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