0

so my Pset3 program, fifteen.c, isn't working. This is the error message that shows up from check50:

:( init initializes 3x3 board correctly
   \ expected log.txt to exist
:( init initializes 4x4 board correctly
   \ expected log.txt to exist

I am pretty sure that this has to do with my init() or draw() function. Anyhow, here is my program. Any feedback would be much appreciated:

#define _XOPEN_SOURCE 500

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

// constants
#define MIN 3
#define MAX 9

// board
int board[MAX][MAX];

// dimensions
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();

    // ask for moves until the game has been won
    while(true)
    {
        // clear the screen
        clear();

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

        //saves the states of the board
        save();

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

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

        // if move legal, then accept move. otherwise, reject move.
        if(!move(tile))
        {
            printf("\n Illegal move.\n");
            usleep(500000);
        }

        usleep(500000);
    }

    return 0;
}


void clear(void)
{
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);
}

void greet(void)
{
    clear();
    printf("WELCOME TO GAME OF FIFTEEN\n");
    usleep(2000000);
}

void init(void)
{
    int n = (d * d) - 1;
    for(int i = 0; i < d; i++) {
        for(int j = 0; j < d; j++) {
            board[i][j] = n;
            n--;
        }
    }

    // swap last two digits if board has odd number of tiles
    if(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)
                printf(" %2d |", board[i][j]);
            else
                printf("  _  ");
        }
        printf("\n");
    }
}

bool move(int tile)
{
    int blankRow;
    int blankCol;

    for(int row = 0; row<d; row++)
    {
        for(int col = 0; col <d; col++)
        {
            if(board[row][col] == 0)
            {
                blankRow = row;
                blankCol = col;
            }
        }
    }

    if(tile == board[blankRow][blankCol- 1])
    {
        board[blankRow][blankCol]= tile;
        board[blankRow][blankCol- 1]= 0;
        return true;

    }
    else if(tile == board[blankRow][blankCol+ 1])
    {
        board[blankRow][blankCol]= tile;
        board[blankRow][blankCol+ 1]= 0;
        return true;
    }
    else if(tile == board[blankRow+1][blankCol])
    {
        board[blankRow][blankCol]= tile;
        board[blankRow+ 1][blankCol]= 0;
        return true;
    }
    return false;
}

bool won(void)
{
    for(int row = 0; row<d; row++)
    {
        for(int col = 0; col < d-1; col++)
        {
            if(board[row][col+1]<board[row][col])
            {
                return false;
            }
        }
    }
    return true;
}

void save(void)
{
    const string log = "log.txt";

    static bool saved = false;
    if(!saved)
    {
        unlink(log);
        saved = true;
    }

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

    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,",");
    fclose(p);
}
1

It looks like the code that creates and populates the log.txt file was removed from the source code. That code is absolutely critical for check50 to test your program. What have you removed from the original source code file? You should only be modifying the sections with the //TODO comments. Do not remove any of the other code unless specifically permitted in the instructions.

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

1
  • thank you! i got it now :) – Coder 123 Aug 11 '16 at 22:08

You must log in to answer this question.

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