1
/* $OpenBSD: scores.c,v 1.11 2006/04/20 03:25:36 ray Exp $ */
2
/* $NetBSD: scores.c,v 1.2 1995/04/22 07:42:38 cgd Exp $ */
5
* Copyright (c) 1992, 1993
6
* The Regents of the University of California. All rights reserved.
8
* This code is derived from software contributed to Berkeley by
9
* Chris Torek and Darren F. Provine.
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
14
* 1. Redistributions of source code must retain the above copyright
15
* notice, this list of conditions and the following disclaimer.
16
* 2. Redistributions in binary form must reproduce the above copyright
17
* notice, this list of conditions and the following disclaimer in the
18
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of the University nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35
* @(#)scores.c 8.1 (Berkeley) 5/31/93
38
/** @addtogroup tetris
45
* Score code for Tetris, by Darren Provine (kilroy@gboro.glassboro.edu)
46
* modified 22 January 1992, to limit the number of entries any one
49
* Major whacks since then.
55
#include <io/console.h>
56
#include <io/keycode.h>
68
* Within this code, we can hang onto one extra "high score", leaving
69
* room for our current score (whether or not it is high).
71
* We also sometimes keep tabs on the "highest" score on each level.
72
* As long as the scores are kept sorted, this is simply the first one at
76
#define NUMSPOTS (MAXHISCORES + 1)
77
#define NLEVELS (MAXLEVEL + 1)
79
static struct highscore scores[NUMSPOTS];
81
/** Copy from hiscore table score with index src to dest
84
static void copyhiscore(int dest, int src)
86
str_cpy(scores[dest].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
88
scores[dest].hs_score = scores[src].hs_score;
89
scores[dest].hs_level = scores[src].hs_level;
92
void showscores(int firstgame)
98
printf("\tRank \tLevel \tName\t points\n");
99
printf("\t========================================================\n");
101
for (i = 0; i < NUMSPOTS - 1; i++)
102
printf("\t%6d %6d %-16s %20d\n",
103
i + 1, scores[i].hs_level, scores[i].hs_name, scores[i].hs_score);
106
printf("\t========================================================\n");
107
printf("\t Last %6d %-16s %20d\n",
108
scores[NUMSPOTS - 1].hs_level, scores[NUMSPOTS - 1].hs_name, scores[NUMSPOTS - 1].hs_score);
111
printf("\n\n\n\n\tPress any key to return to main menu.");
115
void insertscore(int score, int level)
124
puts("Insert your name: ");
125
str_cpy(scores[NUMSPOTS - 1].hs_name, STR_BOUNDS(MAXLOGNAME) + 1,
131
printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME-i,
132
"........................................");
136
if (!console_get_event(fphone(stdin), &ev))
139
if (ev.type == KEY_RELEASE)
142
if (ev.key == KC_ENTER || ev.key == KC_NENTER)
145
if (ev.key == KC_BACKSPACE) {
153
uc = str_decode(scores[NUMSPOTS - 1].hs_name,
154
&otmp, STR_BOUNDS(MAXLOGNAME) + 1);
159
scores[NUMSPOTS - 1].hs_name[off] = '\0';
161
} else if (ev.c != '\0') {
162
if (i < (MAXLOGNAME - 1)) {
163
if (chr_encode(ev.c, scores[NUMSPOTS - 1].hs_name,
164
&off, STR_BOUNDS(MAXLOGNAME) + 1) == EOK) {
167
scores[NUMSPOTS - 1].hs_name[off] = '\0';
172
printf("%s%.*s", scores[NUMSPOTS - 1].hs_name, MAXLOGNAME - i,
173
"........................................");
176
scores[NUMSPOTS - 1].hs_score = score;
177
scores[NUMSPOTS - 1].hs_level = level;
180
while ((i > 0) && (scores[i - 1].hs_score < score))
183
for (j = NUMSPOTS - 2; j > i; j--)
186
copyhiscore(i, NUMSPOTS - 1);
189
void initscores(void)
192
for (i = 0; i < NUMSPOTS; i++) {
193
str_cpy(scores[i].hs_name, STR_BOUNDS(MAXLOGNAME) + 1, "HelenOS Team");
194
scores[i].hs_score = (NUMSPOTS - i) * 200;
195
scores[i].hs_level = (i + 1 > MAXLEVEL ? MAXLEVEL : i + 1);
205
f = fopen("/data/tetris.sco", "rb");
209
cnt = fread(scores, sizeof(struct highscore), NUMSPOTS, f);
212
if (cnt != NUMSPOTS || rc != 0)
218
void savescores(void)
224
f = fopen("/data/tetris.sco", "wb");
225
cnt = fwrite(scores, sizeof(struct highscore), NUMSPOTS, f);
228
if (cnt != NUMSPOTS || rc != 0)
229
printf("Error saving score table\n");