0

When I run my program it works correctly but when I use the CS50 check I get an error saying that the length of my files are the amount of moves to be made more then it was supposed to be. For example, If there are 2 moves then I get and error that its supposed to be 58 but I have 60. If there is 12 moves then there should be 258 and I have 270. My file is attached underneath.

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


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

// empty space
int emptyz;
int emptyx;

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

    emptyz = d - 1; 
    emptyx = d - 1;

    // 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 = 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 squares = d * d;
    for (int i = 0; i < d; i++)
    {   
        for (int t = 0; t < d  ; t++)
            {
                board[i][t] = squares - 1;
                squares = squares - 1;
                if (board[i][t] == 0)
                 {
                     board[i][t] = 0;
                 }
            }
    }
    int swapNum = d % 2;
    if ( swapNum == 0 )
       {
        int fir = d - 1;
        int one = d - 2;
        int two = d - 3;

        int swap = board[fir][one];
        board[fir][one] = board[fir][two];
        board[fir][two] = swap;
       }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
     for (int i = 0; i < d; i++)
    {   
        for (int t = 0; t < d ; t++)
            {
                if ( board[i][t] < 90 && board[i][t] > 0)
                    printf("%2d " , board[i][t]);
                else 
                    printf("%2c ", 95);
            }    
    printf("\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{ 
    if (tile > 0 && tile < (d * d))
    {int now;
    int noww;
    for (int i = 0; i < d; i++)
    {   
        for (int t = 0; t < d ; t++)
        {            
            if (board[i][t] == tile)
            {
                now = i;
                noww = t;
                break;
            }
        }
    }
    if (board[now][noww + 1] == board[emptyz][emptyx] || board[now + 1][noww] == board[emptyz][emptyx] ||
        board[now - 1][noww] == board[emptyz][emptyx] || board[now][noww - 1] == board[emptyz][emptyx])
         {       
            board[emptyz][emptyx] = tile;
            board[now][noww] = 95;
            emptyz = now;
            emptyx = noww;
            return true;
         }
    }
    return false;
}

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

  int last = 0;
   for (int i = 0; i < d ; i++) 
    {
        for (int t = 0; t < d ; t++)
        {
            if ( board[i][t] < last )
                {
                 return false;
                }
            last = board[i][t];
        }
    }
   return true;
}  
0

check50 has certain dependencies in order for it to work. One of them is that the blank tile in the board[][] array is 0. This is so that it is written as 0 in log.txt. Even though the instructions say you can change things, it also says that the logic of main should remain unchanged. Unfortunately, the instructions aren't clear about leaving the blank tile as 0.

The result of changing the blank tile to 95 is that every move adds 1 to the length of log.txt that check50 expects. The good news is that if you leave it as 0 in your code, it mostly works! There are a couple of problems left, but since you haven't had the opportunity, I'll let you work through them on your own first. ;-)

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

  • thank you very much for your help, got me moved along to my next problem ha – Tyler Gotto Sep 6 '16 at 8:36

You must log in to answer this question.

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