Home | History | Annotate | Line # | Download | only in cribbage
support.c revision 1.8
      1  1.8      jmc /*	$NetBSD: support.c,v 1.8 2005/07/02 08:32:32 jmc Exp $	*/
      2  1.3      cgd 
      3  1.3      cgd /*-
      4  1.3      cgd  * Copyright (c) 1980, 1993
      5  1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.7      agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1      cgd  *    may be used to endorse or promote products derived from this software
     17  1.1      cgd  *    without specific prior written permission.
     18  1.1      cgd  *
     19  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1      cgd  * SUCH DAMAGE.
     30  1.1      cgd  */
     31  1.1      cgd 
     32  1.4    lukem #include <sys/cdefs.h>
     33  1.1      cgd #ifndef lint
     34  1.3      cgd #if 0
     35  1.3      cgd static char sccsid[] = "@(#)support.c	8.1 (Berkeley) 5/31/93";
     36  1.3      cgd #else
     37  1.8      jmc __RCSID("$NetBSD: support.c,v 1.8 2005/07/02 08:32:32 jmc Exp $");
     38  1.3      cgd #endif
     39  1.1      cgd #endif /* not lint */
     40  1.1      cgd 
     41  1.3      cgd #include <curses.h>
     42  1.3      cgd #include <string.h>
     43  1.1      cgd 
     44  1.3      cgd #include "deck.h"
     45  1.3      cgd #include "cribbage.h"
     46  1.3      cgd #include "cribcur.h"
     47  1.1      cgd 
     48  1.3      cgd #define	NTV	10		/* number scores to test */
     49  1.1      cgd 
     50  1.1      cgd /* score to test reachability of, and order to test them in */
     51  1.5      jsm const int tv[NTV] = {8, 7, 9, 6, 11, 12, 13, 14, 10, 5};
     52  1.1      cgd 
     53  1.1      cgd /*
     54  1.1      cgd  * computer chooses what to play in pegging...
     55  1.1      cgd  * only called if no playable card will score points
     56  1.1      cgd  */
     57  1.3      cgd int
     58  1.8      jmc cchose(const CARD h[], int n, int s)
     59  1.1      cgd {
     60  1.4    lukem 	int i, j, l;
     61  1.1      cgd 
     62  1.3      cgd 	if (n <= 1)
     63  1.3      cgd 		return (0);
     64  1.3      cgd 	if (s < 4) {		/* try for good value */
     65  1.3      cgd 		if ((j = anysumto(h, n, s, 4)) >= 0)
     66  1.3      cgd 			return (j);
     67  1.3      cgd 		if ((j = anysumto(h, n, s, 3)) >= 0 && s == 0)
     68  1.3      cgd 			return (j);
     69  1.3      cgd 	}
     70  1.3      cgd 	if (s > 0 && s < 20) {
     71  1.3      cgd 				/* try for retaliation to 31 */
     72  1.3      cgd 		for (i = 1; i <= 10; i++) {
     73  1.3      cgd 			if ((j = anysumto(h, n, s, 21 - i)) >= 0) {
     74  1.3      cgd 				if ((l = numofval(h, n, i)) > 0) {
     75  1.3      cgd 					if (l > 1 || VAL(h[j].rank) != i)
     76  1.3      cgd 						return (j);
     77  1.3      cgd 				}
     78  1.3      cgd 			}
     79  1.1      cgd 		}
     80  1.1      cgd 	}
     81  1.3      cgd 	if (s < 15) {
     82  1.3      cgd 				/* for retaliation after 15 */
     83  1.3      cgd 		for (i = 0; i < NTV; i++) {
     84  1.3      cgd 			if ((j = anysumto(h, n, s, tv[i])) >= 0) {
     85  1.3      cgd 				if ((l = numofval(h, n, 15 - tv[i])) > 0) {
     86  1.3      cgd 					if (l > 1 ||
     87  1.3      cgd 					    VAL(h[j].rank) != 15 - tv[i])
     88  1.3      cgd 						return (j);
     89  1.3      cgd 				}
     90  1.3      cgd 			}
     91  1.1      cgd 		}
     92  1.1      cgd 	}
     93  1.1      cgd 	j = -1;
     94  1.3      cgd 				/* remember: h is sorted */
     95  1.3      cgd 	for (i = n - 1; i >= 0; --i) {
     96  1.3      cgd 		l = s + VAL(h[i].rank);
     97  1.3      cgd 		if (l > 31)
     98  1.3      cgd 			continue;
     99  1.3      cgd 		if (l != 5 && l != 10 && l != 21) {
    100  1.3      cgd 			j = i;
    101  1.3      cgd 			break;
    102  1.3      cgd 		}
    103  1.3      cgd 	}
    104  1.3      cgd 	if (j >= 0)
    105  1.3      cgd 		return (j);
    106  1.3      cgd 	for (i = n - 1; i >= 0; --i) {
    107  1.3      cgd 		l = s + VAL(h[i].rank);
    108  1.3      cgd 		if (l > 31)
    109  1.3      cgd 			continue;
    110  1.3      cgd 		if (j < 0)
    111  1.3      cgd 			j = i;
    112  1.3      cgd 		if (l != 5 && l != 21) {
    113  1.3      cgd 			j = i;
    114  1.3      cgd 			break;
    115  1.3      cgd 		}
    116  1.1      cgd 	}
    117  1.3      cgd 	return (j);
    118  1.1      cgd }
    119  1.1      cgd 
    120  1.1      cgd /*
    121  1.1      cgd  * plyrhand:
    122  1.1      cgd  *	Evaluate and score a player hand or crib
    123  1.1      cgd  */
    124  1.3      cgd int
    125  1.8      jmc plyrhand(const CARD hand[], const char *s)
    126  1.1      cgd {
    127  1.3      cgd 	static char prompt[BUFSIZ];
    128  1.4    lukem 	int i, j;
    129  1.4    lukem 	BOOLEAN win;
    130  1.3      cgd 
    131  1.3      cgd 	prhand(hand, CINHAND, Playwin, FALSE);
    132  1.3      cgd 	(void) sprintf(prompt, "Your %s scores ", s);
    133  1.3      cgd 	i = scorehand(hand, turnover, CINHAND, strcmp(s, "crib") == 0, explain);
    134  1.3      cgd 	if ((j = number(0, 29, prompt)) == 19)
    135  1.3      cgd 		j = 0;
    136  1.3      cgd 	if (i != j) {
    137  1.3      cgd 		if (i < j) {
    138  1.3      cgd 			win = chkscr(&pscore, i);
    139  1.3      cgd 			msg("It's really only %d points; I get %d", i, 2);
    140  1.3      cgd 			if (!win)
    141  1.3      cgd 				win = chkscr(&cscore, 2);
    142  1.3      cgd 		} else {
    143  1.3      cgd 			win = chkscr(&pscore, j);
    144  1.3      cgd 			msg("You should have taken %d, not %d!", i, j);
    145  1.3      cgd 		}
    146  1.3      cgd 		if (explain)
    147  1.6  thorpej 			msg("Explanation: %s", explan);
    148  1.3      cgd 		do_wait();
    149  1.3      cgd 	} else
    150  1.3      cgd 		win = chkscr(&pscore, i);
    151  1.3      cgd 	return (win);
    152  1.1      cgd }
    153  1.1      cgd 
    154  1.1      cgd /*
    155  1.1      cgd  * comphand:
    156  1.1      cgd  *	Handle scoring and displaying the computers hand
    157  1.1      cgd  */
    158  1.3      cgd int
    159  1.8      jmc comphand(const CARD h[], const char *s)
    160  1.1      cgd {
    161  1.4    lukem 	int j;
    162  1.1      cgd 
    163  1.1      cgd 	j = scorehand(h, turnover, CINHAND, strcmp(s, "crib") == 0, FALSE);
    164  1.1      cgd 	prhand(h, CINHAND, Compwin, FALSE);
    165  1.1      cgd 	msg("My %s scores %d", s, (j == 0 ? 19 : j));
    166  1.3      cgd 	return (chkscr(&cscore, j));
    167  1.1      cgd }
    168  1.1      cgd 
    169  1.1      cgd /*
    170  1.1      cgd  * chkscr:
    171  1.1      cgd  *	Add inc to scr and test for > glimit, printing on the scoring
    172  1.1      cgd  *	board while we're at it.
    173  1.1      cgd  */
    174  1.3      cgd int Lastscore[2] = {-1, -1};
    175  1.1      cgd 
    176  1.3      cgd int
    177  1.8      jmc chkscr(int *scr, int inc)
    178  1.1      cgd {
    179  1.3      cgd 	BOOLEAN myturn;
    180  1.1      cgd 
    181  1.1      cgd 	myturn = (scr == &cscore);
    182  1.1      cgd 	if (inc != 0) {
    183  1.4    lukem 		prpeg(Lastscore[(int)myturn], '.', myturn);
    184  1.4    lukem 		Lastscore[(int)myturn] = *scr;
    185  1.1      cgd 		*scr += inc;
    186  1.1      cgd 		prpeg(*scr, PEG, myturn);
    187  1.1      cgd 		refresh();
    188  1.1      cgd 	}
    189  1.1      cgd 	return (*scr >= glimit);
    190  1.1      cgd }
    191  1.1      cgd 
    192  1.1      cgd /*
    193  1.1      cgd  * prpeg:
    194  1.1      cgd  *	Put out the peg character on the score board and put the
    195  1.1      cgd  *	score up on the board.
    196  1.1      cgd  */
    197  1.3      cgd void
    198  1.8      jmc prpeg(int curscore, int pegc, BOOLEAN myturn)
    199  1.1      cgd {
    200  1.4    lukem 	int y, x;
    201  1.1      cgd 
    202  1.1      cgd 	if (!myturn)
    203  1.1      cgd 		y = SCORE_Y + 2;
    204  1.1      cgd 	else
    205  1.1      cgd 		y = SCORE_Y + 5;
    206  1.1      cgd 
    207  1.8      jmc 	if (curscore <= 0 || curscore >= glimit) {
    208  1.8      jmc 		if (pegc == '.')
    209  1.8      jmc 			pegc = ' ';
    210  1.8      jmc 		if (curscore == 0)
    211  1.1      cgd 			x = SCORE_X + 2;
    212  1.1      cgd 		else {
    213  1.1      cgd 			x = SCORE_X + 2;
    214  1.1      cgd 			y++;
    215  1.1      cgd 		}
    216  1.3      cgd 	} else {
    217  1.8      jmc 		x = (curscore - 1) % 30;
    218  1.8      jmc 		if (curscore > 90 || (curscore > 30 && curscore <= 60)) {
    219  1.1      cgd 			y++;
    220  1.1      cgd 			x = 29 - x;
    221  1.1      cgd 		}
    222  1.1      cgd 		x += x / 5;
    223  1.1      cgd 		x += SCORE_X + 3;
    224  1.1      cgd 	}
    225  1.8      jmc 	mvaddch(y, x, pegc);
    226  1.8      jmc 	mvprintw(SCORE_Y + (myturn ? 7 : 1), SCORE_X + 10, "%3d", curscore);
    227  1.1      cgd }
    228  1.1      cgd 
    229  1.1      cgd /*
    230  1.1      cgd  * cdiscard -- the computer figures out what is the best discard for
    231  1.1      cgd  * the crib and puts the best two cards at the end
    232  1.1      cgd  */
    233  1.3      cgd void
    234  1.8      jmc cdiscard(BOOLEAN mycrib)
    235  1.1      cgd {
    236  1.3      cgd 	CARD    d[CARDS], h[FULLHAND], cb[2];
    237  1.4    lukem 	int i, j, k;
    238  1.3      cgd 	int     nc, ns;
    239  1.3      cgd 	long    sums[15];
    240  1.3      cgd 	static int undo1[15] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4};
    241  1.3      cgd 	static int undo2[15] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5};
    242  1.1      cgd 
    243  1.3      cgd 	makedeck(d);
    244  1.1      cgd 	nc = CARDS;
    245  1.3      cgd 	for (i = 0; i < knownum; i++) {	/* get all other cards */
    246  1.3      cgd 		cremove(known[i], d, nc--);
    247  1.1      cgd 	}
    248  1.3      cgd 	for (i = 0; i < 15; i++)
    249  1.3      cgd 		sums[i] = 0L;
    250  1.1      cgd 	ns = 0;
    251  1.3      cgd 	for (i = 0; i < (FULLHAND - 1); i++) {
    252  1.3      cgd 		cb[0] = chand[i];
    253  1.3      cgd 		for (j = i + 1; j < FULLHAND; j++) {
    254  1.3      cgd 			cb[1] = chand[j];
    255  1.3      cgd 			for (k = 0; k < FULLHAND; k++)
    256  1.3      cgd 				h[k] = chand[k];
    257  1.3      cgd 			cremove(chand[i], h, FULLHAND);
    258  1.3      cgd 			cremove(chand[j], h, FULLHAND - 1);
    259  1.3      cgd 			for (k = 0; k < nc; k++) {
    260  1.3      cgd 				sums[ns] +=
    261  1.3      cgd 				    scorehand(h, d[k], CINHAND, TRUE, FALSE);
    262  1.3      cgd 				if (mycrib)
    263  1.3      cgd 					sums[ns] += adjust(cb, d[k]);
    264  1.3      cgd 				else
    265  1.3      cgd 					sums[ns] -= adjust(cb, d[k]);
    266  1.3      cgd 			}
    267  1.3      cgd 			++ns;
    268  1.1      cgd 		}
    269  1.1      cgd 	}
    270  1.1      cgd 	j = 0;
    271  1.3      cgd 	for (i = 1; i < 15; i++)
    272  1.3      cgd 		if (sums[i] > sums[j])
    273  1.3      cgd 			j = i;
    274  1.3      cgd 	for (k = 0; k < FULLHAND; k++)
    275  1.3      cgd 		h[k] = chand[k];
    276  1.3      cgd 	cremove(h[undo1[j]], chand, FULLHAND);
    277  1.3      cgd 	cremove(h[undo2[j]], chand, FULLHAND - 1);
    278  1.3      cgd 	chand[4] = h[undo1[j]];
    279  1.3      cgd 	chand[5] = h[undo2[j]];
    280  1.1      cgd }
    281  1.1      cgd 
    282  1.1      cgd /*
    283  1.1      cgd  * returns true if some card in hand can be played without exceeding 31
    284  1.1      cgd  */
    285  1.3      cgd int
    286  1.8      jmc anymove(const CARD hand[], int n, int sum)
    287  1.1      cgd {
    288  1.4    lukem 	int i, j;
    289  1.1      cgd 
    290  1.3      cgd 	if (n < 1)
    291  1.3      cgd 		return (FALSE);
    292  1.1      cgd 	j = hand[0].rank;
    293  1.3      cgd 	for (i = 1; i < n; i++) {
    294  1.3      cgd 		if (hand[i].rank < j)
    295  1.3      cgd 			j = hand[i].rank;
    296  1.1      cgd 	}
    297  1.3      cgd 	return (sum + VAL(j) <= 31);
    298  1.1      cgd }
    299  1.1      cgd 
    300  1.1      cgd /*
    301  1.1      cgd  * anysumto returns the index (0 <= i < n) of the card in hand that brings
    302  1.1      cgd  * the s up to t, or -1 if there is none
    303  1.1      cgd  */
    304  1.3      cgd int
    305  1.8      jmc anysumto(const CARD hand[], int n, int s, int t)
    306  1.1      cgd {
    307  1.4    lukem 	int i;
    308  1.1      cgd 
    309  1.3      cgd 	for (i = 0; i < n; i++) {
    310  1.3      cgd 		if (s + VAL(hand[i].rank) == t)
    311  1.3      cgd 			return (i);
    312  1.1      cgd 	}
    313  1.3      cgd 	return (-1);
    314  1.1      cgd }
    315  1.1      cgd 
    316  1.1      cgd /*
    317  1.1      cgd  * return the number of cards in h having the given rank value
    318  1.1      cgd  */
    319  1.3      cgd int
    320  1.8      jmc numofval(const CARD h[], int n, int v)
    321  1.1      cgd {
    322  1.4    lukem 	int i, j;
    323  1.1      cgd 
    324  1.1      cgd 	j = 0;
    325  1.3      cgd 	for (i = 0; i < n; i++) {
    326  1.3      cgd 		if (VAL(h[i].rank) == v)
    327  1.3      cgd 			++j;
    328  1.1      cgd 	}
    329  1.3      cgd 	return (j);
    330  1.1      cgd }
    331  1.1      cgd 
    332  1.1      cgd /*
    333  1.1      cgd  * makeknown remembers all n cards in h for future recall
    334  1.1      cgd  */
    335  1.3      cgd void
    336  1.8      jmc makeknown(const CARD h[], int n)
    337  1.1      cgd {
    338  1.4    lukem 	int i;
    339  1.1      cgd 
    340  1.3      cgd 	for (i = 0; i < n; i++)
    341  1.3      cgd 		known[knownum++] = h[i];
    342  1.1      cgd }
    343