home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume5
/
yahtzee
/
part01
/
high.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-07-26
|
6KB
|
266 lines
/* high.c
* contains routines for the maintenance of the high score file.
*/
#include <stdio.h>
#include <curses.h>
#include "defs.h"
#define High_Length (max_hs_count + 5)
#define High_Width 29
#define Start_Line 3
#define Display_High_Time 5
extern WINDOW *screen;
/* dis_score() displays the current high score file in a
* window, 'Back_Window' is the window behind the high score
* display window, 'Back_Window' must be restored once the
* user has finished with the high score window */
dis_score(Back_Window)
WINDOW *Back_Window;
{
FILE *fp;
char Name[player_name_len], ch;
int score, i, tmp_y, tmp_x;
extern int non_stop;
WINDOW *High_Window;
/* Back_Window will be NULL in the case where yahtzee is exiting
* and displaying the high score file as its last action */
if (Back_Window != (WINDOW *) 0)
{
getyx(Back_Window, tmp_y, tmp_x);
wmove(Back_Window, 0, 0);
wrefresh(Back_Window);
}
/* create the high score window */
High_Window = newwin(High_Length, High_Width, LINES - High_Length - 1,
3);
werase(High_Window);
BoxUp(High_Window, High_Length, High_Width);
wmove(High_Window, 1, 1);
/* open high score file (note HSFILE is defined at compile time, see
* the makefile) */
fp = fopen(HSFILE, "r");
if (fp == NULL)
waddstr(High_Window, "Cannot access score file.");
else
{
wprintw(High_Window, "Top %d Yahtzee High Scores",
max_hs_count);
i = Start_Line;
/* output all high scores */
while (fscanf(fp, "%d %s\n", &score, Name) != EOF)
{
wmove(High_Window, i, 1);
wprintw(High_Window, "%20s %5d", Name, score);
++i;
}
fclose(fp);
}
touchwin(High_Window);
wrefresh(High_Window);
if (Back_Window != (WINDOW *) 0)
{
/* if the user specified non-stop play then don't get input */
if (non_stop)
{
wmove(High_Window, 0, 0);
wrefresh(High_Window);
sleep(Display_High_Time);
}
else
{
mvwaddstr(High_Window, High_Length - 2, 1,
"--(q)uit--");
#ifndef SYS5_3
wrefresh(High_Window);
#endif
do
{
ch = wgetch(High_Window);
switch (ch)
{
case Form_Feed : redraw(High_Window);
break;
case '?' : help_out(7, High_Window);
break;
case 'b' : rools(High_Window);
break;
case '!' : shell(High_Window);
break;
case 'v' : version(High_Window);
break;
#if defined(SYS5) || defined(SYS5_3)
case '$' : shwin(High_Window);
break;
case 'q' :
case ' ' : break;
default : flash();
break;
#endif
}
} while (ch != 'q' && ch != ' ');
}
delwin(High_Window);
/* restore previous window */
touchwin(Back_Window);
wmove(Back_Window, tmp_y, tmp_x);
wrefresh(Back_Window);
}
}
extern int machine[max_players];
extern char *getenv(), *strcpy(), *strcat();
/* update high score file */
update_high(player_number, score)
int player_number, score;
{
FILE *hsf;
char player[max_hs_count + 1][player_name_len],
tmp_player[player_name_len];
int player_score[max_hs_count + 1], own_score,
lowest_score = 0;
register int hs_count = 0, existing_entries = 0, i, j;
char message[150];
void file_mess();
/* read contents of high score file (also check to see if it exists) */
hsf = fopen(HSFILE, "r");
if (hsf == NULL)
{
sprintf(message, "trying to create: %s", HSFILE);
if (strlen(HSFILE) > 79)
strcpy(message, "Trying to create high score file.");
file_mess(screen, message);
}
else
{
while (fscanf(hsf, "%d %s\n", &player_score[hs_count],
player[hs_count]) != EOF)
++hs_count;
fclose(hsf);
}
/* there is probably a better way of getting a player name,
* but we don't want to pester the user for too much info */
#ifdef BSD
strcpy(tmp_player, getenv("USER"));
#else
strcpy(tmp_player, getenv("LOGNAME"));
#endif
/* make sure that the heuristics get credit for winning a game */
if (machine[player_number])
strcat(tmp_player, "-machine");
/* determine how many entries the user has in the high score file,
* and if the user has an entry what the users lowest recorded score is */
for (i = 0; i < hs_count; ++i)
if (strcmp(player[i], tmp_player) == 0)
{
own_score = i;
++existing_entries;
lowest_score = player_score[i];
}
if (existing_entries >= max_entries)
{
if (lowest_score < score)
{
/* if the user already has the max. number of entries in the HS file
* but the new score is better than a previous score then delete the
* users worst high score and insert the new score */
--hs_count;
for (j = own_score; j < hs_count; ++j)
{
player_score[j] = player_score[j + 1];
strcpy(player[j], player[j + 1]);
}
insert_score(player_score, player, hs_count,
tmp_player, score);
}
}
else
/* attempt to insert the score */
insert_score(player_score, player, hs_count, tmp_player, score);
}
insert_score(player_score, player, hs_count, player_name, score)
int player_score[max_hs_count + 1], hs_count, score;
char *player_name, player[max_hs_count + 1][player_name_len];
{
int j, position = 0;
FILE *hsf;
void file_mess();
/* determine the users ranking in the HS file */
while (position < hs_count && player_score[position] > score)
++position;
/* if the user has the a ranking lower than the number of possible
* entries then don't update file */
if (position >= max_hs_count)
return(0);
/* make room for the new score */
for (j = hs_count; j > position; --j)
{
strcpy(player[j], player[j - 1]);
player_score[j] = player_score[j - 1];
}
/* insert the new name and score */
player_score[position] = score;
strcpy(player[position], player_name);
++hs_count;
/* truncate HS file if it exceeds the maximum allowable entries */
if (hs_count > max_hs_count)
hs_count = max_hs_count;
/* write the new HS file */
if ((hsf = fopen(HSFILE, "w")) == NULL)
{
file_mess(screen, "Cannot write to high score file.");
return;
}
for (j = 0; j < hs_count; ++j)
fprintf(hsf, "%d %s\n", player_score[j], player[j]);
fclose(hsf);
}
static void file_mess(back_window, message)
WINDOW *back_window;
char *message;
{
WINDOW *mess_win;
int mess_len = strlen(message) + 3;
mess_win = newwin(3, mess_len, 11, (COLS - mess_len) / 2);
BoxUp(mess_win, 3, mess_len);
mvwaddstr(mess_win, 1, 1, message);
touchwin(mess_win);
wrefresh(mess_win);
sleep(3);
touchwin(back_window);
wrefresh(back_window);
delwin(mess_win);
}