NetBSD/games/cribbage/support.c

352 lines
7.9 KiB
C
Raw Normal View History

2009-08-12 09:48:04 +04:00
/* $NetBSD: support.c,v 1.14 2009/08/12 05:48:04 dholland 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. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* 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
1995-03-21 18:03:38 +03:00
#if 0
static char sccsid[] = "@(#)support.c 8.1 (Berkeley) 5/31/93";
#else
2009-08-12 09:48:04 +04:00
__RCSID("$NetBSD: support.c,v 1.14 2009/08/12 05:48:04 dholland 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>
#include <stdlib.h>
1995-03-21 18:03:38 +03:00
#include <string.h>
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
#include "deck.h"
#include "cribbage.h"
#include "cribcur.h"
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
#define NTV 10 /* number scores to test */
1993-03-21 12:45:37 +03:00
/* score to test reachability of, and order to test them in */
2009-08-12 09:48:04 +04:00
static const int tv[NTV] = {8, 7, 9, 6, 11, 12, 13, 14, 10, 5};
static int anysumto(const CARD[], int, int, int);
static void prpeg(int, int, BOOLEAN);
static int numofval(const CARD[], int, int);
1993-03-21 12:45:37 +03:00
/*
* computer chooses what to play in pegging...
* only called if no playable card will score points
*/
1995-03-21 18:03:38 +03:00
int
2005-07-02 12:32:32 +04:00
cchose(const CARD h[], int n, int s)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i, j, l;
1995-03-21 18:03:38 +03:00
if (n <= 1)
return (0);
if (s < 4) { /* try for good value */
if ((j = anysumto(h, n, s, 4)) >= 0)
return (j);
if ((j = anysumto(h, n, s, 3)) >= 0 && s == 0)
return (j);
1993-03-21 12:45:37 +03:00
}
1995-03-21 18:03:38 +03:00
if (s > 0 && s < 20) {
/* try for retaliation to 31 */
for (i = 1; i <= 10; i++) {
if ((j = anysumto(h, n, s, 21 - i)) >= 0) {
if ((l = numofval(h, n, i)) > 0) {
if (l > 1 || VAL(h[j].rank) != i)
return (j);
}
}
1993-03-21 12:45:37 +03:00
}
}
1995-03-21 18:03:38 +03:00
if (s < 15) {
/* for retaliation after 15 */
for (i = 0; i < NTV; i++) {
if ((j = anysumto(h, n, s, tv[i])) >= 0) {
if ((l = numofval(h, n, 15 - tv[i])) > 0) {
if (l > 1 ||
VAL(h[j].rank) != 15 - tv[i])
return (j);
}
}
1993-03-21 12:45:37 +03:00
}
}
j = -1;
1995-03-21 18:03:38 +03:00
/* remember: h is sorted */
for (i = n - 1; i >= 0; --i) {
l = s + VAL(h[i].rank);
if (l > 31)
continue;
if (l != 5 && l != 10 && l != 21) {
j = i;
break;
}
1993-03-21 12:45:37 +03:00
}
1995-03-21 18:03:38 +03:00
if (j >= 0)
return (j);
for (i = n - 1; i >= 0; --i) {
l = s + VAL(h[i].rank);
if (l > 31)
continue;
if (j < 0)
j = i;
if (l != 5 && l != 21) {
j = i;
break;
}
1993-03-21 12:45:37 +03:00
}
if (j < 0) {
printf("\ncchose: internal error %d %d\n", j, n);
exit(93);
}
1995-03-21 18:03:38 +03:00
return (j);
1993-03-21 12:45:37 +03:00
}
/*
* plyrhand:
* Evaluate and score a player hand or crib
*/
1995-03-21 18:03:38 +03:00
int
2005-07-02 12:32:32 +04:00
plyrhand(const CARD hand[], const char *s)
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
static char prompt[BUFSIZ];
1997-10-10 16:26:34 +04:00
int i, j;
BOOLEAN win;
1995-03-21 18:03:38 +03:00
prhand(hand, CINHAND, Playwin, FALSE);
2009-06-04 08:48:04 +04:00
(void) snprintf(prompt, sizeof(prompt), "Your %s scores ", s);
1995-03-21 18:03:38 +03:00
i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
if ((j = number(0, 29, prompt)) == 19)
j = 0;
if (i != j) {
if (i < j) {
win = chkscr(&pscore, i);
msg("It's really only %d points; I get %d", i, 2);
if (!win)
win = chkscr(&cscore, 2);
} else {
win = chkscr(&pscore, j);
msg("You should have taken %d, not %d!", i, j);
}
if (explain)
msg("Explanation: %s", explan);
1995-03-21 18:03:38 +03:00
do_wait();
} else
win = chkscr(&pscore, i);
return (win);
1993-03-21 12:45:37 +03:00
}
/*
* comphand:
* Handle scoring and displaying the computers hand
*/
1995-03-21 18:03:38 +03:00
int
2005-07-02 12:32:32 +04:00
comphand(const CARD h[], const char *s)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int j;
1993-03-21 12:45:37 +03:00
j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE);
prhand(h, CINHAND, Compwin, FALSE);
msg("My %s scores %d", s, (j == 0 ? 19 : j));
1995-03-21 18:03:38 +03:00
return (chkscr(&cscore, j));
1993-03-21 12:45:37 +03:00
}
/*
* chkscr:
* Add inc to scr and test for > glimit, printing on the scoring
* board while we're at it.
*/
1995-03-21 18:03:38 +03:00
int Lastscore[2] = {-1, -1};
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
int
2005-07-02 12:32:32 +04:00
chkscr(int *scr, int inc)
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
BOOLEAN myturn;
1993-03-21 12:45:37 +03:00
myturn = (scr == &cscore);
if (inc != 0) {
1997-10-10 16:26:34 +04:00
prpeg(Lastscore[(int)myturn], '.', myturn);
Lastscore[(int)myturn] = *scr;
1993-03-21 12:45:37 +03:00
*scr += inc;
prpeg(*scr, PEG, myturn);
refresh();
}
return (*scr >= glimit);
}
/*
* prpeg:
* Put out the peg character on the score board and put the
* score up on the board.
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:32:32 +04:00
prpeg(int curscore, int pegc, BOOLEAN myturn)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int y, x;
1993-03-21 12:45:37 +03:00
if (!myturn)
y = SCORE_Y + 2;
else
y = SCORE_Y + 5;
2005-07-02 12:32:32 +04:00
if (curscore <= 0 || curscore >= glimit) {
if (pegc == '.')
pegc = ' ';
if (curscore == 0)
1993-03-21 12:45:37 +03:00
x = SCORE_X + 2;
else {
x = SCORE_X + 2;
y++;
}
1995-03-21 18:03:38 +03:00
} else {
2005-07-02 12:32:32 +04:00
x = (curscore - 1) % 30;
if (curscore > 90 || (curscore > 30 && curscore <= 60)) {
1993-03-21 12:45:37 +03:00
y++;
x = 29 - x;
}
x += x / 5;
x += SCORE_X + 3;
}
2005-07-02 12:32:32 +04:00
mvaddch(y, x, pegc);
mvprintw(SCORE_Y + (myturn ? 7 : 1), SCORE_X + 10, "%3d", curscore);
1993-03-21 12:45:37 +03:00
}
/*
* cdiscard -- the computer figures out what is the best discard for
* the crib and puts the best two cards at the end
*/
1995-03-21 18:03:38 +03:00
void
2005-07-02 12:32:32 +04:00
cdiscard(BOOLEAN mycrib)
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
CARD d[CARDS], h[FULLHAND], cb[2];
1997-10-10 16:26:34 +04:00
int i, j, k;
1995-03-21 18:03:38 +03:00
int nc, ns;
long sums[15];
static int undo1[15] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
static int undo2[15] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5};
makedeck(d);
1993-03-21 12:45:37 +03:00
nc = CARDS;
1995-03-21 18:03:38 +03:00
for (i = 0; i < knownum; i++) { /* get all other cards */
cremove(known[i], d, nc--);
1993-03-21 12:45:37 +03:00
}
1995-03-21 18:03:38 +03:00
for (i = 0; i < 15; i++)
sums[i] = 0L;
1993-03-21 12:45:37 +03:00
ns = 0;
1995-03-21 18:03:38 +03:00
for (i = 0; i < (FULLHAND - 1); i++) {
cb[0] = chand[i];
for (j = i + 1; j < FULLHAND; j++) {
cb[1] = chand[j];
for (k = 0; k < FULLHAND; k++)
h[k] = chand[k];
cremove(chand[i], h, FULLHAND);
cremove(chand[j], h, FULLHAND - 1);
for (k = 0; k < nc; k++) {
sums[ns] +=
scorehand(h, d[k], CINHAND, TRUE, FALSE);
if (mycrib)
sums[ns] += adjust(cb, d[k]);
else
sums[ns] -= adjust(cb, d[k]);
}
++ns;
1993-03-21 12:45:37 +03:00
}
}
j = 0;
1995-03-21 18:03:38 +03:00
for (i = 1; i < 15; i++)
if (sums[i] > sums[j])
j = i;
for (k = 0; k < FULLHAND; k++)
h[k] = chand[k];
cremove(h[undo1[j]], chand, FULLHAND);
cremove(h[undo2[j]], chand, FULLHAND - 1);
chand[4] = h[undo1[j]];
chand[5] = h[undo2[j]];
1993-03-21 12:45:37 +03:00
}
/*
* returns true if some card in hand can be played without exceeding 31
*/
1995-03-21 18:03:38 +03:00
int
2005-07-02 12:32:32 +04:00
anymove(const CARD hand[], int n, int sum)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i, j;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
if (n < 1)
return (FALSE);
1993-03-21 12:45:37 +03:00
j = hand[0].rank;
1995-03-21 18:03:38 +03:00
for (i = 1; i < n; i++) {
if (hand[i].rank < j)
j = hand[i].rank;
1993-03-21 12:45:37 +03:00
}
1995-03-21 18:03:38 +03:00
return (sum + VAL(j) <= 31);
1993-03-21 12:45:37 +03:00
}
/*
* anysumto returns the index (0 <= i < n) of the card in hand that brings
* the s up to t, or -1 if there is none
*/
2009-08-12 09:48:04 +04:00
static int
2005-07-02 12:32:32 +04:00
anysumto(const CARD hand[], int n, int s, int t)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
for (i = 0; i < n; i++) {
if (s + VAL(hand[i].rank) == t)
return (i);
1993-03-21 12:45:37 +03:00
}
1995-03-21 18:03:38 +03:00
return (-1);
1993-03-21 12:45:37 +03:00
}
/*
* return the number of cards in h having the given rank value
*/
2009-08-12 09:48:04 +04:00
static int
2005-07-02 12:32:32 +04:00
numofval(const CARD h[], int n, int v)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i, j;
1993-03-21 12:45:37 +03:00
j = 0;
1995-03-21 18:03:38 +03:00
for (i = 0; i < n; i++) {
if (VAL(h[i].rank) == v)
++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
}
/*
* makeknown remembers all n cards in h for future recall
*/
1995-03-21 18:03:38 +03:00
void
2005-07-02 12:32:32 +04:00
makeknown(const CARD h[], int n)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
for (i = 0; i < n; i++)
known[knownum++] = h[i];
1993-03-21 12:45:37 +03:00
}