home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
TOP
/
USR
/
SRC
/
yahtzee.t.Z
/
yahtzee.t
/
yz.c
< prev
Wrap
C/C++ Source or Header
|
1988-07-29
|
6KB
|
288 lines
/* yz.c
* main function and declaration of the important global
* data structures.
*/
#include <stdio.h>
#include <curses.h>
#include <signal.h>
#include "defs.h"
/* sleep period at end of game if no delay flag is given to yahtzee */
#define Non_Stop_Wait 10
#define TicToc fprintf(stderr, "."); fflush(stderr);
/* y and x coordinates for the display of the 5 dice */
int diey[Five_Dice] = {0, 0, 0, 0, 0}, diex[Five_Dice] = {0, 12, 24, 36, 48};
/* scoreboard stores scores for all players for each category, machine
* is a boolean to determine if the given player is a human or
* computer simulation */
int scoreboard[max_players][max_marks], machine[max_players];
int subtotals[max_players];
/* used to form category availability list for all players */
int available[13], avail_count;
WINDOW *screen;
/* users that don't like the default curses standout mode and don't
* want to change it themselves can avoid it my setting this boolean
* at startup */
int BadStandout = FALSE;
/* boolean for non-stop play, set at startup */
int non_stop = FALSE;
/* me */
#ifndef BSD
static char id[] = "@(#)yz.c 1.2 Yahtzee - Stacey Campbell";
#endif
#ifdef BSD
extern long random();
extern void srandom();
#else
extern long random();
extern void srandom();
#endif
extern byebye();
main(argc, argv)
int argc;
char *argv[];
{
int mark, player, player_count, round, i, dice[Five_Dice],
hold[Five_Dice], totals[max_players], best_player;
extern long time();
extern int old_dice[Five_Dice];
char ch = ' ', arg[50];
/* boot the seed for the random number generator */
#ifdef BSD
srandom((int) time(0));
#else
srandom(time((long *) 0));
#endif
TicToc
/* initialise curses */
initscr();
TicToc
/* create the game window */
screen = newwin(0, 0, 0, 0);
TicToc
/* process arguments */
for (i = 1; i < argc; ++i)
{
strcpy(arg, argv[i]);
/* flag for non-stop play */
if (strcmp("-N", arg) == 0)
non_stop = TRUE;
/* flag to avoid using standout mode of terminal */
else if (strcmp("-d", arg) == 0)
BadStandout = TRUE;
/* display score and exit */
else if (strcmp("-s", arg) == 0)
yahtzee_exit(dis_score((WINDOW *) 0));
/* put options on stdout then exit */
else if (strcmp("-h", arg) == 0)
{
option_message(argv[0]);
yahtzee_exit(0);
}
else
{
fprintf(stderr, "Unknown argument: %s\n", arg);
fprintf(stderr, "Try: %s -h\n", argv[0]);
sleep(4);
}
}
/* trap signals for clean exit */
_siginit();
signal(SIGINT, byebye);
signal(SIGQUIT, byebye);
/* signal(SIGTERM, byebye); */
TicToc
/* set crmode and noecho for the terminal */
crmode();
noecho();
/* determine player count and which players will be simulated */
player_count = player_info(machine, screen);
/* loop until user quits */
do
{
/* setup the play screen */
werase(screen);
dis_side();
for (i = 0; i < Five_Dice; ++i)
{
old_dice[i] = 0;
hold[i] = FALSE;
}
/* initialise dice to anything */
roll_dice(dice, hold);
dis_dice(dice, hold);
if (! BadStandout)
wstandout(screen);
/* initialise the scoreboard */
for (player = 0; player < player_count; ++player)
{
subtotals[player] = 0;
for (mark = 0; mark < max_marks; ++mark)
scoreboard[player][mark] = category_available;
mvwprintw(screen, 6, player * 10 + 27, "%d", player);
}
if (! BadStandout)
wstandend(screen);
mvwaddstr(screen, 2, 59, "-- y a h t z e e --");
wmove(screen, 3, 68);
wrefresh(screen);
/* start the game */
for (round = 0; round < 13; ++round)
for (player = 0; player < player_count; ++player)
{
/* player is human then form availability list */
if (! machine[player])
human_availability(player);
/* stage 1: roll dice then get move */
roll_dice(dice, hold);
get_move(player, dice, hold);
if (machine[player])
sleep(4);
/* stage 2: if all dice have not been held then do it again */
if (! all_held(hold))
{
roll_dice(dice, hold);
get_move(player, dice, hold);
if (machine[player])
sleep(4);
roll_dice(dice, hold);
}
/* display the dice */
if (BadStandout)
dis_dice(dice, hold);
for (i = 0; i < Five_Dice; ++i)
hold[i] = FALSE;
if (! BadStandout)
dis_dice(dice, hold);
/* get selection of best category */
if (machine[player])
mark = computer_select(player, dice,
available, avail_count);
else
mark = human_select(player, dice);
/* update the score board (not the HS board!) */
update_score(player, mark, dice);
/* have a little nap if a computer simulation */
if (machine[player])
sleep(4);
}
/* determine who won */
best_player = finish(totals, player_count);
wrefresh(screen);
/* attempt to put the best players score in the HS file */
update_high(best_player, totals[best_player]);
if (! non_stop)
{
/* determine if user wishes to continue */
mvwaddstr(screen, 5, 0,
"--(q)uit, (c)ontinue, (r)econfigure--");
#ifndef SYS5_3
wrefresh(screen);
#endif
do
{
ch = wgetch(screen);
switch (ch)
{
/* get help */
case '?' : help_out(1, screen);
break;
/* display the current HS file */
case 's' : dis_score(screen);
break;
/* redraw the current window */
case Form_Feed : redraw(screen);
break;
/* display the rule book */
case 'b' : rools(screen);
break;
/* reconfigure the players */
case 'r' : player_count =
player_info(machine,
screen);
break;
/* get a shell */
case '!' : shell(screen);
break;
case 'v' : version(screen);
break;
#if defined(SYS5) || defined(SYS5_3)
/* get a shell window */
case '$' : shwin(screen);
break;
case ' ' :
case 'q' :
case 'c' : break;
default : flash();
break;
#endif
}
} while (ch != ' ' && ch != 'q' && ch != 'c');
}
else
{
dis_score(screen);
sleep(Non_Stop_Wait);
}
} while (ch != 'q');
dis_score((WINDOW *) 0);
yahtzee_exit(0);
}
/* return true if all dice are held */
int all_held(hold)
int hold[Five_Dice];
{
register int i;
for (i = 0; i < Five_Dice; ++i)
if (! hold[i])
return(FALSE);
return(TRUE);
}