#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define NUM_CHOICES 5
#define MAX_WORD_LEN 50
#define MAX_MEANING_LEN 100

struct Word {
    char word[MAX_WORD_LEN];
    char meaning[MAX_MEANING_LEN];
};

void shuffleArray(int *array, int n) {
    if (n > 1) {
        int i;
        for (i = 0; i < n - 1; i++) {
            int j = i + rand() / (RAND_MAX / (n - i) + 1);
            int temp = array[j];
            array[j] = array[i];
            array[i] = temp;
        }
    }
}

void dailyWordTest(struct Word *word, char **wrong_choices) {
    srand(time(NULL));
    int random_indices[NUM_CHOICES];
    char choices[NUM_CHOICES][MAX_MEANING_LEN];
    char user_answer[10]; // 사용자 답변을 문자열로 받음

    // 문제 설정
    printf("\n\n--- choose answer!!---\n'%s'\n", word->word);

    // 정답과 오답 선택지 생성
    random_indices[0] = 0; // 0번 인덱스는 항상 정답으로 설정
    for (int i = 1; i < NUM_CHOICES; i++) {
        random_indices[i] = i; // 오답 선택지는 1부터 NUM_CHOICES-1까지
    }

    // 오답 선택지 섞기
    shuffleArray(random_indices, NUM_CHOICES);

    // 선택지 설정
    for (int i = 0; i < NUM_CHOICES; i++) {
        if (random_indices[i] == 0) {
            strcpy(choices[i], word->meaning); // 정답 설정
        } else {
            // 오답 선택지 설정
            strcpy(choices[i], wrong_choices[random_indices[i] - 1]);
        }
    }

    // 선택지 섞기
    shuffleArray(random_indices, NUM_CHOICES);

    // 선택지 출력
    for (int i = 0; i < NUM_CHOICES; i++) {
        printf("%d. %s\n", i + 1, choices[i]);
    }

    // 사용자 답변 받기
    printf("답을 선택하세요 (1-%d): ", NUM_CHOICES);
    scanf("%s", user_answer);
    int user_choice = atoi(user_answer); // 정수로 변환

    // 정답 확인
    if (user_choice >= 1 && user_choice <= NUM_CHOICES) {
        if (random_indices[user_choice - 1] == 0) {
            printf("정답은 '%s'입니다.\n");
        } else {
            printf("정답은 '%s'입니다.\n", word->meaning);
        }
    } else {
        printf("잘못된 입력입니다. 1부터 %d까지의 숫자 중에서 선택해주세요.\n", NUM_CHOICES);
    }
}

int main() {
    struct Word daily_word;

    // 오늘의 단어와 뜻 설정
    strcpy(daily_word.word, "His speech might ______ any fears about the war ");
    strcpy(daily_word.meaning, "dispel");

    // 오답 선택지 배열
    char *wrong_choices[NUM_CHOICES - 1] = {
        "choose", "compute", "deduce", "charge"
    };

    // 테스트 시작
    dailyWordTest(&daily_word, wrong_choices);

    return 0;
}