#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int room;
void doClear()
{
    printf("\033[H\033[J");
}
void doColor(int c)
{
    if (c >= 0 && c <= 8)
        printf("\033[3%dm", c);
}
void undoColor()
{
    printf("\033[m");
}
void quit()
{
    doClear();
    doColor(1);
    printf("You never made it out.\n");
    printf("You died of starvation.\n");
    printf("Thanks for playing!\n");
    undoColor();
    exit(0);
}
void room0()
{
    char user_input[128];
    doColor(4);
    printf("You are in the entry hall.\n");
    printf("The massive doors are securely locked.\n");
    printf("You can go ");
    doColor(1);
    printf("north");
    doColor(4);
    printf(" or ");
    doColor(1);
    printf("south");
    doColor(4);
    printf(".\n");
    while (1)
    {
        undoColor();
        printf("\nYou go: ");
        fgets(user_input, 127, stdin);
        if (strcasecmp(user_input, "north\n") == 0)
        {
            room = 1;
            return;
        }
        else if (strcasecmp(user_input, "south\n") == 0)
        {
            room = 2;
            return;
        }
        else if (strcasecmp(user_input, "quit\n") == 0)
        {
            quit();
        }
        else
        {
            doColor(1);
            printf("Sorry, I didn't understand you. Try again.\n");
        }
    }
}
void room1()
{
    char user_input[128];
    doColor(5);
    printf("You are in the den.\n");
    printf("You see papers scattered everywhere.\n");
    printf("You also see a creaky old door leading to the back yard.\n");
    printf("You can go ");
    doColor(1);
    printf("west");
    doColor(5);
    printf(" or ");
    doColor(1);
    printf("south");
    doColor(5);
    printf(".\n");
    while (1)
    {
        undoColor();
        printf("\nYou go: ");
        fgets(user_input, 127, stdin);
        if (strcasecmp(user_input, "west\n") == 0)
        {
            room = 3;
            return;
        }
        else if (strcasecmp(user_input, "south\n") == 0)
        {
            room = 0;
            return;
        }
        else if (strcasecmp(user_input, "quit\n") == 0)
        {
            quit();
        }
        else
        {
            doColor(1);
            printf("Sorry, I didn't understand you. Try again.\n");
        }
    }
}
void room2()
{
    char user_input[128];
    doColor(2);
    printf("You are in the library.\n");
    printf("Dusty shelves line the walls.\n");
    printf("There are thousands of old books.\n");
    printf("You can go ");
    doColor(1);
    printf("north");
    doColor(2);
    printf(".\n");
    while (1)
    {
        undoColor();
        printf("\nYou go: ");
        fgets(user_input, 127, stdin);
        if (strcasecmp(user_input, "north\n") == 0)
        {
            room = 0;
            return;
        }
        else if (strcasecmp(user_input, "quit\n") == 0)
        {
            quit();
        }
        else
        {
            doColor(1);
            printf("Sorry, I didn't understand you. Try again.\n");
        }
    }
}
int main(int argc, char *argv[])
{
    room = 0;
    doClear();
    doColor(3);
    printf("Welcome!\n");
    printf("You have entered a strange house.\n");
    printf("The door has locked behind you!\n");
    printf("Can you escape alive?!\n");
    printf("Hahahahaha!\n\n");
    while (room != 3)
    {
        if (room == 0)
            room0();
        else if (room == 1)
            room1();
        else if (room == 2)
            room2();
        doClear();
    }
    doColor(3);
    printf("You made it!\n");
    printf("Congratulations.\n");
    printf("Thanks for playing!\n");
    undoColor();
    return 0;
}