0

below is my modified version of a game of fifteen am still working on, I had just implemented the move() function but it fails to compile for some reason.

#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;
void greet(void);
void init(void);
void draw(void);
bool move(int tile);

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

    // greet user with instructions
    greet();

    // initialize the board
    init();

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

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

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

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


/**
 * Greets player.
 */
void greet(void)
{
    system("clear");
    printf("WELCOME TO GAME OF FIFTEEN\n");
}

/**
 * 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 grid[d][d];
    // Initializes the 2d array board
    for(int i = 0; i < d; i ++)
    {
        for(int j = 0; j < d; j++)
        {
            grid[i][j] = 0;
        }
    }

}


/**
 * Prints the board in its current state.
 */
void draw(void)
{
    // draws the boards dimension
    int grid[d][d];
    int max_d = ((d * d) - 1);
    for(int i = 0; i < d; i++)
    {
        int j = 0;
        while(j < d)
        {
            if(max_d == 0)
            {
                printf("\t _");
                break;
            }
            else
            {
                grid[i][j] = max_d;
                printf("\t%2i  ", grid[i][j]);
                j++;
                max_d--;
            }
        }
        printf("\n\n");
    }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // Moves tile only if it borders with empty an empty space
    int swap = 0;
    int grid[d][d];
    //iterates through 2D array of d * d to check for tile,
    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < d; j++)
        {
            if(tile == grid[i][j])
            {
                //checks if move is legal and does the swap
                if(tile == grid[d - 1][(d - 1) - 1] || tile == grid[(d - 1) - 1][d - 1] || tile == grid[d - 1][(d - 1) + 1] || tile == grid[(d - 1) + 1][d - 1])
                {
                    swap = grid[d - 1][d - 1];
                    grid[d - 1][d - 1] = tile;
                    tile = swap;
                    return true;
                }
            }
            return false;
        }
    }
}

am still yet to implement the won() function, when i compiled the above code, the compiler gave the warning below and i have no idea why.

~/workspace/pset3/fifteen/ $ make five
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    five.c  -lcrypt -lcs50 -lm -o five
five.c:152:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
make: *** [five] Error 1
~/workspace/pset3/fifteen/ $ 

as at now, am code blind and any insight would be appreciated.

1

The error states that if non of your for loops are executed then function is not able to return any value to the function call. Reaching the end of a non void function without returning a value is undefined behavior. In this case, say for example, that the in the first for loop, the initial condition is not satisfied, then the return statement is not executed and hence the error. There must be at least one return statement at the end of the function just before the closing curly bracket.

2
  • if i put the return statement at the end of the function, wouldn't that just overwrite the ifs conditions and just return the last return statement? – Jerey Feb 20 '17 at 3:23
  • @Jerey It shouldn't! C will go over your code, and stop + return at the first return statement it hits. The return statement at the end is there mainly for corner cases, when none of your other ones get called (as a function, excluding void here, has to always return something, no matter what) Hope this helps :)! – HowToCompute Feb 20 '17 at 19:43

You must log in to answer this question.

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