0

I've been working on pset3 game of fifteen in cs50 and when working on the draw function, the first output is exactly how it should be. However, if I restart the game by entering zero, then compiling again with ./fifteen #, that's where things get weird. The correct output is there, but on top of that, there are the top one or two lines from the previous run of the game. For example,

./fifteen 5

24 23 22 21 20

19 18 17 16 15

14 13 12 11 10

9  8  7  6  5

4  3  2  1 __

Tile to move: 0

the zero ends the game.

Then, ./fifteen 3

24 23 22 21 20

19 18 17 16 15

24 23 22 21 20

19 18 17 16 15

 8  7  6

 5  4  3

 1  2 __

this is the output. It seems pretty random to me and it's really confusing. It runs fine except for this extra output. Is this because I haven't finished the game yet, or because there's something wrong with my draw code? Here is my draw code:

void draw(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] > 9)
            {
                printf("%d ", board[i][j]);
            }
            else if (board[i][j] > 0)
            {
                 printf("%2d ", board[i][j]);
            }
            else
            {
                printf("__");
            }
        }
        printf("\n");
    }
}

Thanks!

1

It really looks like screen artifacts. It could have to do with screen size. Did you by any chance have (optional) fun with "ANSI escape sequences"? FWIW the original clear function is:

 printf("\033[2J");
 printf("\033[%d;%dH", 0, 0);

I found I could add a printf (eg: printf("THE BOARD\n");) at the beginning of the draw function and it did not affect the check50 result. Maybe something like that would give a visual clue. Alternatively, try a clear at the command line before you run ./fifteen d.

If d is 3, there doesn't appear to be any way this draw function would "print" more than 9 tokens. And it shouldn't have anything to do with whether the other functions are implemented.

1
  • Hey thanks! I think clearing at the command line seems to do the trick. I still feel that there shouldn't be any extra output with the code to begin with, but I don't know haha. If I figure out why I'll be sure to update. It's kinda like a project of it's own figuring that out.
    – DryHeeves
    Nov 23 '16 at 0:03

You must log in to answer this question.

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