#include <cmoc.h>
#include <coco.h>

/* US state capitals quiz for Color BASIC's 32-column text screen. */
typedef struct {
    const char *state;
    const char *capital;
} Place;

static const Place places[50] = {
    {"Alabama", "Montgomery"},
    {"Alaska", "Juneau"},
    {"Arizona", "Phoenix"},
    {"Arkansas", "Little Rock"},
    {"California", "Sacramento"},
    {"Colorado", "Denver"},
    {"Connecticut", "Hartford"},
    {"Delaware", "Dover"},
    {"Florida", "Tallahassee"},
    {"Georgia", "Atlanta"},
    {"Hawaii", "Honolulu"},
    {"Idaho", "Boise"},
    {"Illinois", "Springfield"},
    {"Indiana", "Indianapolis"},
    {"Iowa", "Des Moines"},
    {"Kansas", "Topeka"},
    {"Kentucky", "Frankfort"},
    {"Louisiana", "Baton Rouge"},
    {"Maine", "Augusta"},
    {"Maryland", "Annapolis"},
    {"Massachusetts", "Boston"},
    {"Michigan", "Lansing"},
    {"Minnesota", "Saint Paul"},
    {"Mississippi", "Jackson"},
    {"Missouri", "Jefferson City"},
    {"Montana", "Helena"},
    {"Nebraska", "Lincoln"},
    {"Nevada", "Carson City"},
    {"New Hampshire", "Concord"},
    {"New Jersey", "Trenton"},
    {"New Mexico", "Santa Fe"},
    {"New York", "Albany"},
    {"North Carolina", "Raleigh"},
    {"North Dakota", "Bismarck"},
    {"Ohio", "Columbus"},
    {"Oklahoma", "Oklahoma City"},
    {"Oregon", "Salem"},
    {"Pennsylvania", "Harrisburg"},
    {"Rhode Island", "Providence"},
    {"South Carolina", "Columbia"},
    {"South Dakota", "Pierre"},
    {"Tennessee", "Nashville"},
    {"Texas", "Austin"},
    {"Utah", "Salt Lake City"},
    {"Vermont", "Montpelier"},
    {"Virginia", "Richmond"},
    {"Washington", "Olympia"},
    {"West Virginia", "Charleston"},
    {"Wisconsin", "Madison"},
    {"Wyoming", "Cheyenne"}
};

/* Release the previous key so every screen stays visible until a new press. */
static void pause_for_key(void)
{
    while (inkey() != 0)
        ;
    waitkey(1);
    while (inkey() != 0)
        ;
}

static void shuffle(unsigned char order[50])
{
    unsigned char i, j, temp;
    for (i = 0; i < 50; ++i)
        order[i] = i;
    for (i = 49; i > 0; --i)
    {
        j = (unsigned char) (rand() % (i + 1));
        temp = order[i];
        order[i] = order[j];
        order[j] = temp;
    }
}

static unsigned char answer_key(void)
{
    unsigned char key;
    for (;;)
    {
        key = waitkey(1);
        if (key >= '1' && key <= '3')
            return (unsigned char) (key - '1');
        locate(0, 10);
        printf("USE KEYS 1, 2 OR 3");
        while (inkey() != 0)
            ;
    }
}

static unsigned char ask(unsigned char question,
                         unsigned char state_index,
                         unsigned char correct_slot)
{
    unsigned char first, second, choices[3], i, wrong;
    unsigned char selected;

    do {
        first = (unsigned char) (rand() % 50);
    } while (first == state_index);
    do {
        second = (unsigned char) (rand() % 50);
    } while (second == state_index || second == first);

    wrong = 0;
    for (i = 0; i < 3; ++i)
    {
        if (i == correct_slot)
            choices[i] = state_index;
        else
            choices[i] = (wrong++ == 0) ? first : second;
    }

    cls(255);
    printf("STATE CAPITALS  %u/5\n\n", (unsigned) (question + 1));
    printf("WHAT IS THE CAPITAL OF\n");
    printf("%s?\n\n", places[state_index].state);
    for (i = 0; i < 3; ++i)
        printf("%u. %s\n", (unsigned) (i + 1),
               places[choices[i]].capital);
    printf("\nCHOOSE 1, 2 OR 3");
    selected = answer_key();

    cls(255);
    printf("%s\n\n", places[state_index].state);
    if (selected == correct_slot)
        printf("CORRECT!\n\n");
    else
        printf("NOT QUITE.\n\n");
    printf("CAPITAL: %s\n\n", places[state_index].capital);
    printf("PRESS ANY KEY TO CONTINUE");
    pause_for_key();
    return selected == correct_slot;
}

int main(void)
{
    unsigned char order[50];
    unsigned char question, score, offset;

    width(32);
    cls(255);
    printf("STATE CAPITALS\n\n");
    printf("FIVE QUESTIONS PER ROUND\n");
    printf("PICK THE RIGHT CAPITAL.\n\n");
    printf("PRESS ANY KEY TO START");
    pause_for_key();
    srand(getTimer());

    for (;;)
    {
        shuffle(order);
        score = 0;
        offset = (unsigned char) (rand() % 3);
        for (question = 0; question < 5; ++question)
            score += ask(question, order[question],
                         (unsigned char) ((question + offset) % 3));

        cls(255);
        printf("ROUND COMPLETE!\n\n");
        printf("FINAL SCORE: %u/5\n\n", (unsigned) score);
        if (score == 5)
            printf("PERFECT ROUND!\n\n");
        else if (score >= 3)
            printf("NICE WORK!\n\n");
        else
            printf("KEEP PRACTICING!\n\n");
        printf("PRESS ANY KEY TO PLAY AGAIN");
        pause_for_key();
    }
    return 0;
}
