algor.cc revision 1.3 1 1.3 christos /* $NetBSD: algor.cc,v 1.3 2006/05/14 03:20:42 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.3 christos RCSID("$NetBSD: algor.cc,v 1.3 2006/05/14 03:20:42 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.2 christos size_t 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.3 christos int maxdir = -1;
139 1.1 christos size_t nbox, maxbox = 0;
140 1.3 christos size_t maxx = ~0, maxy = ~0;
141 1.3 christos size_t tx = 0, ty = 0; /* XXX: GCC */
142 1.3 christos int tdir = 0; /* XXX: GCC */
143 1.1 christos
144 1.1 christos while ((nbox = count_closure(ty, tx, tdir, nb)) != 0)
145 1.1 christos if (nbox > maxbox) {
146 1.1 christos // This closure is better, update max
147 1.1 christos maxbox = nbox;
148 1.1 christos maxx = tx;
149 1.1 christos maxy = ty;
150 1.1 christos maxdir = tdir;
151 1.1 christos }
152 1.1 christos
153 1.1 christos // Return the max found
154 1.1 christos y = maxy;
155 1.1 christos x = maxx;
156 1.1 christos dir = maxdir;
157 1.1 christos return maxbox;
158 1.1 christos }
159 1.1 christos
160 1.1 christos
161 1.1 christos // Find if a turn does not result in a capture on the given box
162 1.1 christos // and return the direction if found.
163 1.1 christos int ALGOR::try_good_turn(BOX& box, size_t y, size_t x, int& dir, BOARD& b)
164 1.1 christos {
165 1.1 christos // Sanity check; we must have a good box
166 1.1 christos if (box.count() >= 2)
167 1.1 christos b.abort("try_good_turn: box[%d,%d] has more than 2 sides occupied",
168 1.1 christos y, x);
169 1.1 christos
170 1.1 christos // Make sure we don't make a closure in an adjacent box.
171 1.1 christos // We use a random direction to randomize the game
172 1.1 christos RANDOM rd(BOX::last);
173 1.1 christos for (dir = rd(); dir < BOX::last; dir = rd())
174 1.1 christos if (!box.isset(dir)) {
175 1.1 christos size_t by = y + BOX::edges[dir].y;
176 1.1 christos size_t bx = x + BOX::edges[dir].x;
177 1.1 christos if (!b.bounds(by, bx))
178 1.1 christos return 1;
179 1.1 christos
180 1.1 christos BOX nbox(by, bx, b);
181 1.1 christos if (nbox.count() < 2)
182 1.1 christos return 1;
183 1.1 christos }
184 1.1 christos
185 1.1 christos return 0;
186 1.1 christos }
187 1.1 christos
188 1.1 christos
189 1.1 christos // Try to find a turn that does not result in an opponent closure, and
190 1.1 christos // return it in (x, y, dir); if not found return 0.
191 1.1 christos int ALGOR::find_good_turn(size_t& y, size_t& x, int& dir, const BOARD& b)
192 1.1 christos {
193 1.1 christos BOARD nb(b);
194 1.1 christos RANDOM rdy(b.ny()), rdx(b.nx());
195 1.1 christos
196 1.1 christos for (y = rdy(); y < b.ny(); y = rdy()) {
197 1.1 christos rdx.clear();
198 1.1 christos for (x = rdx(); x < b.nx(); x = rdx()) {
199 1.1 christos BOX box(y, x, nb);
200 1.1 christos if (box.count() < 2 && try_good_turn(box, y, x, dir, nb))
201 1.1 christos return 1;
202 1.1 christos }
203 1.1 christos }
204 1.1 christos return 0;
205 1.1 christos }
206 1.1 christos
207 1.1 christos // On a box with 2 edges, return the first or the last free edge, depending
208 1.1 christos // on the order specified
209 1.1 christos int ALGOR::try_bad_turn(BOX& box, size_t& y, size_t& x, int& dir, BOARD& b,
210 1.1 christos int last)
211 1.1 christos {
212 1.1 christos if (4 - box.count() <= last)
213 1.1 christos b.abort("try_bad_turn: Called at [%d,%d] for %d with %d",
214 1.1 christos y, x, last, box.count());
215 1.1 christos for (dir = BOX::first; dir < BOX::last; dir++)
216 1.1 christos if (!box.isset(dir)) {
217 1.1 christos if (!last)
218 1.1 christos return 1;
219 1.1 christos else
220 1.1 christos last--;
221 1.1 christos }
222 1.1 christos return 0;
223 1.1 christos }
224 1.1 christos
225 1.1 christos // Find a box that has 2 edges and return the first free edge of that
226 1.1 christos // box or the last free edge of that box
227 1.1 christos int ALGOR::find_bad_turn(size_t& y, size_t& x, int& dir, BOARD& b, int last)
228 1.1 christos {
229 1.1 christos RANDOM rdy(b.ny()), rdx(b.nx());
230 1.1 christos for (y = rdy(); y < b.ny(); y = rdy()) {
231 1.1 christos rdx.clear();
232 1.1 christos for (x = rdx(); x < b.nx(); x = rdx()) {
233 1.1 christos BOX box(y, x, b);
234 1.1 christos if ((4 - box.count()) > last &&
235 1.1 christos try_bad_turn(box, y, x, dir, b, last))
236 1.1 christos return 1;
237 1.1 christos }
238 1.1 christos }
239 1.1 christos return 0;
240 1.1 christos }
241 1.1 christos
242 1.2 christos size_t ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b,
243 1.2 christos int last)
244 1.1 christos {
245 1.1 christos BOARD nb(b);
246 1.3 christos int tdir, mindir = -1, mv;
247 1.1 christos // number of boxes per closure
248 1.1 christos size_t nbox, minbox = nb.nx() * nb.ny() + 1;
249 1.1 christos size_t tx, ty, minx = ~0, miny = ~0;
250 1.3 christos int xdir = 0; /* XXX: GCC */
251 1.1 christos
252 1.1 christos while (find_bad_turn(ty, tx, tdir, nb, last)) {
253 1.1 christos
254 1.1 christos // Play a bad move that would cause the opponent's closure
255 1.1 christos if ((mv = nb.domove(ty, tx, tdir, getWho())) != 0)
256 1.1 christos b.abort("find_min_closure1: Invalid move %d (%d, %d, %d)", mv,
257 1.1 christos ty, tx, tdir);
258 1.1 christos
259 1.1 christos // Count the opponent's closure
260 1.1 christos if ((nbox = count_closure(y, x, xdir, nb)) == 0)
261 1.1 christos b.abort("find_min_closure1: no closure found");
262 1.1 christos
263 1.1 christos if (nbox <= minbox) {
264 1.1 christos // This closure has fewer boxes
265 1.1 christos minbox = nbox;
266 1.1 christos minx = tx;
267 1.1 christos miny = ty;
268 1.1 christos mindir = tdir;
269 1.1 christos }
270 1.1 christos }
271 1.1 christos
272 1.1 christos y = miny;
273 1.1 christos x = minx;
274 1.1 christos dir = mindir;
275 1.1 christos return minbox;
276 1.1 christos }
277 1.1 christos
278 1.1 christos
279 1.1 christos // Search for the move that makes the opponent close the least number of
280 1.1 christos // boxes; returns 1 if a move found, 0 otherwise
281 1.2 christos size_t ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
282 1.1 christos {
283 1.1 christos size_t x1, y1;
284 1.1 christos int dir1;
285 1.2 christos size_t count = b.ny() * b.nx() + 1, count1;
286 1.1 christos
287 1.1 christos for (size_t i = 0; i < 3; i++)
288 1.1 christos if (count > (count1 = find_min_closure1(y1, x1, dir1, b, i))) {
289 1.1 christos count = count1;
290 1.1 christos y = y1;
291 1.1 christos x = x1;
292 1.1 christos dir = dir1;
293 1.1 christos }
294 1.1 christos
295 1.2 christos return count != b.ny() * b.nx() + 1;
296 1.1 christos }
297 1.1 christos
298 1.1 christos // Return a move in (y, x, dir)
299 1.1 christos void ALGOR::play(const BOARD& b, size_t& y, size_t& x, int& dir)
300 1.1 christos {
301 1.1 christos // See if we can close the largest closure available
302 1.1 christos if (find_max_closure(y, x, dir, b))
303 1.1 christos return;
304 1.1 christos
305 1.1 christos #ifdef notyet
306 1.1 christos size_t sgl = find_single();
307 1.1 christos size_t dbl = find_double();
308 1.1 christos #endif
309 1.1 christos
310 1.1 christos // See if we can play an edge without giving the opponent a box
311 1.1 christos if (find_good_turn(y, x, dir, b))
312 1.1 christos return;
313 1.1 christos
314 1.1 christos // Too bad, find the move that gives the opponent the fewer boxes
315 1.1 christos if (find_min_closure(y, x, dir, b))
316 1.1 christos return;
317 1.1 christos }
318