xref: /openbsd-src/games/backgammon/common_source/odds.c (revision 95d0ce2685f5d586568c0b520783a6a8f21ac808)
1 /*	$OpenBSD: odds.c,v 1.7 2016/01/08 13:40:05 tb Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "back.h"
33 
34 void
odds(int r1,int r2,int val)35 odds(int r1, int r2, int val)
36 {
37 	int     i, j;
38 
39 	if (r1 == 0) {
40 		for (i = 0; i < 6; i++)
41 			for (j = 0; j < 6; j++)
42 				table[i][j] = 0;
43 		return;
44 	} else {
45 		r1--;
46 		if (r2-- == 0) {
47 			for (i = 0; i < 6; i++) {
48 				table[i][r1] += val;
49 				table[r1][i] += val;
50 			}
51 		} else {
52 			table[r2][r1] += val;
53 			table[r1][r2] += val;
54 		}
55 	}
56 }
57 
58 int
count(void)59 count(void)
60 {
61 	int     i, j, total;
62 
63 	total = 0;
64 	for (i = 0; i < 6; i++)
65 		for (j = 0; j < 6; j++)
66 			total += table[i][j];
67 	return(total);
68 }
69 
70 int
canhit(int i,int c)71 canhit(int i, int c)
72 {
73 	int     j, k, b;
74 	int     a, diff, place, addon, menstuck;
75 
76 	if (c == 0)
77 		odds(0, 0, 0);
78 	if (board[i] > 0) {
79 		a = -1;
80 		b = 25;
81 	} else {
82 		a = 1;
83 		b = 0;
84 	}
85 	place = abs(25 - b - i);
86 	menstuck = abs(board[b]);
87 	for (j = b; j != i; j += a) {
88 		if (board[j] * a > 0) {
89 			diff = abs(j - i);
90 			addon = place + ((board[j] * a > 2 || j == b) ? 5 : 0);
91 			if ((j == b && menstuck == 1) ||
92 			    (j != b && menstuck == 0))
93 				for (k = 1; k < diff; k++)
94 					if (k < 7 && diff - k < 7 &&
95 					    (board[i + a * k] * a >= 0 ||
96 					     board[i + a * (diff - k)] >= 0))
97 						odds(k, diff - k, addon);
98 			if ((j == b || menstuck < 2) && diff < 7)
99 				odds(diff, 0, addon);
100 		}
101 		if (j == b && menstuck > 1)
102 			break;
103 	}
104 	return(count());
105 }
106