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
|
/*
* CS 453 - Final project : An OpenGL version of the pegboard game IQ
* Due : June 5, 1997
* Author : Kiri Wagstaff
*
* File : score.c
* Description : Maintains and adds to highscores.
*
*/
#include "gliq.h"
int scores[10];
int totpegs[10];
char inits[10][4];
int minscore=0;
int minpegs=100;
char newinits[3];
int numentered=0;
int written=0;
float color1=1.0, color2=1.0, color3=0.0;
int lasthigh=-1;
void highscore(void);
void readscores(void);
void showhighscores(void);
void keyscores(unsigned char key, int x, int y);
void highscore(void)
{
int i, j;
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
FILE* fp;
/* Prompt for initials */
glColor3f(1.0, 1.0, 0.0); /* yellow */
text(0.08*width, 0.85*height, 0.1*height, "CONGRATULATIONS!");
glColor3f(1.0, 0.0, 0.0); /* red */
text(0.05*width, 0.7*height, 0.07*height, "You made it into the top 10!");
glColor3f(1.0, 1.0, 0.0); /* yellow */
text(0.2*width, 0.55*height, 0.07*height, "%02d remaining of %02d",
pegs, totalpegs);
glColor3f(0.0, 0.0, 1.0); /* blue */
text(0.13*width, 0.4*height, 0.07*height, "Please enter your initials:");
/* Display what's been entered */
glColor3f(color1, color2, color3);
for (i=0; i<numentered; i++)
text((0.4+i/10.0)*width, 0.2*height, 0.2*height, "%c", newinits[i]);
if (!written && numentered == 3)
{
#if 0
// printf("Saving to file scores.txt...\n");
#endif
for (i=0; i<10; i++)
if (scores[i]==-1 ||
(pegs<scores[i] || (pegs==scores[i] && totalpegs>totpegs[i])))
break;
for (j=9; j>i; j--)
{
if (scores[j-1]==-1 || scores[j-1]==0)
continue;
#if 0
// printf("compare : ");
// printf(" %s %02d %02d\n", inits[j], scores[j], totpegs[j]);
#endif
scores[j] = scores[j-1];
totpegs[j] = totpegs[j-1];
inits[j][0] = inits[j-1][0];
inits[j][1] = inits[j-1][1];
inits[j][2] = inits[j-1][2];
inits[j][3] = inits[j-1][3];
#if 0
// printf("with : ");
// printf(" %s %02d %02d\n", inits[j], scores[j], totpegs[j]);
#endif
}
#if 0
// printf("Storing in index %d\n", i);
#endif
lasthigh=i;
scores[i] = pegs;
totpegs[i] = totalpegs;
inits[i][0] = newinits[0];
inits[i][1] = newinits[1];
inits[i][2] = newinits[2];
inits[i][3] = 0;
/* get the new min */
for (j=9; j>0; j--)
if (scores[j]==-1 || scores[j]==0)
continue;
else
{
minscore = scores[j];
minpegs = totpegs[j];
break;
}
#if 0
// printf("New minscore %d, minpegs %d\n", minscore, minpegs);
#endif
fp = fopen("scores.txt", "w");
if (!fp)
{
printf("Could not open scores.txt, exiting.\n");
exit(1);
}
for (i=0; i<10; i++)
if (scores[i]!=-1 && scores[i]!=0)
fprintf(fp, "%02d %02d %s\n", scores[i], totpegs[i], inits[i]);
else
break;
written=1;
}
}
void readscores(void)
{
int i;
FILE* fp;
newinits[0] = 0;
newinits[1] = 0;
newinits[2] = 0;
/* Read in the current high scores */
fp = fopen("scores.txt", "r");
if (!fp)
{
printf("Could not open scores.txt, exiting.\n");
exit(1);
}
for (i=0; i<10; i++)
{
/* Pegs remaining */
if ((fscanf(fp, "%d", &(scores[i])))!=1)
{
scores[i] = -1;
break;
}
/* Total pegs */
if ((fscanf(fp, "%d", &(totpegs[i])))!=1)
{
totpegs[i] = -1;
break;
}
fscanf(fp, "%s", inits[i]);
#if 0
// printf("read %s\n", inits[i]);
#endif
}
if (i>0)
{
minscore = scores[i-1];
minpegs = totpegs[i-1];
}
if (i<10)
{
minscore=100;
minpegs=0;
}
#if 0
// printf("Minscore is %d, minpegs is %d\n", minscore, minpegs);
#endif
}
void showhighscores(void)
{
int i;
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
/* Display the current highs */
glColor3f(1.0, 1.0, 0.0); /* yellow */
text(0.15*width, 0.9*height, 0.07*height, "Initials Score Out of");
for (i=0; i<10; i++)
{
if (i>=1)
glColor3f(1.0, 0.0, 0.0); /* red */
else if (i>=5)
glColor3f(0.0, 0.0, 1.0); /* blue */
if (scores[i]>0)
{
if (i==lasthigh)
glColor3f(color1, color2, color3);
text(0.15*width, (8.0-0.65*i)/10.0*height, 0.05*height,
" %s", inits[i]);
text(0.48*width, (8.0-0.65*i)/10.0*height, 0.05*height,
"%02d", scores[i]);
text(0.75*width, (8.0-0.65*i)/10.0*height, 0.05*height,
"%02d", totpegs[i]);
}
}
glColor3f(color1, color2, color3);
text(0.15*width, 0.1*height, 0.07*height, "Click to continue...");
}
/* ARGSUSED1 */
void keyscores(unsigned char key, int x, int y)
{
#if 0
if (key == '\r') /*return*/ {
} else if (key == '\b') /*backspace*/ {
}
#endif
if (numentered>=3)
return;
newinits[numentered] = key;
numentered++;
#if 0
// printf("Read a %c\n", key);
#endif
glutPostRedisplay();
}
void idlescore(void)
{
static int hscolor=0;
switch(hscolor)
{
case 0:
color1=1.0;
color2=0.0;
color3=0.0;
hscolor++;
break;
case 1:
color1=0.5;
color2=0.5;
color3=0.0;
hscolor++;
break;
case 2:
color1=1.0;
color2=1.0;
color3=0.0;
hscolor++;
break;
case 3:
color1=0.0;
color2=1.0;
color3=0.0;
hscolor++;
break;
case 4:
color1=0.0;
color2=0.0;
color3=1.0;
hscolor++;
break;
case 5:
color1=1.0;
color2=0.0;
color3=1.0;
hscolor=0;
break;
}
if (curstate==HIGHSC)
highscore();
else if (curstate==VIEWSCORES)
showhighscores();
else
{
printf("Unknown state %d, exiting\n", curstate);
exit(1);
}
glutPostRedisplay();
}
|