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