From 65e7a2c21588a2e3ce73b33db0e59490dfdd8815 Mon Sep 17 00:00:00 2001 From: Tobias Rehbein Date: Thu, 23 Apr 2009 22:55:34 +0200 Subject: [PATCH] Scoring This commit adds a simple scoring system. You have 60 seconds to enter your guess (the period may be changed using the TIMEOUT define in common.h). If your guess is wrong 60 (or whatever value TIMEOUT is set to) points are added to your score. If the guess was right only the time used to enter your guess gets added to your score. The player with the lowest score is the winner. At the moment the player gets no feedback how much time has passed. But this won't change until the UI has been reimplemented; this is just a prototype ;) * add simple scoring * show scoreboard Closes GH-1. --- common.h | 2 ++ oggquiz.c | 22 ++++++++++++++++------ ui.c | 11 +++++++++++ ui.h | 3 +++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/common.h b/common.h index 97afab1..9cb142c 100644 --- a/common.h +++ b/common.h @@ -14,6 +14,7 @@ #define ALBUMLEN 128 #define TITLELEN 128 +#define TIMEOUT 60 #define CHOICES 4 #define PLAYERS 4 @@ -23,4 +24,5 @@ #define safe_strncpy(dst, src, len) strncpy(dst, src, (len)-1); \ dst[(len)-1] = '\0'; +#define min(a, b) ((a) < (b)) ? a : b #endif diff --git a/oggquiz.c b/oggquiz.c index 190661a..a080c91 100644 --- a/oggquiz.c +++ b/oggquiz.c @@ -52,27 +52,37 @@ main(int argc, char **argv) static int new_turn(oggfile_t * oggfiles) { - static int rpg_initialized = 0; + static int first_invocation = 0; + static ui_model_t model; int correct; char guess; - ui_model_t model; + time_t start; - if (!rpg_initialized) { - rpg_initialized = 1; + if (!first_invocation) { + first_invocation = 1; srand(time(NULL)); + /* abusing the correct variable as a temporary variable */ + for (correct = 0; correct < CHOICES; correct++) { + model.scores[correct] = 0; + model.turn = 0; + } } + model.turn++; + model.current_player = ((model.turn - 1) % PLAYERS); model.oggfiles = oggfiles; correct = rand() % 4; model.correct = &oggfiles[correct]; ui_display_quiz(&model); player_play(model.correct); + start = time(NULL); guess = ui_get_answer(); if (guess == 'q') return 1; model.guess = &oggfiles[guess - '1']; if (model.guess == model.correct) - /* TODO: scoring */ - puts("debug: right guess"); + model.scores[model.current_player] += min(TIMEOUT, time(NULL) - start); + else + model.scores[model.current_player] += TIMEOUT; ui_display_result(&model); ui_pause(); return 0; diff --git a/ui.c b/ui.c index 1a893e6..e8f8f18 100644 --- a/ui.c +++ b/ui.c @@ -41,6 +41,9 @@ ui_display_quiz(ui_model_t * model) void ui_display_result(ui_model_t * model) { + int i; + char mark; + if (model->correct == model->guess) printf("\nYou are right!\n"); else { @@ -51,6 +54,14 @@ ui_display_result(ui_model_t * model) printf("\nYou are listening to:\n"); print_oggfile(model->correct); + puts("\nScoreboard:\n"); + for (i = 0; i < PLAYERS; i++) { + if (i == model->current_player) + mark = '*'; + else + mark = ' '; + printf("%c Player %d: %5d %c\n", mark, i + 1, model->scores[i], mark); + } puts(""); } diff --git a/ui.h b/ui.h index d24f1e4..e0b17ca 100644 --- a/ui.h +++ b/ui.h @@ -16,6 +16,9 @@ typedef struct { oggfile_t *oggfiles; oggfile_t *guess; oggfile_t *correct; + int turn; + int scores[PLAYERS]; + int current_player; } ui_model_t; void ui_display_quiz(ui_model_t * model); -- 2.11.4.GIT