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