← Kitchen Game in C

Kitchen game code walkthrough

Logic from the report's code, re-run in your browser

The game itself is a Windows program built on the EasyX graphics library, and its full source and artwork are not part of this site. Two pieces of its logic are printed in my course report, so they can be re-run here exactly: the pickup test and the leaderboard.

Both tools follow the C code in the report line for line, translated to TypeScript. Positions are in the game's pixels; the chef's box is 40 × 35 and the clock's 55 × 56, as in the code.

Does the chef pick it up?

Drag the chef (or focus it and use the arrow keys) onto the item. The game counts a pickup only if the chef's top-left or bottom-right corner (●) is inside the item's box. Of all positions where the two boxes touch, … are not counted.

Try:

With show zones on, the shaded areas are the places the chef's top-left corner can be: green where the test counts a pickup, orange where the boxes touch but it does not. The speed-up item is drawn 65 × 50, but the game checks it with pickupClock, so its 55 × 56 box (dashed) is what counts.

The test, from the report
bool pickupClock(double xlong, double ylong, double xtime, double ytime) {
    if (xlong >= xtime && xlong <= xtime + 55 && ylong >= ytime && ylong <= ytime + 56 ||
        xlong + 40 >= xtime && xlong + 40 <= xtime + 55 &&
        ylong + 35 >= ytime && ylong + 35 <= ytime + 56) {
        return true;
    }
    return false;
}

The leaderboard, step by step

Players are stored one per line in userData.txt as name password best-score. Opening the leaderboard reads the file into a linked list, sorts it and prints the top ten. Edit the file and replay.

Linked list

The code, from the report (shortened)
while (!feof(userFILE)) {                 /* read: one node per line */
    fscanf(userFILE, "%s %s %d", username, password, &highestScore);
    p = (Leaderboard*)malloc(sizeof(Leaderboard));
    ...append p...
}
p1 = head; p2 = head->next;
p3 = (Leaderboard*)malloc(sizeof(Leaderboard));
while (p1->next != NULL) {               /* sort: exchange sort on the data */
    while (p2->next != NULL) {
        if (p1->user_highestScore < p2->user_highestScore) {
            ...swap p1 and p2 through p3...
            free(p3);
        }
        p2 = p2->next;
    }
    p1 = p1->next;
    if (p1 != NULL) p2 = p1->next;
}
p = head; rank = 0;
while (p->next != NULL && rank < 10) {   /* display */
    ...print p...
    p = p->next;
}