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