-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhiscore.c
320 lines (243 loc) · 7.87 KB
/
hiscore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/***************************************
$Header: /home/amb/CVS/xbomb/hiscore.c,v 1.13 2008-01-05 19:23:07 amb Exp $
XBomb - 'Minesweeper' game - Version 2.3
Hi-score table management.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1994-2007 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#if defined(__sun__) && !defined(__svr4__)
int fprintf(FILE*, const char*,...);
int printf(const char*, ... );
int sscanf(char*, const char*,...);
int fread(void*,unsigned int,unsigned int, FILE*);
int fwrite(const void*,unsigned int,unsigned int, FILE*);
int fclose(FILE*);
long time(long*);
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "xbomb.h"
/*+ The current information about the grid +*/
extern int grid_type, /*+ type. +*/
grid_level; /*+ level. +*/
/*+ The names of the different game +*/
extern char *levels[NLEVELS], /*+ levels (difficulty). +*/
*types[NTYPES]; /*+ types (grid shapes). +*/
/*+ The size of the grids +*/
extern int widths[NLEVELS], /*+ width in tiles. +*/
heights[NLEVELS], /*+ height in tiles. +*/
nbombs[NLEVELS]; /*+ number of bombs. +*/
/*+ The names of the high score tables. +*/
static char *filenames[NTYPES]={"/var/tmp/xbomb6.hi","/var/tmp/xbomb4.hi","/var/tmp/xbomb3.hi"};
/*+ The names of the positions in the high score tables. +*/
static char pos[11][5]={"Top","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","Lost"};
/*+ The list of hi-scores for one grid type. +*/
static int score[NLEVELS+1][11];
/*+ The list of hi-score usernames for one grid type. +*/
static char name[NLEVELS+1][11][21];
/*+ The list of hi-score dates for one grid type. +*/
static long date[NLEVELS+1][11];
/*+ Which of the hoigh scores in the table to highlight. +*/
static int which_hiscore=-1;
/*+ A structure to store the high score information in the file. +*/
struct score_name
{
long score; /*+ The time in milliseconds. +*/
char name[20]; /*+ The user name. +*/
long date; /*+ The time in seconds from the epoch. +*/
};
static void load_high_scores(void);
static void save_high_scores(void);
static void decrypt_score(struct score_name* sn);
static void encrypt_score(struct score_name* sn);
/*++++++++++++++++++++++++++++++++++++++
Prints the high score tables.
++++++++++++++++++++++++++++++++++++++*/
void PrintHighScores(void)
{
int l,j;
load_high_scores();
printf("\nHigh score tables for %s\n",types[grid_type-GAME_TYPE]);
for(l=0;l<NLEVELS;l++)
{
printf("\nHigh scores for %s level (%dx%d grid of %s with %d bombs)\n",levels[l],widths[l],heights[l],types[grid_type-GAME_TYPE],nbombs[l]);
for(j=0;j<10;j++)
{
printf("%4s : %12s = ",pos[j],name[l][j]);
if(!date[l][j])
printf(" SLOW");
else if(l==0)
printf("%6.2f",(double)score[l][j]/1000.0);
else if(l==1)
printf("%6.1f",(double)score[l][j]/1000.0);
else
printf("%6.0f",(double)score[l][j]/1000.0);
if(date[l][j])
printf(" : %s",ctime(&date[l][j]));
else
{printf(" : Never\n");break;}
}
printf("\n");
}
}
/*++++++++++++++++++++++++++++++++++++++
Adds a high score to the table if it is good enough.
int ticks The time that was taken.
++++++++++++++++++++++++++++++++++++++*/
void AddHighScore(int ticks)
{
int j;
int changed=-1;
struct passwd *pwd;
char *username;
load_high_scores();
pwd=getpwuid(geteuid());
if(pwd)
username=pwd->pw_name;
else
username="?unknown?";
which_hiscore=10;
score[grid_level-GAME_LEVEL][10]=ticks;
strcpy(name[grid_level-GAME_LEVEL][10],username);
date[grid_level-GAME_LEVEL][10]=time(NULL);
for(j=0;j<10;j++)
if(ticks<score[grid_level-GAME_LEVEL][j])
{changed=which_hiscore=j;break;}
if(changed!=-1)
{
for(j=9;j>changed;j--)
{
score[grid_level-GAME_LEVEL][j]=score[grid_level-GAME_LEVEL][j-1];
strcpy(name[grid_level-GAME_LEVEL][j],name[grid_level-GAME_LEVEL][j-1]);
date[grid_level-GAME_LEVEL][j]=date[grid_level-GAME_LEVEL][j-1];
}
score[grid_level-GAME_LEVEL][changed]=ticks;
strcpy(name[grid_level-GAME_LEVEL][changed],username);
date[grid_level-GAME_LEVEL][changed]=time(NULL);
save_high_scores();
}
}
/*++++++++++++++++++++++++++++++++++++++
Select a set of high scores for display.
++++++++++++++++++++++++++++++++++++++*/
void ShowHighScores(void)
{
char datestr[11][32],*scores[11][4],scorestr[11][10];
int i;
load_high_scores();
for(i=0;i<11;i++)
{
if(i==10 && which_hiscore!=10)
{scores[i][0]=scores[i][1]=scores[i][2]=scores[i][3]="";continue;}
strcpy(datestr[i],ctime(&date[grid_level-GAME_LEVEL][i]));
scores[i][0]=pos[i];
scores[i][1]=name[grid_level-GAME_LEVEL][i];
if(!date[grid_level-GAME_LEVEL][i])
scores[i][2]="SLOW";
else if(grid_level==GAME_EASY)
{sprintf(scorestr[i],"%6.2f",(double)score[grid_level-GAME_LEVEL][i]/1000.0);scores[i][2]=scorestr[i];}
else if(grid_level==GAME_MEDIUM)
{sprintf(scorestr[i],"%6.1f",(double)score[grid_level-GAME_LEVEL][i]/1000.0);scores[i][2]=scorestr[i];}
else
{sprintf(scorestr[i],"%6.0f",(double)score[grid_level-GAME_LEVEL][i]/1000.0);scores[i][2]=scorestr[i];}
if(date[grid_level-GAME_LEVEL][i])
scores[i][3]=datestr[i];
else
scores[i][3]="Never";
}
DisplayHighScores(scores,which_hiscore);
which_hiscore=-1;
}
/*++++++++++++++++++++++++++++++++++++++
Load in the high score table.
++++++++++++++++++++++++++++++++++++++*/
static void load_high_scores(void)
{
FILE *f;
int i,j;
struct score_name sn;
for(i=0;i<NLEVELS;i++)
for(j=0;j<10;j++)
{
score[i][j]=9999999;
strcpy(name[i][j],"nobody");
date[i][j]=0;
}
f=fopen(filenames[grid_type-GAME_TYPE],"r");
if(!f)
{
fprintf(stderr,"XBomb: Cannot open high score table '%s' to read.\n",filenames[grid_type-GAME_TYPE]);
return;
}
for(i=0;i<NLEVELS;i++)
for(j=0;j<10;j++)
{
if(!fread(&sn,sizeof(sn),1,f))
break;
decrypt_score(&sn);
score[i][j]=sn.score;
strcpy(name[i][j],sn.name);
date[i][j]=sn.date;
}
fclose(f);
}
/*++++++++++++++++++++++++++++++++++++++
Saves the high score table.
++++++++++++++++++++++++++++++++++++++*/
static void save_high_scores(void)
{
FILE *f;
int i,j;
struct score_name sn;
f=fopen(filenames[grid_type-GAME_TYPE],"w");
if(!f)
{
fprintf(stderr,"XBomb: Cannot open high score table '%s' to write.\n",filenames[grid_type-GAME_TYPE]);
return;
}
for(i=0;i<NLEVELS;i++)
for(j=0;j<10;j++)
{
sn.score=score[i][j];
strcpy(sn.name,name[i][j]);
sn.date=date[i][j];
encrypt_score(&sn);
fwrite(&sn,sizeof(sn),1,f);
}
fclose(f);
chmod(filenames[grid_type-GAME_TYPE],S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
}
/*++++++++++++++++++++++++++++++++++++++
Decrypts the high score table
struct score_name* sn The score and name to decrypt.
++++++++++++++++++++++++++++++++++++++*/
static void decrypt_score(struct score_name* sn)
{
int i;
char *p=(char*)sn;
for(i=0;i<sizeof(struct score_name);i++)
p[i]^=125;
return;
}
/*++++++++++++++++++++++++++++++++++++++
Encrypts the high score table
struct score_name* sn The score and name to encrypt.
++++++++++++++++++++++++++++++++++++++*/
static void encrypt_score(struct score_name* sn)
{
int i;
char *p=(char*)sn;
for(i=0;i<sizeof(struct score_name);i++)
p[i]^=125;
return;
}