home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume14
/
scrabble2
/
part14
/
misc.c
next >
Wrap
C/C++ Source or Header
|
1993-01-27
|
5KB
|
228 lines
/*
*
* misc.c -- miscellaneous routines
*
*/
#include "scrab.h"
#include "globals.h"
char spaces[81] =
" \
";
read_words() {
FILE *fp;
char inp[16], tinp[16];
int i, tlen, rlen;
long pt, wr;
fp = fopen( DICT_FILE, "r" );
if( fp == NULL ) {
move( 4, 0 );
printw( "Error: could not find dictionary file '%s'\n", DICT_FILE );
exit_window();
exit( 1 );
}
printw( "Reading dictionary...\n" );
refresh();
pt = 0;
wr = 0;
tlen = 2;
wlen[tlen] = wr;
tinp[0] = '\0';
while( fscanf( fp, "%s", inp ) != EOF ) {
rlen = strlen( inp );
if( rlen < 2 || rlen > 15 ) {
move( 4, 0 );
printw( "Error in dictionary file at word %s: words must be between 2 and 15\n", inp );
printw( "characters long.\n" );
exit_window();
exit( 1 );
}
if( rlen < strlen( tinp ) ||
( strcmp( tinp, inp ) > 0 && strlen( tinp ) == rlen ) ) {
move( 4, 0 );
printw( "Error in dictionary file at the words %s and %s.\n",
tinp, inp );
printw( "The dictionary must be sorted from shortest words to longest, and\n" );
printw( "alphabetically within each list of words of the same length." );
exit_window();
exit( 1 );
}
for( i = 0; i < rlen; i++ ) {
if( inp[i] >= 'a' && inp[i] <= 'z' ) inp[i] = inp[i] - 'a' + 'A';
else if( inp[i] < 'A' || inp[i] > 'Z' ) {
move( 4, 0 );
printw( "Error in dictionary file at word %s: words must contain alphabetic characters\n", inp );
printw( "only.\n" );
exit_window();
exit( 1 );
}
}
if( rlen > tlen ) {
tlen = rlen;
wlen[tlen] = wr;
}
strcpy( &words[pt], inp );
wptr[wr++] = pt;
pt += rlen + 1;
strcpy( tinp, inp );
}
dict_size = pt;
wlen[16] = wr;
fclose( fp );
}
clear_rect( y1, x1, y2, x2 )
int y1, x1, y2, x2;
{
int i;
for( i = y1; i <= y2; i++ ) {
move( i, x1 );
printw( "%s", &spaces[79 - ( x2 - x1 )] );
}
move( y1, x1 );
}
clear_prompt() {
clear_rect( 19, 0, 23, 78 );
move( 19, 0 );
}
clear_turn() {
clear_rect( 18, 0, 18, 79 );
move( 18, 0 );
}
press_return() {
char c;
move( 23, 0 );
printw( "Press <RETURN> -> " );
refresh();
do {
c = getch();
if( c == 'q' || c == 'Q' ) abort = 1;
} while( c != '\n' );
}
print_board() {
int i, j;
for( i = 1; i < 16; i++ ) {
move( BOARD_UY + i - 1, BOARD_LX );
for( j = 1; j < 16; j++ ) {
if( board[i][j] >= 'A' ) standout();
if( ( i == b1y && j == b1x ) || ( i == b2y && j == b2x ) )
addch( board[i][j] - 'A' + 'a' );
else if( board[i][j] == CH_DW ) addch( 'o' );
else addch( board[i][j] );
if( board[i][j] >= 'A' ) standend();
}
}
refresh();
}
print_tiles() {
int i, j;
clear_rect( TILE_RACKS_Y, TILE_RACKS_X,
TILE_RACKS_Y + 7, TILE_RACKS_X + 24 );
for( i = 0; i < players; i++ ) {
move( TILE_RACKS_Y + i * 3, TILE_RACKS_X - 4 );
if( plr_skills[i] != 0 ) printw( "(%d)", plr_skills[i] );
move( TILE_RACKS_Y + i * 3, TILE_RACKS_X );
if( i == human_player )
printw( "Your tiles:" );
else
printw( "%s tiles:", you[i] );
for( j = 0; j < 7; j++ ) {
addch( ' ' );
if( plr_tiles[i][j] > 0 ) {
#ifndef PRINT_CPU_TILES
if( i == human_player || game_done == players )
#endif
addch( plr_tiles[i][j] );
#ifndef PRINT_CPU_TILES
else addch( '-' );
#endif
} else addch( ' ' );
}
move( TILE_RACKS_Y + i * 3 + 1, TILE_RACKS_X + 5 );
printw( "Score: %3d", plr_scores[i] );
}
move( TILE_RACKS_Y + 12, TILE_RACKS_X - 2 );
i = 0;
if( b1x != 0 ) i++;
if( b2x != 0 ) i++;
printw( "Blank tiles placed so far: %d", i );
if( b1x != 0 ) {
move( TILE_RACKS_Y + 13, TILE_RACKS_X + 3 );
printw( "Row %2d, Column %2d (%c)", b1y, b1x, board[b1y][b1x] );
}
if( b2x != 0 ) {
move( TILE_RACKS_Y + 14, TILE_RACKS_X + 3 );
printw( "Row %2d, Column %2d (%c)", b2y, b2x, board[b2y][b2x] );
}
move( TILE_RACKS_Y + human_player * 3, TILE_RACKS_X - 1 );
refresh();
}
char draw_tile() {
int i, tleft;
int tile;
tleft = 0;
for( i = 0; i < 27; i++ )
tleft += tiles_left[i];
if( tleft == 0 ) return( 0 );
tile = get_random() % tleft;
for( i = 0;; i++ ) {
if( tile - tiles_left[i] >= 0 )
tile -= tiles_left[i];
else
break;
}
tiles_left[i]--;
print_tiles_left( tleft - 1 );
return( i == 26 ? CH_BL : i + 'A' );
}
print_tiles_left( t )
int t;
{
move( TILES_LEFT_Y, TILES_LEFT_X );
printw( "Tiles left to draw: %3d", t );
}
char get_key() {
char c;
c = getch();
if( c >= 'a' && c <= 'z' ) c = c - 'a' + 'A';
/* if( c == '\033' )
if( getch() == '[' ) {
c = getch();
if( c == 'A' ) c = 'K';
else if( c == 'B' ) c = 'J';
else if( c == 'C' ) c = 'L';
else if( c == 'D' ) c = 'H';
else c = '\0';
} else c = '\0'; */
return( c );
}
seed_random() {
struct timeval tp;
gettimeofday( &tp, 0 );
srandom( tp.tv_sec % 65536 - 32768 );
}
long get_random() {
return( (long) random() );
}