#include <stdio.h>
#include <string.h>
#include <stdlib.h>             /* for system("clear") */

#define NUM_CATEGORIES 5
#define NUM_QUESTIONS 4
#define STRING_SIZE 26 

void printWelcome(void)
{
    printf("Welcome to C Jeopardy!\n\n");
}

void displayGrid(int grid[][NUM_QUESTIONS], char categories[][STRING_SIZE])
{
    int i, j;
    for (i = 0; i < NUM_CATEGORIES; i++) {
        printf("%12s", categories[i]);
    }
    printf("\n");

    for (i = 0; i < NUM_QUESTIONS; i++) {
         for (j = 0; j < NUM_CATEGORIES; j++) {
             printf("%12d", grid[j][i]);
         }
         printf("\n");
    }
}

void getName(char name[])
{
    printf("Enter your name: ");
    scanf("%5s", name);
}

void getRowCol(int *cat, int *value)
{
    printf("Enter a cateogory (0 - 4): ");
    scanf("%d", cat);
    printf("Enter a value (100, 200, 300, 400): ");
    scanf("%d", value);
    *value = (*value / 100) - 1; 
}

int processQuestion(int row, int col, char questions[][NUM_QUESTIONS][STRING_SIZE], char answers[][NUM_QUESTIONS][STRING_SIZE])
{
    char answer[STRING_SIZE];
    printf("%s\n", questions[row][col]);
    scanf("%25s", answer);
    if (strcmp(answer, answers[row][col]) == 0) {
        printf("\nYou are correct!\n");
        return 1;
    } else {
        printf("\nNope, that's wrong.\n");
        return 0;
    }
}

int questionsRemain(int grid[][NUM_QUESTIONS])
{
    int i, j;
    for(i = 0; i < NUM_CATEGORIES; i++) {
        for(j = 0; j < NUM_QUESTIONS; j++) {
            if (grid[i][j] != 0) {
                return 1;
            }
        }
    }
    return 0;
}

void playJeopardy()
{
    char categories[NUM_CATEGORIES][STRING_SIZE] = 
         { "Data Types", "Selection", "Loops", "Arrays", "Functions" };
    char questions[NUM_CATEGORIES][NUM_QUESTIONS][STRING_SIZE] =
         { {"Holds counting numbers", "Holds real numbers", "Holds letters", "Holds booleans"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"} };
    char answers[NUM_CATEGORIES][NUM_QUESTIONS][STRING_SIZE] =
         { {"int", "double", "char", "int"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"},
             {"1", "2", "3", "4"} };
    int grid[NUM_CATEGORIES][NUM_QUESTIONS] =
         { {100, 200, 300, 400}, 
             {100, 200, 300, 400}, 
             {100, 200, 300, 400}, 
             {100, 200, 300, 400}, 
             {100, 200, 300, 400} };
    char name[STRING_SIZE];
    int row, col;
    int score = 0;

    getName(name);
    while (questionsRemain(grid)) {
        system("clear");
        printf("%s's score: %d\n", name, score);
        displayGrid(grid, categories);
        getRowCol(&row, &col);
        if (processQuestion(row, col, questions, answers)) {
            score = score + grid[row][col];
        } else {
            score = score - grid[row][col];
        }
        grid[row][col] = 0;
    }
    printf("%s's final score: %d\n", name, score);
}

int main(void)
{
    playJeopardy();
    return 0;
}