/* csol - CURSES solitaire Copyright (C) 2018 Ian Cowburn (ianc@noddybox.co.uk) 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ------------------------------------------------------------------------- Main */ #include #include #include #include #include #include "deck.h" #include "pile.h" #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; srand(time(NULL)); initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); LoadScores(); while(!quit) { int row = 3; int won = 0; int game; int f; erase(); Centre(1, "CURSES solitaire"); 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(); game = 0; won = Klondike(3, FALSE); break; case '2': erase(); game = 1; won = Klondike(1, FALSE); break; case '3': erase(); game = 2; won = Klondike(3, TRUE); break; case '4': erase(); game = 3; won = Klondike(1, TRUE); break; case 'q': case 'Q': quit = TRUE; break; case '?': erase(); Centre(2, "Press the indicated key to select the menu item."); Centre(4, "Press SPACE to continue."); refresh(); while(getch() != ' '); break; default: break; } if (game != -1) { score[game].played++; if (won) { score[game].won++; } SaveScores(); } } erase(); refresh(); endwin(); return EXIT_SUCCESS; } /* vim: ai sw=4 ts=8 expandtab */