algor.cc revision 1.1 1 1.1 christos /* $NetBSD: algor.cc,v 1.1 2003/12/27 01:16:55 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos /*
40 1.1 christos * algor.C: Computer algorithm
41 1.1 christos */
42 1.1 christos #include "defs.h"
43 1.1 christos RCSID("$NetBSD: algor.cc,v 1.1 2003/12/27 01:16:55 christos Exp $")
44 1.1 christos
45 1.1 christos #include "algor.h"
46 1.1 christos #include "board.h"
47 1.1 christos #include "box.h"
48 1.1 christos #include "random.h"
49 1.1 christos
50 1.1 christos ALGOR::ALGOR(const char c) : PLAYER(c)
51 1.1 christos {
52 1.1 christos #ifdef notyet
53 1.1 christos // Single Edges = (x + y) * 2
54 1.1 christos _edge1 = (_b.nx() * _b.ny()) * 2;
55 1.1 christos // Shared Edges = (x * (y - 1)) + ((x - 1) * y)
56 1.1 christos _edge2 = (_b.nx() * (_b.ny() - 1)) + ((_b.nx() - 1) * _b.ny());
57 1.1 christos // Maximum Edges filled before closure = x * y * 2
58 1.1 christos _maxedge = _b.nx() * _b.ny() * 2;
59 1.1 christos #endif
60 1.1 christos }
61 1.1 christos
62 1.1 christos // Find the first closure, i.e. a box that has 3 edges
63 1.1 christos int ALGOR::find_closure(size_t& y, size_t& x, int& dir, BOARD& b)
64 1.1 christos {
65 1.1 christos RANDOM rdy(b.ny()), rdx(b.nx());
66 1.1 christos
67 1.1 christos for (y = rdy(); y < b.ny(); y = rdy()) {
68 1.1 christos rdx.clear();
69 1.1 christos for (x = rdx(); x < b.nx(); x = rdx()) {
70 1.1 christos BOX box(y, x, b);
71 1.1 christos if (box.count() == 3) {
72 1.1 christos for (dir = BOX::first; dir < BOX::last; dir++)
73 1.1 christos if (!box.isset(dir))
74 1.1 christos return 1;
75 1.1 christos b.abort("find_closure: 3 sided box[%d,%d] has no free sides",
76 1.1 christos y, x);
77 1.1 christos }
78 1.1 christos }
79 1.1 christos }
80 1.1 christos return 0;
81 1.1 christos }
82 1.1 christos
83 1.1 christos #if 0
84 1.1 christos size_t ALGOR::find_single()
85 1.1 christos {
86 1.1 christos size_t ne;
87 1.1 christos
88 1.1 christos // Find the number of single edges in use
89 1.1 christos for (size_t x = 0; x < b.nx(); x++) {
90 1.1 christos BOX tbox(0, x, b);
91 1.1 christos ne += tbox.isset(BOX::top);
92 1.1 christos BOX bbox(b.ny() - 1, x, b);
93 1.1 christos ne += bbox.isset(BOX::bottom);
94 1.1 christos }
95 1.1 christos for (size_t y = 0; y < _b.ny(); y++) {
96 1.1 christos BOX lbox(y, 0, b);
97 1.1 christos ne += lbox.isset(BOX::left);
98 1.1 christos BOX rbox(y,_b.nx() - 1, b);
99 1.1 christos ne += rbox.isset(BOX::right);
100 1.1 christos }
101 1.1 christos return ne;
102 1.1 christos }
103 1.1 christos #endif
104 1.1 christos
105 1.1 christos
106 1.1 christos // Count a closure, by counting all boxes that we can close in the current
107 1.1 christos // move
108 1.1 christos size_t ALGOR::count_closure(size_t& y, size_t& x, int& dir, BOARD& b)
109 1.1 christos {
110 1.1 christos size_t i = 0;
111 1.1 christos size_t tx, ty;
112 1.1 christos int tdir, mv;
113 1.1 christos
114 1.1 christos while (find_closure(ty, tx, tdir, b)) {
115 1.1 christos if (i == 0) {
116 1.1 christos // Mark the beginning of the closure
117 1.1 christos x = tx;
118 1.1 christos y = ty;
119 1.1 christos dir = tdir;
120 1.1 christos }
121 1.1 christos if ((mv = b.domove(ty, tx, tdir, getWho())) == -1)
122 1.1 christos b.abort("count_closure: Invalid move (%d, %d, %d)", y, x, dir);
123 1.1 christos else
124 1.1 christos i += mv;
125 1.1 christos }
126 1.1 christos return i;
127 1.1 christos }
128 1.1 christos
129 1.1 christos
130 1.1 christos /*
131 1.1 christos * Find the largest closure, by closing all possible closures.
132 1.1 christos * return the number of boxes closed in the maximum closure,
133 1.1 christos * and the first box of the maximum closure in (x, y, dir)
134 1.1 christos */
135 1.1 christos int ALGOR::find_max_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
136 1.1 christos {
137 1.1 christos BOARD nb(b);
138 1.1 christos int tdir, maxdir = -1;
139 1.1 christos size_t nbox, maxbox = 0;
140 1.1 christos size_t tx, ty, maxx = ~0, maxy = ~0;
141 1.1 christos
142 1.1 christos while ((nbox = count_closure(ty, tx, tdir, nb)) != 0)
143 1.1 christos if (nbox > maxbox) {
144 1.1 christos // This closure is better, update max
145 1.1 christos maxbox = nbox;
146 1.1 christos maxx = tx;
147 1.1 christos maxy = ty;
148 1.1 christos maxdir = tdir;
149 1.1 christos }
150 1.1 christos
151 1.1 christos // Return the max found
152 1.1 christos y = maxy;
153 1.1 christos x = maxx;
154 1.1 christos dir = maxdir;
155 1.1 christos return maxbox;
156 1.1 christos }
157 1.1 christos
158 1.1 christos
159 1.1 christos // Find if a turn does not result in a capture on the given box
160 1.1 christos // and return the direction if found.
161 1.1 christos int ALGOR::try_good_turn(BOX& box, size_t y, size_t x, int& dir, BOARD& b)
162 1.1 christos {
163 1.1 christos // Sanity check; we must have a good box
164 1.1 christos if (box.count() >= 2)
165 1.1 christos b.abort("try_good_turn: box[%d,%d] has more than 2 sides occupied",
166 1.1 christos y, x);
167 1.1 christos
168 1.1 christos // Make sure we don't make a closure in an adjacent box.
169 1.1 christos // We use a random direction to randomize the game
170 1.1 christos RANDOM rd(BOX::last);
171 1.1 christos for (dir = rd(); dir < BOX::last; dir = rd())
172 1.1 christos if (!box.isset(dir)) {
173 1.1 christos size_t by = y + BOX::edges[dir].y;
174 1.1 christos size_t bx = x + BOX::edges[dir].x;
175 1.1 christos if (!b.bounds(by, bx))
176 1.1 christos return 1;
177 1.1 christos
178 1.1 christos BOX nbox(by, bx, b);
179 1.1 christos if (nbox.count() < 2)
180 1.1 christos return 1;
181 1.1 christos }
182 1.1 christos
183 1.1 christos return 0;
184 1.1 christos }
185 1.1 christos
186 1.1 christos
187 1.1 christos // Try to find a turn that does not result in an opponent closure, and
188 1.1 christos // return it in (x, y, dir); if not found return 0.
189 1.1 christos int ALGOR::find_good_turn(size_t& y, size_t& x, int& dir, const BOARD& b)
190 1.1 christos {
191 1.1 christos BOARD nb(b);
192 1.1 christos RANDOM rdy(b.ny()), rdx(b.nx());
193 1.1 christos
194 1.1 christos for (y = rdy(); y < b.ny(); y = rdy()) {
195 1.1 christos rdx.clear();
196 1.1 christos for (x = rdx(); x < b.nx(); x = rdx()) {
197 1.1 christos BOX box(y, x, nb);
198 1.1 christos if (box.count() < 2 && try_good_turn(box, y, x, dir, nb))
199 1.1 christos return 1;
200 1.1 christos }
201 1.1 christos }
202 1.1 christos return 0;
203 1.1 christos }
204 1.1 christos
205 1.1 christos // On a box with 2 edges, return the first or the last free edge, depending
206 1.1 christos // on the order specified
207 1.1 christos int ALGOR::try_bad_turn(BOX& box, size_t& y, size_t& x, int& dir, BOARD& b,
208 1.1 christos int last)
209 1.1 christos {
210 1.1 christos if (4 - box.count() <= last)
211 1.1 christos b.abort("try_bad_turn: Called at [%d,%d] for %d with %d",
212 1.1 christos y, x, last, box.count());
213 1.1 christos for (dir = BOX::first; dir < BOX::last; dir++)
214 1.1 christos if (!box.isset(dir)) {
215 1.1 christos if (!last)
216 1.1 christos return 1;
217 1.1 christos else
218 1.1 christos last--;
219 1.1 christos }
220 1.1 christos return 0;
221 1.1 christos }
222 1.1 christos
223 1.1 christos // Find a box that has 2 edges and return the first free edge of that
224 1.1 christos // box or the last free edge of that box
225 1.1 christos int ALGOR::find_bad_turn(size_t& y, size_t& x, int& dir, BOARD& b, int last)
226 1.1 christos {
227 1.1 christos RANDOM rdy(b.ny()), rdx(b.nx());
228 1.1 christos for (y = rdy(); y < b.ny(); y = rdy()) {
229 1.1 christos rdx.clear();
230 1.1 christos for (x = rdx(); x < b.nx(); x = rdx()) {
231 1.1 christos BOX box(y, x, b);
232 1.1 christos if ((4 - box.count()) > last &&
233 1.1 christos try_bad_turn(box, y, x, dir, b, last))
234 1.1 christos return 1;
235 1.1 christos }
236 1.1 christos }
237 1.1 christos return 0;
238 1.1 christos }
239 1.1 christos
240 1.1 christos int ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b,
241 1.1 christos int last)
242 1.1 christos {
243 1.1 christos BOARD nb(b);
244 1.1 christos int tdir, mindir = -1, xdir, mv;
245 1.1 christos // number of boxes per closure
246 1.1 christos size_t nbox, minbox = nb.nx() * nb.ny() + 1;
247 1.1 christos size_t tx, ty, minx = ~0, miny = ~0;
248 1.1 christos
249 1.1 christos while (find_bad_turn(ty, tx, tdir, nb, last)) {
250 1.1 christos
251 1.1 christos // Play a bad move that would cause the opponent's closure
252 1.1 christos if ((mv = nb.domove(ty, tx, tdir, getWho())) != 0)
253 1.1 christos b.abort("find_min_closure1: Invalid move %d (%d, %d, %d)", mv,
254 1.1 christos ty, tx, tdir);
255 1.1 christos
256 1.1 christos // Count the opponent's closure
257 1.1 christos if ((nbox = count_closure(y, x, xdir, nb)) == 0)
258 1.1 christos b.abort("find_min_closure1: no closure found");
259 1.1 christos
260 1.1 christos if (nbox <= minbox) {
261 1.1 christos // This closure has fewer boxes
262 1.1 christos minbox = nbox;
263 1.1 christos minx = tx;
264 1.1 christos miny = ty;
265 1.1 christos mindir = tdir;
266 1.1 christos }
267 1.1 christos }
268 1.1 christos
269 1.1 christos y = miny;
270 1.1 christos x = minx;
271 1.1 christos dir = mindir;
272 1.1 christos return minbox;
273 1.1 christos }
274 1.1 christos
275 1.1 christos
276 1.1 christos // Search for the move that makes the opponent close the least number of
277 1.1 christos // boxes; returns 1 if a move found, 0 otherwise
278 1.1 christos int ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
279 1.1 christos {
280 1.1 christos size_t x1, y1;
281 1.1 christos int dir1;
282 1.1 christos int count = b.ny() * b.nx() + 1, count1;
283 1.1 christos
284 1.1 christos for (size_t i = 0; i < 3; i++)
285 1.1 christos if (count > (count1 = find_min_closure1(y1, x1, dir1, b, i))) {
286 1.1 christos count = count1;
287 1.1 christos y = y1;
288 1.1 christos x = x1;
289 1.1 christos dir = dir1;
290 1.1 christos }
291 1.1 christos
292 1.1 christos return (size_t) count != b.ny() * b.nx() + 1;
293 1.1 christos }
294 1.1 christos
295 1.1 christos // Return a move in (y, x, dir)
296 1.1 christos void ALGOR::play(const BOARD& b, size_t& y, size_t& x, int& dir)
297 1.1 christos {
298 1.1 christos // See if we can close the largest closure available
299 1.1 christos if (find_max_closure(y, x, dir, b))
300 1.1 christos return;
301 1.1 christos
302 1.1 christos #ifdef notyet
303 1.1 christos size_t sgl = find_single();
304 1.1 christos size_t dbl = find_double();
305 1.1 christos #endif
306 1.1 christos
307 1.1 christos // See if we can play an edge without giving the opponent a box
308 1.1 christos if (find_good_turn(y, x, dir, b))
309 1.1 christos return;
310 1.1 christos
311 1.1 christos // Too bad, find the move that gives the opponent the fewer boxes
312 1.1 christos if (find_min_closure(y, x, dir, b))
313 1.1 christos return;
314 1.1 christos }
315