~ubuntu-branches/ubuntu/jaunty/xbill/jaunty

« back to all changes in this revision

Viewing changes to Scorelist.cc

  • Committer: Bazaar Package Importer
  • Author(s): Adrian Bridgett
  • Date: 2001-06-24 22:44:40 UTC
  • Revision ID: james.westby@ubuntu.com-20010624224440-r8kbgt5ae7q1230g
Tags: upstream-2.0
ImportĀ upstreamĀ versionĀ 2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "objects.h"
 
2
 
 
3
FILE *Scorelist::open_file(char *mode) {
 
4
        char file[255];
 
5
        sprintf (file, "%sscores", XBILL_HOME);
 
6
        return fopen (file, mode);
 
7
}
 
8
 
 
9
void Scorelist::read() {
 
10
        FILE *scorefile = open_file("r");
 
11
        int i;
 
12
        if (scorefile) {
 
13
                for (i=0; i<10; i++) {
 
14
                        fgets (name[i], 21, scorefile);
 
15
                        fscanf (scorefile, "%d%d\n", &(level[i]), &(score[i]));
 
16
                }
 
17
                fclose(scorefile);
 
18
        }
 
19
        else
 
20
                for (i=0; i<10; i++) {
 
21
                        strcpy(name[i], "me");
 
22
                        level[i] = score[i] = 0;
 
23
                }
 
24
}
 
25
 
 
26
void Scorelist::write() {
 
27
        int i, j;
 
28
        FILE *scorefile = open_file("w");
 
29
        if (!scorefile) return;
 
30
        for (i=0; i<10; i++) {
 
31
                fputs(name[i], scorefile);
 
32
                for (j=strlen(name[i]); j<25; j++)
 
33
                        fputc(' ', scorefile);
 
34
                fprintf (scorefile, " %d %d\n", level[i], score[i]);
 
35
        }
 
36
        fclose(scorefile);
 
37
}
 
38
 
 
39
/*  Add new high score to list   */
 
40
void Scorelist::recalc (char *str) {
 
41
        int i;
 
42
        if (score[9] >= game.score) return;
 
43
        for (i=9; i>0; i--) {
 
44
                if (score[i-1] < game.score) {
 
45
                        strcpy (name[i], name[i-1]);
 
46
                        level[i] = level[i-1];
 
47
                        score[i] = score[i-1];
 
48
                }
 
49
                else break;
 
50
        }
 
51
        strcpy (name[i], str);
 
52
        level[i] = game.level;
 
53
        score[i] = game.score;
 
54
}
 
55
 
 
56
void Scorelist::update() {
 
57
        char str[500], temp[40];
 
58
        int i, j;
 
59
        strcpy (str,"High Scores:\n\n");
 
60
        strcat (str, "Name                 Level    Score\n");
 
61
        for (i=0; i<10; i++) {
 
62
                strcat (str, name[i]);
 
63
                for (j=strlen(name[i]); j<21; j++) strcat (str, " ");
 
64
                sprintf (temp, "%5d  %7d\n", level[i], score[i]);
 
65
                strcat (str, temp);
 
66
        }
 
67
        ui.update_hsbox(str);
 
68
}
 
69