Saved individual high score.
authorlimb <limb@bc5cbbab-a4ec-0310-bb52-ff3d296db539>
Mon, 12 Feb 2007 14:41:11 +0000 (14:41 +0000)
committerlimb <limb@bc5cbbab-a4ec-0310-bb52-ff3d296db539>
Mon, 12 Feb 2007 14:41:11 +0000 (14:41 +0000)
git-svn-id: svn+ssh://svn/var/repos/curfender@635 bc5cbbab-a4ec-0310-bb52-ff3d296db539

CHANGELOG
main.cpp

index 31c708e..cd4be69 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,4 @@
+Saved individual high score.
 Title screen.
 Only saved pods respawned for 4 levels.
 Copyright info.
index d55e17e..465c27b 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -61,15 +61,6 @@ struct game_object {
   char radar[1]; //radar symbol
 };
 
-int high_score(){
-  return score;
-};
-
-void record_score(int score){
-  
-};
-
 int check_collision(game_object object1, game_object object2){
   int collided = 0;
   int spacing = 2;
@@ -536,9 +527,37 @@ int life_loss(int lives, int score){
     halfdelay(1);
   } else {
     pause = 0;
+    
+    //record score if high
+    
+    int hscore = 0;
+    char filename[250];
+    char outstring[50];
+    strcpy (filename, "/.curfender_scores");
+    char home[250];
+    strcpy(home, getenv("HOME"));
+    
+    strncat(home, filename, 20);
+    
+    FILE *scorefile = fopen(home, "r");
+    char workchara[6];
+    fgets(workchara, 6, scorefile);
+    hscore = atoi(workchara);
+    fclose(scorefile);
+    
+    if(score>hscore){
+      FILE *writescorefile = fopen(home, "w");
+      sprintf(outstring, "%d", score);
+      fputs(outstring, writescorefile);
+      fclose(writescorefile);
+    };
+    
     while(pause!=' '){
       cbreak();
       mvprintw(10,20,"GAME OVER. Score:%d  Press SPACE to exit.\n", score);
+      if(score>hscore){
+       mvprintw(11,20,"High Score: %s", outstring);
+      };
       pause = getch();
     };
       endwin();
@@ -951,34 +970,26 @@ int main(){
   //high score
 
   int hscore = 0;
-  int hscores[10];
-  char hname[15];
-  char hnames[10][15];
-  //char hold[200];
-  char hold[20];
-  FILE *scorefile = fopen("/var/games/curfender/scores", "r");  //might want to handle failure, eventually
-  for(int x=0;x<10;x++){
-    char *workchara;
-    char *workcharb;
-    fgets(hold, 20, scorefile);
-    workchara = strtok(hold, " ");
-    workcharb = strpbrk(hold, " ");
-    //strncpy(workchara, hold, 5);
-    //strncpy(workcharb, hold, 15);
-    hscores[x] = atoi(workchara);
-    strcpy(hnames[x], workcharb);
-  };
-  fclose(scorefile);
-  for(int x=0;x<10;x++){
-    if(hscores[x]>hscore){
-      hscore=hscores[x];
-      strcpy(hname,hnames[x]);
-    };
+  char filename[250];
+  strcpy (filename, "/.curfender_scores");
+  char home[250];
+  strcpy(home, getenv("HOME"));
+  
+  strncat(home, filename, 20);
+  
+  if(FILE *scorefile = fopen(home, "r")){
+    char workchara[6];
+    fgets(workchara, 6, scorefile);
+    hscore = atoi(workchara);
+    fclose(scorefile);
+  } else {
+    FILE *scorefile = fopen(home, "w");
+    fputs("0\n", scorefile);
+    fclose(scorefile);
+    hscore = 0;
   };
   
-  //strcpy(hname, "limb");//debug
-
-  mvprintw(13,34,"High Score: %s %d",hname,hscore);
+  mvprintw(13,34,"High Score: %d",hscore);
   
   //get the cursor out of the way
   mvprintw(23,79,"-");