The music module is complete. The only file that changes now is
game.c. main.c, load.c, and display.c are untouched.
Add music_pid to game_loop
Open game.c. At the top of game_loop, alongside the existing local
variables, add one more:
pid_t music_pid;pid_t is available through music.h, which game.h now includes.
Start music at game start
Immediately after the existing variable initializations, before the
while loop, start tier 1:
music_pid = start_music("music/tier1.wav");Switch tiers on correct answers
Inside the if (c - 'A' == q->answer) branch, after the
tci_printf("\nCorrect!\n") line and the level++ increment, add the
tier-switch logic:
if (level == 5)
{
stop_music(music_pid);
music_pid = start_music("music/tier2.wav");
}
else if (level == 10)
{
stop_music(music_pid);
music_pid = start_music("music/tier3.wav");
}level has already been incremented by this point. When level
becomes 5, the player just answered Q5 correctly and is moving to Q6 —
that is when tier 2 begins.
Stop music on all exit paths
There are three ways the game ends: win, loss, and walk-away. Each must stop the music before returning or breaking.
Walk-away — inside the if (c == 'W') block, before
display_walkaway:
stop_music(music_pid);
display_walkaway(level);
free_questions(questions, count);
return ;Win — inside the if (level == LEVELS) block, before
display_win:
stop_music(music_pid);
display_win();
break;Loss — in the else branch (wrong answer), before
display_loss:
stop_music(music_pid);
display_loss(safe_level);
break;The complete game_loop
After all changes, game_loop in game.c looks like this:
void game_loop(question_t **questions, int count)
{
int level;
int safe_level;
int lifelines;
int hidden[4];
int i;
char c;
question_t *q;
pid_t music_pid;
level = 0;
safe_level = -1;
lifelines = 7;
music_pid = start_music("music/tier1.wav");
while (level < LEVELS && level < count) {
q = questions[level];
for (i = 0; i < 4; i++)
hidden[i] = 0;
display_ladder(level, safe_level);
if (SAFE[level])
safe_level = level;
tci_printf("Level %d — %s\n\n", level + 1, PRIZES[level]);
display_question(q, hidden);
tci_printf("[1] 50:50 [2] Phone [3] Audience "
"[W] Walk away\n");
tci_printf("Your answer (A-D): ");
while (1) {
c = read_input();
if (c == 'A' || c == 'B' || c == 'C' || c == 'D')
break;
if (c == '1' || c == '2' || c == '3' || c == 'W') {
if (c == 'W') {
stop_music(music_pid);
display_walkaway(level);
free_questions(questions, count);
return ;
}
handle_lifeline(q, &lifelines, hidden, c);
display_question(q, hidden);
tci_printf("Your answer (A-D): ");
}
}
if (c - 'A' == q->answer) {
tci_printf("\nCorrect!\n");
level++;
if (level == 5) {
stop_music(music_pid);
music_pid = start_music("music/tier2.wav");
} else if (level == 10) {
stop_music(music_pid);
music_pid = start_music("music/tier3.wav");
}
if (level == LEVELS) {
stop_music(music_pid);
display_win();
break;
}
} else {
stop_music(music_pid);
display_loss(safe_level);
break;
}
}
free_questions(questions, count);
}Build check
make re
./game questions.txtPlay through several questions. Verify:
- Tier 1 music starts immediately.
- After answering Q5 correctly, the music changes.
- After answering Q10 correctly, the music changes again.
- Music stops on win, loss, and walk-away.
- No
aplayprocesses remain after the game exits (pgrep aplayshould return nothing).