İşte canlı yardımlar, harici soru yükleme ve kademeli puanlama ile Kim Milyoner Olmak İster tarzı bir bilgi yarışması oyununun tam C uygulaması.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> typedef struct { char question[256]; char answer[256]; char option1[256]; char option2[256]; char option3[256]; char option4[256]; } Question; typedef struct { int currentQuestion; int moneyWon; int lifelineUsed[3]; // 0=kullanılmadı, 1=kullanıldı int gameOver; } GameState; Question* loadQuestions(const char* filename, int* numQuestions) { FILE* fp = fopen(filename, "r"); if (!fp) { printf("Soru dosyası açılamadı!\n"); return NULL; } fscanf(fp, "%d\n", numQuestions); Question* questions = malloc(*numQuestions * sizeof(Question)); if (!questions) { printf("Bellek ayırma başarısız!\n"); fclose(fp); return NULL; } for (int i = 0; i < *numQuestions; i++) { fscanf(fp, "%[^\n]\n", questions[i].question); fscanf(fp, "%[^\n]\n", questions[i].answer); fscanf(fp, "%[^\n]\n", questions[i].option1); fscanf(fp, "%[^\n]\n", questions[i].option2); fscanf(fp, "%[^\n]\n", questions[i].option3); fscanf(fp, "%[^\n]\n", questions[i].option4); } fclose(fp); return questions; } void displayQuestion(Question q) { printf("\n%s\n\n", q.question); printf("1. %s\n", q.option1); printf("2. %s\n", q.option2); printf("3. %s\n", q.option3); printf("4. %s\n", q.option4); } int getAnswer(GameState* game) { int answer; printf("\nCevabınızı girin (1-4): "); scanf("%d", &answer); while (answer < 1 || answer > 4) { printf("Geçersiz! 1-4 arası girin: "); scanf("%d", &answer); } return answer; } int checkAnswer(Question q, int answer) { char* opts[4] = {q.option1, q.option2, q.option3, q.option4}; return strcmp(opts[answer-1], q.answer) == 0; } void useLifeline(GameState* game) { int choice; printf("\nYardım seçin:\n1. 50/50\n2. İzleyiciye Sor\n3. Arkadaşa Telefon Et\nSeçim: "); scanf("%d", &choice); while (choice < 1 || choice > 3) { printf("Geçersiz! 1-3: "); scanf("%d", &choice); } if (game->lifelineUsed[choice-1]) { printf("Zaten kullanıldı!\n"); return; } game->lifelineUsed[choice-1] = 1; printf("Yardım aktif! (Uygulanması gerekiyor)\n"); } void playGame(Question* questions, int numQuestions) { GameState game = {0, 0, {0,0,0}, 0}; while (!game.gameOver) { displayQuestion(questions[game.currentQuestion]); printf("\nYardım kullan? (e/h): "); char c; scanf(" %c", &c); if (c == 'e') useLifeline(&game); int ans = getAnswer(&game); if (checkAnswer(questions[game.currentQuestion], ans)) { printf("Doğru!\n"); game.moneyWon += 1000 * (game.currentQuestion + 1); if (++game.currentQuestion == numQuestions) { game.gameOver = 1; printf("\nTebrikler! $%d kazandınız!\n", game.moneyWon); } } else { printf("Yanlış!\nOyun bitti. $%d kazandınız.\n", game.moneyWon); game.gameOver = 1; } } } int main() { srand(time(NULL)); int numQuestions; Question* questions = loadQuestions("questions.txt", &numQuestions); if (!questions) return 1; printf("\n=== KIM MİLYONER OLMAK İSTER ===\n"); playGame(questions, numQuestions); free(questions); return 0; }
10 Fransa'nın başkenti neresidir? Paris Berlin Londra Roma Tokyo ... (her soru için tekrar)
Derleme: gcc millionaire.c -o millionaire