1997-10-11 06:44:30 +04:00
|
|
|
/* $NetBSD: crib.c,v 1.9 1997/10/11 02:44:30 lukem Exp $ */
|
1995-03-21 18:03:38 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 16:26:34 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-10 16:26:34 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-21 18:03:38 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
1997-10-11 06:44:30 +04:00
|
|
|
__RCSID("$NetBSD: crib.c,v 1.9 1997/10/11 02:44:30 lukem Exp $");
|
1995-03-21 18:03:38 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-03-21 18:03:38 +03:00
|
|
|
#include <curses.h>
|
1997-10-11 06:44:30 +04:00
|
|
|
#include <err.h>
|
1995-03-21 18:03:38 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "deck.h"
|
|
|
|
#include "cribbage.h"
|
|
|
|
#include "cribcur.h"
|
|
|
|
#include "pathnames.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-10 16:26:34 +04:00
|
|
|
int main __P((int, char *[]));
|
|
|
|
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
1995-03-21 18:03:38 +03:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN playing;
|
|
|
|
FILE *f;
|
1993-03-21 12:45:37 +03:00
|
|
|
int ch;
|
|
|
|
|
1997-10-10 16:26:34 +04:00
|
|
|
while ((ch = getopt(argc, argv, "eqr")) != -1)
|
1995-03-21 18:03:38 +03:00
|
|
|
switch (ch) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'e':
|
|
|
|
explain = TRUE;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = TRUE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = TRUE;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
(void) fprintf(stderr, "usage: cribbage [-eqr]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
initscr();
|
1995-03-21 18:03:38 +03:00
|
|
|
(void)signal(SIGINT, rint);
|
1993-03-21 12:45:37 +03:00
|
|
|
crmode();
|
|
|
|
noecho();
|
1995-03-21 18:03:38 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0);
|
|
|
|
Tablewin = subwin(stdscr, TABLE_Y, TABLE_X, 0, PLAY_X);
|
|
|
|
Compwin = subwin(stdscr, COMP_Y, COMP_X, 0, TABLE_X + PLAY_X);
|
|
|
|
Msgwin = subwin(stdscr, MSG_Y, MSG_X, Y_MSG_START, SCORE_X + 1);
|
|
|
|
leaveok(Playwin, TRUE);
|
|
|
|
leaveok(Tablewin, TRUE);
|
|
|
|
leaveok(Compwin, TRUE);
|
|
|
|
clearok(stdscr, FALSE);
|
|
|
|
|
|
|
|
if (!quiet) {
|
1995-03-21 18:03:38 +03:00
|
|
|
msg("Do you need instructions for cribbage? ");
|
|
|
|
if (getuchar() == 'Y') {
|
|
|
|
endwin();
|
|
|
|
clear();
|
|
|
|
mvcur(0, COLS - 1, LINES - 1, 0);
|
|
|
|
fflush(stdout);
|
|
|
|
instructions();
|
|
|
|
crmode();
|
|
|
|
noecho();
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
msg("For cribbage rules, use \"man cribbage\"");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
playing = TRUE;
|
|
|
|
do {
|
1995-03-21 18:03:38 +03:00
|
|
|
wclrtobot(Msgwin);
|
|
|
|
msg(quiet ? "L or S? " : "Long (to 121) or Short (to 61)? ");
|
|
|
|
if (glimit == SGAME)
|
|
|
|
glimit = (getuchar() == 'L' ? LGAME : SGAME);
|
|
|
|
else
|
|
|
|
glimit = (getuchar() == 'S' ? SGAME : LGAME);
|
|
|
|
game();
|
|
|
|
msg("Another game? ");
|
|
|
|
playing = (getuchar() == 'Y');
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (playing);
|
|
|
|
|
1997-07-10 10:47:29 +04:00
|
|
|
if ((f = fopen(_PATH_LOG, "a")) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(f, "%s: won %5.5d, lost %5.5d\n",
|
1995-03-21 18:03:38 +03:00
|
|
|
getlogin(), cgames, pgames);
|
|
|
|
(void) fclose(f);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
bye();
|
1997-10-11 06:44:30 +04:00
|
|
|
if (!f)
|
|
|
|
errx(1, "can't open %s", _PATH_LOG);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* makeboard:
|
|
|
|
* Print out the initial board on the screen
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
makeboard()
|
|
|
|
{
|
1995-03-21 18:03:38 +03:00
|
|
|
mvaddstr(SCORE_Y + 0, SCORE_X,
|
|
|
|
"+---------------------------------------+");
|
|
|
|
mvaddstr(SCORE_Y + 1, SCORE_X,
|
|
|
|
"| Score: 0 YOU |");
|
|
|
|
mvaddstr(SCORE_Y + 2, SCORE_X,
|
|
|
|
"| *.....:.....:.....:.....:.....:..... |");
|
|
|
|
mvaddstr(SCORE_Y + 3, SCORE_X,
|
|
|
|
"| *.....:.....:.....:.....:.....:..... |");
|
|
|
|
mvaddstr(SCORE_Y + 4, SCORE_X,
|
|
|
|
"| |");
|
|
|
|
mvaddstr(SCORE_Y + 5, SCORE_X,
|
|
|
|
"| *.....:.....:.....:.....:.....:..... |");
|
|
|
|
mvaddstr(SCORE_Y + 6, SCORE_X,
|
|
|
|
"| *.....:.....:.....:.....:.....:..... |");
|
|
|
|
mvaddstr(SCORE_Y + 7, SCORE_X,
|
|
|
|
"| Score: 0 ME |");
|
|
|
|
mvaddstr(SCORE_Y + 8, SCORE_X,
|
|
|
|
"+---------------------------------------+");
|
|
|
|
gamescore();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gamescore:
|
|
|
|
* Print out the current game score
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
gamescore()
|
|
|
|
{
|
1995-03-21 18:03:38 +03:00
|
|
|
extern int Lastscore[];
|
|
|
|
|
|
|
|
if (pgames || cgames) {
|
|
|
|
mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames);
|
|
|
|
mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames);
|
|
|
|
}
|
|
|
|
Lastscore[0] = -1;
|
|
|
|
Lastscore[1] = -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* game:
|
|
|
|
* Play one game up to glimit points. Actually, we only ASK the
|
|
|
|
* player what card to turn. We do a random one, anyway.
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
game()
|
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
int i, j;
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN flag;
|
|
|
|
BOOLEAN compcrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-10 16:26:34 +04:00
|
|
|
compcrib = FALSE;
|
1993-03-21 12:45:37 +03:00
|
|
|
makedeck(deck);
|
|
|
|
shuffle(deck);
|
|
|
|
if (gamecount == 0) {
|
1995-03-21 18:03:38 +03:00
|
|
|
flag = TRUE;
|
|
|
|
do {
|
|
|
|
if (!rflag) { /* player cuts deck */
|
|
|
|
msg(quiet ? "Cut for crib? " :
|
|
|
|
"Cut to see whose crib it is -- low card wins? ");
|
|
|
|
getline();
|
|
|
|
}
|
|
|
|
i = (rand() >> 4) % CARDS; /* random cut */
|
|
|
|
do { /* comp cuts deck */
|
|
|
|
j = (rand() >> 4) % CARDS;
|
|
|
|
} while (j == i);
|
|
|
|
addmsg(quiet ? "You cut " : "You cut the ");
|
|
|
|
msgcard(deck[i], FALSE);
|
|
|
|
endmsg();
|
|
|
|
addmsg(quiet ? "I cut " : "I cut the ");
|
|
|
|
msgcard(deck[j], FALSE);
|
|
|
|
endmsg();
|
|
|
|
flag = (deck[i].rank == deck[j].rank);
|
|
|
|
if (flag) {
|
|
|
|
msg(quiet ? "We tied..." :
|
|
|
|
"We tied and have to try again...");
|
|
|
|
shuffle(deck);
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
compcrib = (deck[i].rank > deck[j].rank);
|
|
|
|
} while (flag);
|
1997-07-09 10:25:45 +04:00
|
|
|
do_wait();
|
1995-03-21 18:03:38 +03:00
|
|
|
clear();
|
|
|
|
makeboard();
|
|
|
|
refresh();
|
|
|
|
} else {
|
1997-07-09 10:25:45 +04:00
|
|
|
makeboard();
|
|
|
|
refresh();
|
1995-03-21 18:03:38 +03:00
|
|
|
werase(Tablewin);
|
|
|
|
wrefresh(Tablewin);
|
|
|
|
werase(Compwin);
|
|
|
|
wrefresh(Compwin);
|
|
|
|
msg("Loser (%s) gets first crib", (iwon ? "you" : "me"));
|
|
|
|
compcrib = !iwon;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pscore = cscore = 0;
|
|
|
|
flag = TRUE;
|
|
|
|
do {
|
1995-03-21 18:03:38 +03:00
|
|
|
shuffle(deck);
|
|
|
|
flag = !playhand(compcrib);
|
|
|
|
compcrib = !compcrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (flag);
|
|
|
|
++gamecount;
|
|
|
|
if (cscore < pscore) {
|
1995-03-21 18:03:38 +03:00
|
|
|
if (glimit - cscore > 60) {
|
|
|
|
msg("YOU DOUBLE SKUNKED ME!");
|
|
|
|
pgames += 4;
|
|
|
|
} else
|
|
|
|
if (glimit - cscore > 30) {
|
|
|
|
msg("YOU SKUNKED ME!");
|
|
|
|
pgames += 2;
|
|
|
|
} else {
|
|
|
|
msg("YOU WON!");
|
|
|
|
++pgames;
|
|
|
|
}
|
|
|
|
iwon = FALSE;
|
|
|
|
} else {
|
|
|
|
if (glimit - pscore > 60) {
|
|
|
|
msg("I DOUBLE SKUNKED YOU!");
|
|
|
|
cgames += 4;
|
|
|
|
} else
|
|
|
|
if (glimit - pscore > 30) {
|
|
|
|
msg("I SKUNKED YOU!");
|
|
|
|
cgames += 2;
|
|
|
|
} else {
|
|
|
|
msg("I WON!");
|
|
|
|
++cgames;
|
|
|
|
}
|
|
|
|
iwon = TRUE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
gamescore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* playhand:
|
|
|
|
* Do up one hand of the game
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
playhand(mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
int deckpos;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
werase(Compwin);
|
1997-07-09 10:25:45 +04:00
|
|
|
wrefresh(Compwin);
|
|
|
|
werase(Tablewin);
|
|
|
|
wrefresh(Tablewin);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
knownum = 0;
|
|
|
|
deckpos = deal(mycrib);
|
|
|
|
sorthand(chand, FULLHAND);
|
|
|
|
sorthand(phand, FULLHAND);
|
|
|
|
makeknown(chand, FULLHAND);
|
|
|
|
prhand(phand, FULLHAND, Playwin, FALSE);
|
|
|
|
discard(mycrib);
|
|
|
|
if (cut(mycrib, deckpos))
|
1995-03-21 18:03:38 +03:00
|
|
|
return TRUE;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (peg(mycrib))
|
1995-03-21 18:03:38 +03:00
|
|
|
return TRUE;
|
1993-03-21 12:45:37 +03:00
|
|
|
werase(Tablewin);
|
|
|
|
wrefresh(Tablewin);
|
|
|
|
if (score(mycrib))
|
1995-03-21 18:03:38 +03:00
|
|
|
return TRUE;
|
1993-03-21 12:45:37 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deal cards to both players from deck
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
|
|
|
deal(mycrib)
|
|
|
|
BOOLEAN mycrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
int i, j;
|
1995-03-21 18:03:38 +03:00
|
|
|
|
|
|
|
for (i = j = 0; i < FULLHAND; i++) {
|
|
|
|
if (mycrib) {
|
|
|
|
phand[i] = deck[j++];
|
|
|
|
chand[i] = deck[j++];
|
|
|
|
} else {
|
|
|
|
chand[i] = deck[j++];
|
|
|
|
phand[i] = deck[j++];
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-21 18:03:38 +03:00
|
|
|
return (j);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* discard:
|
|
|
|
* Handle players discarding into the crib...
|
|
|
|
* Note: we call cdiscard() after prining first message so player doesn't wait
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
discard(mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
char *prompt;
|
1995-03-21 18:03:38 +03:00
|
|
|
CARD crd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
prcrib(mycrib, TRUE);
|
|
|
|
prompt = (quiet ? "Discard --> " : "Discard a card --> ");
|
1995-03-21 18:03:38 +03:00
|
|
|
cdiscard(mycrib); /* puts best discard at end */
|
1993-03-21 12:45:37 +03:00
|
|
|
crd = phand[infrom(phand, FULLHAND, prompt)];
|
|
|
|
cremove(crd, phand, FULLHAND);
|
|
|
|
prhand(phand, FULLHAND, Playwin, FALSE);
|
|
|
|
crib[0] = crd;
|
1995-03-21 18:03:38 +03:00
|
|
|
|
|
|
|
/* Next four lines same as last four except for cdiscard(). */
|
1993-03-21 12:45:37 +03:00
|
|
|
crd = phand[infrom(phand, FULLHAND - 1, prompt)];
|
|
|
|
cremove(crd, phand, FULLHAND - 1);
|
|
|
|
prhand(phand, FULLHAND, Playwin, FALSE);
|
|
|
|
crib[1] = crd;
|
|
|
|
crib[2] = chand[4];
|
|
|
|
crib[3] = chand[5];
|
|
|
|
chand[4].rank = chand[4].suit = chand[5].rank = chand[5].suit = EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cut:
|
|
|
|
* Cut the deck and set turnover. Actually, we only ASK the
|
|
|
|
* player what card to turn. We do a random one, anyway.
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
cut(mycrib, pos)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib;
|
|
|
|
int pos;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
int i;
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN win;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-21 18:03:38 +03:00
|
|
|
win = FALSE;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (mycrib) {
|
1995-03-21 18:03:38 +03:00
|
|
|
if (!rflag) { /* random cut */
|
|
|
|
msg(quiet ? "Cut the deck? " :
|
|
|
|
"How many cards down do you wish to cut the deck? ");
|
|
|
|
getline();
|
|
|
|
}
|
|
|
|
i = (rand() >> 4) % (CARDS - pos);
|
|
|
|
turnover = deck[i + pos];
|
|
|
|
addmsg(quiet ? "You cut " : "You cut the ");
|
|
|
|
msgcard(turnover, FALSE);
|
|
|
|
endmsg();
|
|
|
|
if (turnover.rank == JACK) {
|
|
|
|
msg("I get two for his heels");
|
|
|
|
win = chkscr(&cscore, 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = (rand() >> 4) % (CARDS - pos) + pos;
|
|
|
|
turnover = deck[i];
|
|
|
|
addmsg(quiet ? "I cut " : "I cut the ");
|
|
|
|
msgcard(turnover, FALSE);
|
|
|
|
endmsg();
|
|
|
|
if (turnover.rank == JACK) {
|
|
|
|
msg("You get two for his heels");
|
|
|
|
win = chkscr(&pscore, 2);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
makeknown(&turnover, 1);
|
|
|
|
prcrib(mycrib, FALSE);
|
1995-03-21 18:03:38 +03:00
|
|
|
return (win);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prcrib:
|
|
|
|
* Print out the turnover card with crib indicator
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
prcrib(mycrib, blank)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib, blank;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 16:26:34 +04:00
|
|
|
int y, cardx;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
cardx = CRIB_X;
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1995-03-21 18:03:38 +03:00
|
|
|
cardx = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
mvaddstr(CRIB_Y, cardx + 1, "CRIB");
|
|
|
|
prcard(stdscr, CRIB_Y + 1, cardx, turnover, blank);
|
|
|
|
|
|
|
|
if (mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
cardx = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1995-03-21 18:03:38 +03:00
|
|
|
cardx = CRIB_X;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (y = CRIB_Y; y <= CRIB_Y + 5; y++)
|
1995-03-21 18:03:38 +03:00
|
|
|
mvaddstr(y, cardx, " ");
|
1997-07-09 10:25:45 +04:00
|
|
|
refresh();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* peg:
|
|
|
|
* Handle all the pegging...
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
static CARD Table[14];
|
|
|
|
static int Tcnt;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
peg(mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-03-21 18:03:38 +03:00
|
|
|
static CARD ch[CINHAND], ph[CINHAND];
|
1997-10-10 16:26:34 +04:00
|
|
|
int i, j, k;
|
|
|
|
int l;
|
|
|
|
int cnum, pnum, sum;
|
|
|
|
BOOLEAN myturn, mego, ugo, last, played;
|
1995-03-21 18:03:38 +03:00
|
|
|
CARD crd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-10 16:26:34 +04:00
|
|
|
played = FALSE;
|
1993-03-21 12:45:37 +03:00
|
|
|
cnum = pnum = CINHAND;
|
1995-03-21 18:03:38 +03:00
|
|
|
for (i = 0; i < CINHAND; i++) { /* make copies of hands */
|
|
|
|
ch[i] = chand[i];
|
|
|
|
ph[i] = phand[i];
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-21 18:03:38 +03:00
|
|
|
Tcnt = 0; /* index to table of cards played */
|
|
|
|
sum = 0; /* sum of cards played */
|
1993-03-21 12:45:37 +03:00
|
|
|
mego = ugo = FALSE;
|
|
|
|
myturn = !mycrib;
|
|
|
|
for (;;) {
|
1995-03-21 18:03:38 +03:00
|
|
|
last = TRUE; /* enable last flag */
|
|
|
|
prhand(ph, pnum, Playwin, FALSE);
|
|
|
|
prhand(ch, cnum, Compwin, TRUE);
|
|
|
|
prtable(sum);
|
|
|
|
if (myturn) { /* my tyrn to play */
|
|
|
|
if (!anymove(ch, cnum, sum)) { /* if no card to play */
|
|
|
|
if (!mego && cnum) { /* go for comp? */
|
|
|
|
msg("GO");
|
|
|
|
mego = TRUE;
|
|
|
|
}
|
|
|
|
/* can player move? */
|
|
|
|
if (anymove(ph, pnum, sum))
|
|
|
|
myturn = !myturn;
|
|
|
|
else { /* give him his point */
|
|
|
|
msg(quiet ? "You get one" :
|
|
|
|
"You get one point");
|
1997-07-09 10:25:45 +04:00
|
|
|
do_wait();
|
1995-03-21 18:03:38 +03:00
|
|
|
if (chkscr(&pscore, 1))
|
|
|
|
return TRUE;
|
|
|
|
sum = 0;
|
|
|
|
mego = ugo = FALSE;
|
|
|
|
Tcnt = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
played = TRUE;
|
|
|
|
j = -1;
|
|
|
|
k = 0;
|
|
|
|
/* maximize score */
|
|
|
|
for (i = 0; i < cnum; i++) {
|
|
|
|
l = pegscore(ch[i], Table, Tcnt, sum);
|
|
|
|
if (l > k) {
|
|
|
|
k = l;
|
|
|
|
j = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < 0) /* if nothing scores */
|
|
|
|
j = cchose(ch, cnum, sum);
|
|
|
|
crd = ch[j];
|
|
|
|
cremove(crd, ch, cnum--);
|
|
|
|
sum += VAL(crd.rank);
|
|
|
|
Table[Tcnt++] = crd;
|
|
|
|
if (k > 0) {
|
|
|
|
addmsg(quiet ? "I get %d playing " :
|
|
|
|
"I get %d points playing ", k);
|
|
|
|
msgcard(crd, FALSE);
|
|
|
|
endmsg();
|
|
|
|
if (chkscr(&cscore, k))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
myturn = !myturn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!anymove(ph, pnum, sum)) { /* can player move? */
|
|
|
|
if (!ugo && pnum) { /* go for player */
|
|
|
|
msg("You have a GO");
|
|
|
|
ugo = TRUE;
|
|
|
|
}
|
|
|
|
/* can computer play? */
|
|
|
|
if (anymove(ch, cnum, sum))
|
|
|
|
myturn = !myturn;
|
|
|
|
else {
|
|
|
|
msg(quiet ? "I get one" :
|
|
|
|
"I get one point");
|
|
|
|
do_wait();
|
|
|
|
if (chkscr(&cscore, 1))
|
|
|
|
return TRUE;
|
|
|
|
sum = 0;
|
|
|
|
mego = ugo = FALSE;
|
|
|
|
Tcnt = 0;
|
|
|
|
}
|
|
|
|
} else { /* player plays */
|
|
|
|
played = FALSE;
|
|
|
|
if (pnum == 1) {
|
|
|
|
crd = ph[0];
|
|
|
|
msg("You play your last card");
|
|
|
|
} else
|
|
|
|
for (;;) {
|
|
|
|
prhand(ph,
|
|
|
|
pnum, Playwin, FALSE);
|
|
|
|
crd = ph[infrom(ph,
|
|
|
|
pnum, "Your play: ")];
|
|
|
|
if (sum + VAL(crd.rank) <= 31)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
msg("Total > 31 -- try again");
|
|
|
|
}
|
|
|
|
makeknown(&crd, 1);
|
|
|
|
cremove(crd, ph, pnum--);
|
|
|
|
i = pegscore(crd, Table, Tcnt, sum);
|
|
|
|
sum += VAL(crd.rank);
|
|
|
|
Table[Tcnt++] = crd;
|
|
|
|
if (i > 0) {
|
|
|
|
msg(quiet ? "You got %d" :
|
|
|
|
"You got %d points", i);
|
1997-07-09 10:25:45 +04:00
|
|
|
if (pnum == 0)
|
|
|
|
do_wait();
|
1995-03-21 18:03:38 +03:00
|
|
|
if (chkscr(&pscore, i))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
myturn = !myturn;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1995-03-21 18:03:38 +03:00
|
|
|
if (sum >= 31) {
|
|
|
|
if (!myturn)
|
|
|
|
do_wait();
|
1993-03-21 12:45:37 +03:00
|
|
|
sum = 0;
|
|
|
|
mego = ugo = FALSE;
|
|
|
|
Tcnt = 0;
|
1995-03-21 18:03:38 +03:00
|
|
|
last = FALSE; /* disable last flag */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-21 18:03:38 +03:00
|
|
|
if (!pnum && !cnum)
|
|
|
|
break; /* both done */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
prhand(ph, pnum, Playwin, FALSE);
|
|
|
|
prhand(ch, cnum, Compwin, TRUE);
|
|
|
|
prtable(sum);
|
|
|
|
if (last)
|
1995-03-21 18:03:38 +03:00
|
|
|
if (played) {
|
|
|
|
msg(quiet ? "I get one for last" :
|
|
|
|
"I get one point for last");
|
|
|
|
do_wait();
|
|
|
|
if (chkscr(&cscore, 1))
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
msg(quiet ? "You get one for last" :
|
1993-03-21 12:45:37 +03:00
|
|
|
"You get one point for last");
|
1997-07-09 10:25:45 +04:00
|
|
|
do_wait();
|
1995-03-21 18:03:38 +03:00
|
|
|
if (chkscr(&pscore, 1))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return (FALSE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prtable:
|
|
|
|
* Print out the table with the current score
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
prtable(score)
|
1995-03-21 18:03:38 +03:00
|
|
|
int score;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
prhand(Table, Tcnt, Tablewin, FALSE);
|
|
|
|
mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score);
|
|
|
|
wrefresh(Tablewin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* score:
|
|
|
|
* Handle the scoring of the hands
|
|
|
|
*/
|
1995-03-21 18:03:38 +03:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
score(mycrib)
|
1995-03-21 18:03:38 +03:00
|
|
|
BOOLEAN mycrib;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
sorthand(crib, CINHAND);
|
|
|
|
if (mycrib) {
|
1995-03-21 18:03:38 +03:00
|
|
|
if (plyrhand(phand, "hand"))
|
|
|
|
return (TRUE);
|
|
|
|
if (comphand(chand, "hand"))
|
|
|
|
return (TRUE);
|
|
|
|
do_wait();
|
|
|
|
if (comphand(crib, "crib"))
|
|
|
|
return (TRUE);
|
1997-07-09 10:25:45 +04:00
|
|
|
do_wait();
|
1995-03-21 18:03:38 +03:00
|
|
|
} else {
|
|
|
|
if (comphand(chand, "hand"))
|
|
|
|
return (TRUE);
|
|
|
|
if (plyrhand(phand, "hand"))
|
|
|
|
return (TRUE);
|
|
|
|
if (plyrhand(crib, "crib"))
|
|
|
|
return (TRUE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-21 18:03:38 +03:00
|
|
|
return (FALSE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|