0

I haven't finished the won function yet, but I'm using the check50 to make sure I'm on the right track

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

// 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)
{
    int tiles = (d * d) - 1;    // to insert number in columns

    for (int i = 0; i < d; i++)         // for every row
    {
        for (int j = 0; j < d; j++)     // for every column     
        {

           board[i][j] = tiles;         // insert the number
           tiles--;                     // and decrease by 1
        } // end inner loop
    } // end outer loop
    if ((d*d - 1) % 2 != 0)                     // odd number? swap.
    {
        board[d-1][d-2] = 2;
        board[d-1][d-3] = 1;
    } // end if
    board[d-1][d-1] = 0;
} // end func

/**
 * Prints the board in its current state.
 */
void draw(void)
{


    for (int i = 0; i < d; i++)              // for every row
    {
        for (int j = 0; j < d; j++)          // for every column
        {
            if (board[i][j] == 0)           // if reached the last column break before printing
            {
                printf(" _");

            } else 
            {
            printf("%2d",board[i][j]);                   // other wise, draw
            }
        }

        printf("\n\n");
    } 
    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 every row
    {
        for (int j = 0; j < d; j++)         // for every colmun
        {
            if (board[i][j] == tile)        // search for the tile
            {
                if (board[i-1][j] == 0) // move up
                {

                    board[i-1][j] = tile;   // put the number in the blank space
                    board[i][j] = 0;       // put the space in the number position 

                    return true;
                } else if (board[i+1][j] == -1) // move down
                {
                    board[i+1][j] = tile;    // put the number in the blank space
                    board[i][j] = 0;       // put the space in the number position

                    return true;
                } else if (board[i][j-1] == -1) // move left
                {
                    board[i][j-1] = tile;   // put the number in the blank space
                    board[i][j] = 0;       // put the space in the number position

                    return true;
                } else if (board[i][j+1] == -1) // move right
                {
                    board[i][j+1] = tile;   // put the number in the blank space
                    board[i][j] = 0;       // put the space in the number position

                    return true;
                }
            }
        }
    }

    return false; // not legal
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    // TODO
    return false;
}

and I'm getting this result by check50

:) fifteen.c compiles
:) init initializes 3x3 board correctly
:) init initializes 4x4 board correctly
:( 3x3 board, move blank up twice
   \ expected "0" character 5 of line 6, not "3":( 3x3 board, move blank left twice
   \ expected "0" character 3 of line 7, not "1":( 3x3 board, move blank left then right
   \ expected "0" character 3 of line 7, not "1":( 3x3 board, move blank up then down
   \ expected "0" character 5 of line 6, not "3":( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
   \ expected "0" character 5 of line 6, not "3":( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8
   \ expected "6" character 5 of line 17, not "0":( 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
   \ expected "0" character 3 of line 7, not "1":( 3x3 board, move blank up (tile 3), then try tile 2
   \ expected "0" character 5 of line 6, not "3":| 3x3.txt wins the 3x3 board
   \ can't check until a frown turns upside down
:| 4x4.txt wins the 4x4 board
   \ can't check until a frown turns upside down
https://sandbox.cs50.net/checks/df0ebc3c7c574fb4a8c23177ef372339
1

At least two things going on here. First, unless I've misread something, this else if (board[i+1][j] == -1) will never be true (nor any of the else that compare a board location to -1).

All of the iftests in the move run the risk of "testing off the board". For example i - 1 and j - 1 are out of range when i or j = 0, respectively. Likewise, i + 1 and j + 1 are out of range when i/j = d - 1.

check50 gives you some testing scenarios you can use to see how your program functions before you even send it to check50.

You must log in to answer this question.

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