/* loadScores Input: char* f1 The filename where the scores are saved HighScores* hiScores The structure to store the scores in Output: int the number of scores successfuly loaded, -1 if the file failed to open Loads the high score table from a file */ int loadScores(char* f1, HighScores* hiScores) { FILE *fd1; /* File pointer */ int i; /*Counters */ int numScores; /* the number of scores in the file*/ if ((fd1 = fopen(f1,"r")) == NULL) { return -1 ; } else { hiScores->file =f1; fscanf(fd1,"%d",&numScores) ; for (i = 0 ; i < numScores && !feof ( fd1) ; i++) { fscanf(fd1,"%d %s", & hiScores->record[i].score, &hiScores->record[i].initials) ; } fclose(fd1) ; hiScores->numScores= numScores; sortScores ( hiScores ); return numScores; } }
void displayScores(scores* s, int sr){ f3d_lcd_fillScreen(BLACK); f3d_lcd_drawString(30,10,"High Scores",RED,BLACK); f_mount(0, &Fatfs); setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); int i,j; int currname = 0, currletter = 0, currmoves=0, currdigit = 0; if(!sr){ rc = f_open(&Fil, "SCORES.TXT", FA_READ); if(rc) die(rc); for (;;) { rc = f_read(&Fil, Buff, sizeof Buff, &br); /* Read a chunk of file */ if(rc) die(rc); if (rc || !br) break; /* Error or end of file */ for (i = 0; i < br; i++){ while(Buff[i]!='.'){ (*s).names[currname][currletter]=Buff[i]; currletter++; i++; } i++; currname++; currletter = 0; while(Buff[i]>=48&&Buff[i]<=57){ (*s).strmoves[currmoves][currdigit]=Buff[i]; currdigit++; i++; } currdigit = 0; int numdigits = 0; for(j=0;j<5;j++){ if((*s).strmoves[currmoves][j]!='\0') numdigits++; else break; } for(j=0;j<5;j++){ if((*s).strmoves[currmoves][j]=='\0') break; (*s).moves[currmoves]+=((*s).strmoves[currmoves][j]-48)*pow(10,(numdigits-j-1)); } currmoves++; } } rc = f_close(&Fil); if(rc) die(rc); sortScores(s); } for(i=0;i<currname;i++) { f3d_lcd_drawString(10,30+10*i,(*s).names[i],WHITE,BLACK); f3d_lcd_drawString(100,30+10*i,(*s).strmoves[i],WHITE,BLACK); } }
int main() { scores s; addScore(&s,80); addScore(&s,5); addScore(&s,600); addScore(&s,120); sortScores(&s); int i; for(i=0;i<MAXSCORES;i++){ printf("name = %s move = %d\n",s.names[i],s.moves[i]); } return 0; }
/* insertHiScore Input: Player thePlayer The player to be inserted HighScores* hiScores The group of high scores Output: none Adds the player's score and intials to the high score table */ void insertHiScore ( Player thePlayer, HighScores* hiScores) { int setScore; /* index of where the plaer will be inserted */ if ( hiScores->numScores == MAX_SCORES ) { setScore = hiScores->numScores-1; } else { hiScores->numScores++; setScore = hiScores->numScores -1; } hiScores->record[setScore].score= thePlayer.score; strcpy ( hiScores->record[ setScore ].initials, thePlayer.initials); sortScores( hiScores ); saveScores ( hiScores); }
void addScore(scores* s, int m){ int i, numchars = 0, numscores = 0; for(i=0;i<MAXSCORES;i++){ if((*s).moves[i]!=0) numscores++; } if(numscores==10&&m>(*s).moves[9]) return; printf("Type in your name: "); char ch = 'a'; char name[25]; while(ch!='\n'){ if(numchars=25) break; ch = getchar(); if(ch!='\x01'){ putchar(ch); name[numchars] = ch; numchars++; } } if(numscores==10) numscores=9; strCopy((*s).names[numscores],name,25); (*s).moves[numscores] = m; i=0; int temp = m; while(temp>0){ temp/=10; i++; } i--; for(i=i; i>=0; i--){ (*s).strmoves[numscores][i]=m%10; m/=10; } sortScores(s); }