xref: /netbsd-src/games/backgammon/common_source/odds.c (revision 1990635dbbf77a3bbe516360ed6a4b9711d9eea7)
1*1990635dSchristos /*	$NetBSD: odds.c,v 1.7 2006/03/22 04:22:05 christos Exp $	*/
2101657d1Scgd 
361f28255Scgd /*
4101657d1Scgd  * Copyright (c) 1980, 1993
5101657d1Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3216641b76Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34101657d1Scgd #if 0
35101657d1Scgd static char sccsid[] = "@(#)odds.c	8.1 (Berkeley) 5/31/93";
36101657d1Scgd #else
37*1990635dSchristos __RCSID("$NetBSD: odds.c,v 1.7 2006/03/22 04:22:05 christos Exp $");
38101657d1Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include "back.h"
4261f28255Scgd 
4316641b76Slukem void
odds(int r1,int r2,int val)44fcb317eeSjmc odds(int r1, int r2, int val)
4561f28255Scgd {
4616641b76Slukem 	int     i, j;
4761f28255Scgd 
4861f28255Scgd 	if (r1 == 0) {
4961f28255Scgd 		for (i = 0; i < 6; i++)
5061f28255Scgd 			for (j = 0; j < 6; j++)
5161f28255Scgd 				table[i][j] = 0;
5261f28255Scgd 		return;
5361f28255Scgd 	} else {
5461f28255Scgd 		r1--;
5516641b76Slukem 		if (r2-- == 0) {
5661f28255Scgd 			for (i = 0; i < 6; i++) {
5761f28255Scgd 				table[i][r1] += val;
5861f28255Scgd 				table[r1][i] += val;
5961f28255Scgd 			}
6016641b76Slukem 		} else {
6161f28255Scgd 			table[r2][r1] += val;
6261f28255Scgd 			table[r1][r2] += val;
6361f28255Scgd 		}
6461f28255Scgd 	}
6561f28255Scgd }
6661f28255Scgd 
6716641b76Slukem int
count(void)68fcb317eeSjmc count(void)
6916641b76Slukem {
7016641b76Slukem 	int     i;
7116641b76Slukem 	int     j;
7216641b76Slukem 	int     total;
7361f28255Scgd 
7461f28255Scgd 	total = 0;
7561f28255Scgd 	for (i = 0; i < 6; i++)
7661f28255Scgd 		for (j = 0; j < 6; j++)
7761f28255Scgd 			total += table[i][j];
7861f28255Scgd 	return (total);
7961f28255Scgd }
8016641b76Slukem 
8116641b76Slukem int
canhit(int i,int c)82fcb317eeSjmc canhit(int i, int c)
8361f28255Scgd {
8416641b76Slukem 	int     j, k, b;
8516641b76Slukem 	int     a, diff, place, addon, menstuck;
8661f28255Scgd 
8761f28255Scgd 	if (c == 0)
8861f28255Scgd 		odds(0, 0, 0);
8961f28255Scgd 	if (board[i] > 0) {
9061f28255Scgd 		a = -1;
9161f28255Scgd 		b = 25;
9261f28255Scgd 	} else {
9361f28255Scgd 		a = 1;
9461f28255Scgd 		b = 0;
9561f28255Scgd 	}
9661f28255Scgd 	place = abs(25 - b - i);
9761f28255Scgd 	menstuck = abs(board[b]);
9861f28255Scgd 	for (j = b; j != i; j += a) {
9961f28255Scgd 		if (board[j] * a > 0) {
10061f28255Scgd 			diff = abs(j - i);
10161f28255Scgd 			addon = place + ((board[j] * a > 2 || j == b) ? 5 : 0);
102*1990635dSchristos 			if ((j == b && menstuck == 1) ||
10361f28255Scgd 			    (j != b && menstuck == 0))
10461f28255Scgd 				for (k = 1; k < diff; k++)
10561f28255Scgd 					if (k < 7 && diff - k < 7 &&
10661f28255Scgd 					    (board[i + a * k] * a >= 0 ||
10761f28255Scgd 						board[i + a * (diff - k)] >= 0))
10861f28255Scgd 						odds(k, diff - k, addon);
10961f28255Scgd 			if ((j == b || menstuck < 2) && diff < 7)
11061f28255Scgd 				odds(diff, 0, addon);
11161f28255Scgd 		}
11261f28255Scgd 		if (j == b && menstuck > 1)
11361f28255Scgd 			break;
11461f28255Scgd 	}
11561f28255Scgd 	return (count());
11661f28255Scgd }
117