diff options
-rw-r--r-- | csol.c | 150 | ||||
-rw-r--r-- | klondike.c | 4 | ||||
-rw-r--r-- | klondike.h | 2 |
3 files changed, 145 insertions, 11 deletions
@@ -24,6 +24,7 @@ */ #include <stdlib.h> #include <stdio.h> +#include <string.h> #include <time.h> #include <curses.h> @@ -33,8 +34,113 @@ #include "util.h" #include "klondike.h" +#define NO_GAMES 4 + +typedef struct +{ + unsigned long played; + unsigned long won; +} Score; + +static Score score[NO_GAMES]; +static char score_file[4096]; + +static void LoadScores() +{ + FILE *fp; + + if (!score_file[0]) + { + char *home = NULL; + + home = getenv("HOME"); + + if (home) + { + strcpy(score_file, home); + } + + strcat(score_file,"/.csolscore"); + } + + fp = fopen(score_file, "r"); + + if (fp) + { + int f; + char line[80]; + + for(f = 0; f < NO_GAMES; f++) + { + if (fgets(line, sizeof line, fp)) + { + char *p; + + p = strtok(line, "\r\n "); + + if (p) + { + score[f].played = strtoul(p, NULL, 0); + + p = strtok(NULL, "\r\n "); + + if (p) + { + score[f].won = strtoul(p, NULL, 0); + } + } + } + } + + fclose(fp); + } +} + +static const char *GetScore(int game) +{ + static char buff[80]; + + if (score[game].played == 0) + { + strcpy(buff, "0 played, 0.00% won"); + } + else + { + snprintf(buff, sizeof buff, "%lu played, %.2f%% won", + score[game].played, + (double)score[game].won / (double)score[game].played * 100.0); + } + + return buff; +} + +static void SaveScores() +{ + FILE *fp; + int f; + + fp = fopen(score_file, "w"); + + if (fp) + { + for(f = 0; f < NO_GAMES; f++) + { + fprintf(fp, "%lu %lu\n", score[f].played, score[f].won); + } + + fclose(fp); + } +} + int main(int argc, char *argv[]) { + const char *game_title[NO_GAMES] = + { + "1 ............. Klondike (draw three)", + "2 ............... Klondike (draw one)", + "3 .. Thoughtful Klondike (draw three)", + "4 .... Thoughtful Klondike (draw one)" + }; int key; int opt = 1; int quit = FALSE; @@ -46,38 +152,52 @@ int main(int argc, char *argv[]) noecho(); keypad(stdscr, TRUE); + LoadScores(); + while(!quit) { int row = 3; + int won = 0; + int game; + int f; erase(); Centre(1, "CURSES solitaire"); - Centre(row++, "1 ............. Klondike (draw three)"); - Centre(row++, "2 ............... Klondike (draw one)"); - Centre(row++, "3 .. Thoughtful Klondike (draw three)"); - Centre(row++, "4 .... Thoughtful Klondike (draw one)"); - Centre(row++, "Q .............................. Quit"); + + for(f = 0; f < NO_GAMES; f++) + { + mvprintw(row++, 0, "%s [%s]", game_title[f], GetScore(f)); + } + + row++; + mvaddstr(row++, 0, "Q .. Quit"); key = getch(); + game = -1; + switch(key) { case '1': erase(); - Klondike(3, FALSE); + game = 0; + won = Klondike(3, FALSE); break; case '2': erase(); - Klondike(1, FALSE); + game = 1; + won = Klondike(1, FALSE); break; case '3': erase(); - Klondike(3, TRUE); + game = 2; + won = Klondike(3, TRUE); break; case '4': erase(); - Klondike(1, TRUE); + game = 3; + won = Klondike(1, TRUE); break; case 'q': case 'Q': @@ -93,6 +213,18 @@ int main(int argc, char *argv[]) default: break; } + + if (game != -1) + { + score[game].played++; + + if (won) + { + score[game].won++; + } + + SaveScores(); + } } erase(); @@ -164,7 +164,7 @@ static void KlondikeHelp(void) while(getch() != ' '); } -void Klondike(int draw, int thoughtful) +int Klondike(int draw, int thoughtful) { Deck deck; Pile pile = {0}; @@ -510,6 +510,8 @@ void Klondike(int draw, int thoughtful) FreePile(&column_down[f]); FreePile(&column_up[f]); } + + return won; } @@ -27,7 +27,7 @@ #define CSOL_KLONDIKE_H -void Klondike(int draw, int thoughtful); +int Klondike(int draw, int thoughtful); #endif |