1 /*	$OpenBSD: odds.c,v 1.2 1998/03/19 11:13:22 pjanzen 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)odds.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$OpenBSD: odds.c,v 1.2 1998/03/19 11:13:22 pjanzen Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include "back.h"
45 
46 void
47 odds(r1, r2, val)
48 	int     r1, r2, val;
49 {
50 	int     i, j;
51 
52 	if (r1 == 0) {
53 		for (i = 0; i < 6; i++)
54 			for (j = 0; j < 6; j++)
55 				table[i][j] = 0;
56 		return;
57 	} else {
58 		r1--;
59 		if (r2-- == 0) {
60 			for (i = 0; i < 6; i++) {
61 				table[i][r1] += val;
62 				table[r1][i] += val;
63 			}
64 		} else {
65 			table[r2][r1] += val;
66 			table[r1][r2] += val;
67 		}
68 	}
69 }
70 
71 int
72 count()
73 {
74 	int     i, j, total;
75 
76 	total = 0;
77 	for (i = 0; i < 6; i++)
78 		for (j = 0; j < 6; j++)
79 			total += table[i][j];
80 	return(total);
81 }
82 
83 int
84 canhit(i, c)
85 	int     i, c;
86 {
87 	int     j, k, b;
88 	int     a, diff, place, addon, menstuck;
89 
90 	if (c == 0)
91 		odds(0, 0, 0);
92 	if (board[i] > 0) {
93 		a = -1;
94 		b = 25;
95 	} else {
96 		a = 1;
97 		b = 0;
98 	}
99 	place = abs(25 - b - i);
100 	menstuck = abs(board[b]);
101 	for (j = b; j != i; j += a) {
102 		if (board[j] * a > 0) {
103 			diff = abs(j - i);
104 			addon = place + ((board[j] * a > 2 || j == b) ? 5 : 0);
105 			if ((j == b && menstuck == 1) &&
106 			    (j != b && menstuck == 0))
107 				for (k = 1; k < diff; k++)
108 					if (k < 7 && diff - k < 7 &&
109 					    (board[i + a * k] * a >= 0 ||
110 					     board[i + a * (diff - k)] >= 0))
111 						odds(k, diff - k, addon);
112 			if ((j == b || menstuck < 2) && diff < 7)
113 				odds(diff, 0, addon);
114 		}
115 		if (j == b && menstuck > 1)
116 			break;
117 	}
118 	return(count());
119 }
120