#include #include #include #include #include "base.h" #include "score.h" void hi_scores_disp(int i) /**********************************************/ /* Function: Displays high score table */ /* */ /* Parameters: */ /* i: IN current index of the high score */ /* table */ /**********************************************/ { if (i < last_hi_score) goto recur; return; /*exit of recursivity*/ format_output(" -----------------------------------",0); recur: { /* print a score*/ printf (" | %10.10s ", hi_scores_tab [i].name); printf ("| %10.10s ", hi_scores_tab [i].firstname); printf ("| %.2d |\n", hi_scores_tab [i].score); i++; hi_scores_disp(i); } } int hi_scores_write () /* update the score file*/ { int i,status; hi_scores_file = fopen ("hi_score.lst", "w"); if (hi_scores_file == NULL) goto openerror; { /* writing loop */ i = 0; loopwrite: if (i == last_hi_score) goto endloopwrite; { fprintf (hi_scores_file, "%s\n", hi_scores_tab [i].name); fprintf (hi_scores_file, "%s\n", hi_scores_tab [i].firstname); fprintf (hi_scores_file, "%d\n", hi_scores_tab [i].score); i++; goto loopwrite; } endloopwrite: /* close the high score file*/ fclose (hi_scores_file); status = 0; goto endwrite; } openerror: { /* error when opening the high score file*/ status = 1; format_output("opening problem, file: hi_score.lst\n",1); } endwrite: { return(status); } } void player_score (int score) { char car; int max_string; char * name; char * firstname; char * number; int i,j; last_hi_score = 0; /*open the high score file*/ hi_scores_file = fopen ("hi_score.lst", "r"); if (hi_scores_file == NULL) goto nofile; /* fill the high score table */ car = getc (hi_scores_file); /* reading the file */ loopfic: if ((car == EOF) || (last_hi_score == 10)) goto endloopfic; /* end of loop test: reading file */ { name = (char *) malloc (sizeof (char) * MAX_STRING); firstname = (char *) malloc (sizeof (char) * MAX_STRING); number = (char *) malloc (sizeof (char) * MAX_STRING); /* read a name */ max_string = MAX_STRING; i = 0; loopname: /* reading name*/ if ((car == '\n') || (car == EOF)) goto endloopname; /*end of loop test: reading name*/ { if (i < max_string - 1) goto endifname; /*if size of name is too long: reallocation */ max_string += MAX_STRING; name = (char *) realloc (name, sizeof (char) * max_string); endifname: if (isupper(car)) /*if lowercase then uppercase*/ goto endupname; car = toupper(car); endupname: name [i] = car; car = getc (hi_scores_file); i++; } goto loopname; endloopname: name [i] = '\0'; (hi_scores_tab [last_hi_score]).name = name; /* reading firstname */ car = getc (hi_scores_file); max_string = MAX_STRING; i = 0; loopfirstname: /* reading a firstname */ if ((car == '\n') || (car == EOF)) goto endloopfirstname; /*end of loop test: reading firstname */ { if (i < max_string - 1) goto endiffirstname; /*if size of firstname is too long: reallocation */ max_string += MAX_STRING; firstname = (char *) realloc (firstname, sizeof (char) * max_string); endiffirstname: if (isupper(car)) /*if lowercase then uppercase */ goto endupfirstname; car = toupper(car); endupfirstname: firstname [i] = car; car = getc (hi_scores_file); i++; } goto loopfirstname; endloopfirstname: firstname [i] = '\0'; (hi_scores_tab [last_hi_score]).firstname = firstname; /* read a score */ car = getc (hi_scores_file); max_string = MAX_STRING; i = 0; loopnumber: /* reading score*/ if ((car == '\n') || (car == EOF)) goto endloopnumber; /*end of loop test: reading score */ { if (i < max_string - 1) goto endifnumber; /*if size of score is too long: reallocation*/ max_string += MAX_STRING; number = (char *) realloc (number, sizeof (char) * max_string); endifnumber: number [i] = car; car = getc (hi_scores_file); i++; } goto loopnumber; endloopnumber: number [i] = '\0'; (hi_scores_tab [last_hi_score]).score = atoi (number); free (number); car = getc (hi_scores_file); last_hi_score++; } goto loopfic; endloopfic: /* close hi_scores_file file */ fclose (hi_scores_file); goto updatefile; nofile: { /* no high score file*/ skipline(2); format_output(" -------------------------\n",0); format_output(" NO HIGH SCORE TABLE \n",0); format_output(" -------------------------\n\n",0); } updatefile: /* begin updating high score file*/ if ((score > hi_scores_tab [last_hi_score - 1].score) && (last_hi_score >= 10)) goto elsebadscore; /* if bad score: we are not in the table*/ { j = last_hi_score; loopupdate: /*loop to insert the current score*/ if ((j == 0) || (score > hi_scores_tab [j - 1].score)) goto endloopupdate; { if (j < 10) goto elseifupdate;/*include in the table*/ { /*if insertion in last position: free */ free (hi_scores_tab [j - 1].name); free (hi_scores_tab [j - 1].firstname); goto endifupdate; } elseifupdate: { /* move the end of the table down*/ hi_scores_tab [j].name = hi_scores_tab [j - 1].name; hi_scores_tab [j].firstname = hi_scores_tab [j - 1].firstname; hi_scores_tab [j].score = hi_scores_tab [j - 1].score; } endifupdate: j--; goto loopupdate; } endloopupdate: skipline(2); format_output(" OK! YOU ARE IN THE TOP TEN\n",0); skipline(1); format_output(" Please, enter your name: ",0); car = getchar (); /* read a name */ max_string = MAX_STRING; name = (char *) malloc (sizeof (char) * MAX_STRING); i = 0; loopgetname: /*reading a name at the screen*/ if (car == '\n') goto endloopgetname; /*end of loop test for a name*/ { if (i < max_string - 1) /*if size of name is too long: reallocation */ goto endifgetname; max_string += MAX_STRING; name = (char *) realloc (name, sizeof (char) * max_string); endifgetname: if (isupper(car)) /*if lowercase then uppercase*/ goto endupgetname; car = toupper(car); endupgetname: name [i] = car; car = getchar (); i++; } goto loopgetname; endloopgetname: name [i] = '\0'; /* firstname reading */ format_output(" Enter your firstname: ",0); car = getchar (); max_string = MAX_STRING; firstname = (char *) malloc (sizeof (char) * MAX_STRING); i = 0; loopgetfirstname: /*loop to read a firstname at the screen*/ if (car == '\n') goto endloopgetfirstname; /*end of loop test for a firstname*/ { if (i < max_string - 1) goto endifgetfirstname; max_string += MAX_STRING; firstname = (char *) realloc (firstname, sizeof (char) * max_string); endifgetfirstname: if (isupper(car)) /*if lowercase then uppercase*/ goto endupgetfirstname; car = toupper(car); endupgetfirstname: firstname [i] = car; car = getchar (); i++; } goto loopgetfirstname; endloopgetfirstname: firstname [i] = '\0'; hi_scores_tab [j].name = (char *) malloc (sizeof (char) * strlen (name)); strcpy (hi_scores_tab [j].name, name); hi_scores_tab [j].firstname = (char *) malloc (sizeof (char) * strlen (firstname)); strcpy (hi_scores_tab [j].firstname, firstname); hi_scores_tab [j].score = score; if (last_hi_score < 10) { last_hi_score++; } goto endupdate; } elsebadscore: /*we are not in the top ten*/ format_output(" Sorry !!!!You are not in the top 10!!!!\n",0); endupdate: skipline(1); format_output(" SEE YOU LATER\n",0); } void print_score_player (int try) { int i = 0; skipline(2); format_output("---------------------------------------------------------------\n",0); player_score (try); if (hi_scores_write() == 1) format_output("opening error, file: hi_score.lst\n",1); skipline(2); format_output(" -----------------------------------\n",0); format_output(" | HIGH SCORE TABLE |\n",0); format_output(" -----------------------------------\n",0); format_output(" -----------------------------------\n",0); format_output(" | NAME | FIRSTNAME | SCORE |\n",0); format_output(" -----------------------------------\n",0); hi_scores_disp (i); format_output(" -----------------------------------\n",0); }