Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am supposed to complete a move() function in fifteen.c, a program with the goal of implementing Game of Fifteen. I just don't see where/how the move() function itself is used in the program.

What I have tried:

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


----

My Task:

 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // TODO
    return false;
}


Thanks a lot,
Posted
Updated 30-Dec-16 6:17am
Comments
Richard MacCutchan 30-Dec-16 4:19am    
It does not really matter where. The exercise is for you to write the function. But since we have no idea what this function is supposed to do we cannot offer any suggestions.
Dave Kreskowiak 30-Dec-16 13:16pm    
You're not writing this code, are you? Yeah, I though not.

1 solution

You better look sharper at your code because it goes:

C++
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);
}

There is a call of the move function. This function should return true, if the move for the int tile is valid. I guess the value should be fetched by some user input.

As I understand your code fragment the move function should also draw on the screen. You better ask the author of the code (and your home work) if something is unclear.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900