Arts >> Movies & TV >> Reality TV

C programming for the game who wants to be a millionaire?

```c

#include

#include

#include

#include

// Define the question structure

typedef struct {

char question[256];

char answer[256];

char option1[256];

char option2[256];

char option3[256];

char option4[256];

} Question;

// Define the game state structure

typedef struct {

int currentQuestion;

int moneyWon;

int lifelineUsed[3]; // 0 for unused, 1 for used

int gameOver;

} GameState;

// Function to load questions from a file

Question* loadQuestions(const char* filename, int* numQuestions) {

FILE* fp = fopen(filename, "r");

if (fp == NULL) {

printf("Error opening question file!\n");

return NULL;

}

// Read the number of questions

fscanf(fp, "%d\n", numQuestions);

// Allocate memory for the questions

Question* questions = malloc(*numQuestions * sizeof(Question));

if (questions == NULL) {

printf("Error allocating memory for questions!\n");

fclose(fp);

return NULL;

}

// Read the questions from the file

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;

}

// Function to display the current question

void displayQuestion(Question question) {

printf("\n%s\n\n", question.question);

printf("1. %s\n", question.option1);

printf("2. %s\n", question.option2);

printf("3. %s\n", question.option3);

printf("4. %s\n", question.option4);

}

// Function to handle player's answer

int getAnswer(GameState* game) {

int answer;

printf("\nEnter your answer (1-4): ");

scanf("%d", &answer);

// Validate answer

while (answer < 1 || answer > 4) {

printf("Invalid answer! Please enter a number between 1 and 4: ");

scanf("%d", &answer);

}

return answer;

}

// Function to check if the answer is correct

int checkAnswer(Question question, int answer) {

if (answer == 1 && strcmp(question.option1, question.answer) == 0) {

return 1;

} else if (answer == 2 && strcmp(question.option2, question.answer) == 0) {

return 1;

} else if (answer == 3 && strcmp(question.option3, question.answer) == 0) {

return 1;

} else if (answer == 4 && strcmp(question.option4, question.answer) == 0) {

return 1;

} else {

return 0;

}

}

// Function to handle lifelines

void useLifeline(GameState* game) {

int choice;

printf("\nWhich lifeline would you like to use?\n");

printf("1. 50/50\n");

printf("2. Ask the Audience\n");

printf("3. Phone a Friend\n");

printf("Enter your choice (1-3): ");

scanf("%d", &choice);

// Validate choice

while (choice < 1 || choice > 3) {

printf("Invalid choice! Please enter a number between 1 and 3: ");

scanf("%d", &choice);

}

// Check if the lifeline is already used

if (game->lifelineUsed[choice - 1] == 1) {

printf("You have already used this lifeline!\n");

return;

}

// Implement lifeline logic here (e.g., remove incorrect options, simulate audience vote, etc.)

game->lifelineUsed[choice - 1] = 1;

printf("Lifeline used successfully!\n");

}

// Function to play the game

void playGame(Question* questions, int numQuestions) {

GameState game;

game.currentQuestion = 0;

game.moneyWon = 0;

memset(game.lifelineUsed, 0, sizeof(game.lifelineUsed));

game.gameOver = 0;

while (!game.gameOver) {

// Display current question

displayQuestion(questions[game.currentQuestion]);

// Handle lifelines

printf("\nDo you want to use a lifeline? (y/n): ");

char lifelineChoice;

scanf(" %c", &lifelineChoice);

if (lifelineChoice == 'y') {

useLifeline(&game);

}

// Get player's answer

int answer = getAnswer(&game);

// Check if the answer is correct

if (checkAnswer(questions[game.currentQuestion], answer)) {

printf("Correct!\n");

game.moneyWon += 1000 * (game.currentQuestion + 1);

game.currentQuestion++;

if (game.currentQuestion == numQuestions) {

game.gameOver = 1;

printf("\nCongratulations! You won $%d!\n", game.moneyWon);

break;

}

} else {

printf("Incorrect!\n");

game.gameOver = 1;

printf("\nYou won $%d. Better luck next time!\n", game.moneyWon);

break;

}

}

}

int main() {

srand(time(NULL));

int numQuestions;

Question* questions = loadQuestions("questions.txt", &numQuestions);

if (questions == NULL) {

return 1;

}

printf("\nWelcome to Who Wants to Be a Millionaire!\n");

playGame(questions, numQuestions);

free(questions);

return 0;

}

```

How to use this code:

1. Create a question file: Create a text file named `questions.txt` and follow this format:

```

10 // Number of questions

What is the capital of France? // Question

Paris // Correct answer

Berlin // Option 1

London // Option 2

Rome // Option 3

Tokyo // Option 4

... // Add more questions in the same format

```

2. Compile and run: Compile the code using a C compiler (e.g., GCC) and run the executable.

Explanation:

* Data Structures:

* `Question` structure: Holds the question text, answer, and multiple-choice options.

* `GameState` structure: Tracks the current question, money won, lifelines used, and game over status.

* Functions:

* `loadQuestions()`: Loads questions from a file.

* `displayQuestion()`: Displays the current question and options.

* `getAnswer()`: Prompts the player for an answer and validates input.

* `checkAnswer()`: Compares the player's answer with the correct answer.

* `useLifeline()`: Handles lifeline selection and implementation (placeholder for actual lifeline logic).

* `playGame()`: Manages the main game loop, handling questions, answers, and lifelines.

* Game Logic:

* The `playGame()` function iterates through questions until the player reaches the final question or answers incorrectly.

* The player can use lifelines by choosing options from a menu.

* The game keeps track of the player's progress and money won.

To implement the lifelines, you will need to add the following in the `useLifeline()` function:

* 50/50: Remove two incorrect options randomly.

* Ask the Audience: Generate random audience votes, giving the correct answer a higher chance of winning.

* Phone a Friend: Generate a random "friend's" answer, which could be correct or incorrect with a certain probability.

This code provides a basic structure for a "Who Wants to Be a Millionaire" game. You can enhance it further by:

* Implementing more advanced lifeline logic.

* Adding visual elements and animations.

* Incorporating more complex question formats.

* Saving and loading game progress.

Reality TV

Related Categories