X-Git-Url: https://git.cworth.org/git?p=wordgame;a=blobdiff_plain;f=word-game.c;h=48e8692edaf41a0ef8f6d65f3ccf964878bd3c98;hp=d4fa77e822582cc82086fc17779857a9baacaaad;hb=HEAD;hpb=95950fcacd327b24eb22bad24d15f377c0640e2c diff --git a/word-game.c b/word-game.c index d4fa77e..48e8692 100644 --- a/word-game.c +++ b/word-game.c @@ -1,7 +1,7 @@ /* * Copyright © 2006 Carl Worth * - * This program is free software; you can redistribute it and\/or modify + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. @@ -57,6 +57,24 @@ _count_possible (dict_cursor_t cursor, _count_possible (dict_cursor_next (cursor, c), depth + 1, possible); } +static void +_print_stats (int found[WORD_GAME_MAX_WORD_LENGTH+1], + int possible[WORD_GAME_MAX_WORD_LENGTH+1], + int found_total, + int possible_total) +{ + int i; + + for (i = 2; i <= 17; i++) + if (possible[i]) + printf ("%d:(%d/%d) ", i, found[i], possible[i]); + + printf ("total:(%d/%d = %.2f%%)\n", + found_total, possible_total, + 100 * (double) found_total / possible_total); +} + + void word_game_play (const char *puzzle, dict_t *dict, @@ -94,11 +112,17 @@ word_game_play (const char *puzzle, sprintf (prompt, "> "); remaining = 1; } - do { + while (1) { if (time_limit_seconds) { - minutes = remaining / 60; - seconds = remaining % 60; - sprintf (prompt, "%02d:%02d ", minutes, seconds); + if (remaining > 0) { + minutes = remaining / 60; + seconds = remaining % 60; + sprintf (prompt, "%02d:%02d ", minutes, seconds); + } else { + printf ("Time's up.\a\n"); + sprintf (prompt, "> "); + time_limit_seconds = 0; + } } response = readline (prompt); if (response == NULL) @@ -108,12 +132,7 @@ word_game_play (const char *puzzle, printf ("%s\n", puzzle); just_saw_puzzle = TRUE; } else { - for (i = 2; i <= 17; i++) - if (possible[i]) - printf ("%d:(%d/%d) ", i, found[i], possible[i]); - printf ("total:(%d/%d = %.2f%%)\n", - found_total, possible_total, - 100 * (double) found_total / possible_total); + _print_stats (found, possible, found_total, possible_total); } } else { dict_entry_t *entry; @@ -144,9 +163,13 @@ word_game_play (const char *puzzle, remaining = floor (0.5 + (tv_stop.tv_sec - tv.tv_sec) + (tv_stop.tv_usec - tv.tv_usec) / 1000000.0); minutes = remaining / 60; } - } while (remaining > 0); + } - printf ("%s\n", puzzle); + printf ("\n%s\n", puzzle); + + printf ("\n"); + _print_stats (found, possible, found_total, possible_total); + printf ("\n"); if (found_total) { printf ("Words you found:\n");