home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
games
/
volume1
/
yahtzee
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1987-05-08
|
2KB
|
184 lines
#include "types.h"
#include <sys/time.h>
#include <signal.h>
Game player[4];
char DELETE1, DELETE2;
main()
{
int np, clean();
seedrand();
initscr();
initkey(&DELETE1, &DELETE2);
signal(SIGINT, clean);
crmode();
noecho();
nonl();
np = getnames();
while(1) {
init(np);
if(playgame(np) == 0) break;
move(23,0);
clrtoeol();
addstr("Same Players? (Y/N) ");
refresh();
if(yes(23,21)) continue;
else np = getnames();
}
clean();
}
clean()
{
clear();
refresh();
endwin();
endkey();
exit(0);
}
yes(y,x)
int y,x;
{
char s[2];
while(1) {
getstring(s,1,0, y,x);
s[0] |= 040;
if(s[0] == 'y') return(1);
else if(s[0] == 'n') return(0);
else {
DisplayBeep(0);
mvaddstr(y,x+1," ");
move(y,x+1);
refresh();
}
}
}
init(n)
int n;
{
int i,j;
for(i=0; i<n; i++)
for(j=0; j<NS; j++) player[i].score[j] = -1;
player[i].ycount = 0;
}
playgame(n)
int n;
{
int which = 0, old = 0, i, j;
drawscreen(n);
while(gameon(n)) {
setname(old,which);
roll();
for(i=0; i<2; i++) {
if(choose()) break;
roll();
}
clrroll();
makechoice(which);
old = which;
if(++which == n) which=0;
}
return(winner(n));
}
gameon(n)
{
int i,j;
for(i=0; i<n; i++)
for(j=0; j<NS; j++) {
if(j == 6) continue;
if(player[i].score[j] == -1) return(1);
}
return(0);
}
getnames()
{
int n,i;
char s[7];
clear();
mvaddstr(0,0,"How many players: ");
refresh();
while(1) {
move(0,18);
clrtoeol();
refresh();
getstring(s,1,1,0,18);
if((n=atoi(s)) >= 1 && n <= 4) break;
DisplayBeep(0);
}
for(i=0; i<n; i++) {
mvaddstr(i+1,0,"Enter name: ");
refresh();
move(i+1,12);
clrtoeol();
getstring(s, 6, 0, i+1, 12);
strcpy(player[i].name, s);
}
return(n);
}
getstring(s,n,flag,y,x)
char *s;
int n,flag,y,x;
{
int c,i=0;
while(1) {
c = key();
if(c < 0 && c!=LF_ARROW) DisplayBeep(0);
else if(c == 8 || c == LF_ARROW) {
if(i == 0) DisplayBeep(0);
else {
s[--i] = 0;
addch(8);
addch(' ');
addch(8);
refresh();
x--;
}
} else if(c == '\r') {
s[i]=0;
return;
} else {
if(i < n) {
if(flag) {
if(c<'0' || c>'9') {
DisplayBeep(0);
continue;
}
}
s[i++] = c;
x++;
addch(c);
refresh();
} else DisplayBeep(0);
}
}
}
seedrand()
{
struct timeval tp;
struct timezone tpz;
gettimeofday(&tp,&tpz);
srandom((int)tp.tv_sec);
}
get_rand()
{
long random();
return( (int) random() % 6 + 1);
}