[BACK]Return to algor.cc CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / dab

File: [cvs.NetBSD.org] / src / games / dab / algor.cc (download)

Revision 1.5, Wed Feb 29 23:39:53 2012 UTC (12 years, 1 month ago) by joerg
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, is-mlppp-base, is-mlppp, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver
Changes since 1.4: +7 -7 lines

Fix various format string mismatches.

/*	$NetBSD: algor.cc,v 1.5 2012/02/29 23:39:53 joerg Exp $	*/

/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Christos Zoulas.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * algor.C: Computer algorithm
 */
#include "defs.h"
RCSID("$NetBSD: algor.cc,v 1.5 2012/02/29 23:39:53 joerg Exp $")

#include "algor.h"
#include "board.h"
#include "box.h"
#include "random.h"

ALGOR::ALGOR(const char c) : PLAYER(c)
{
#ifdef notyet
    // Single Edges = (x + y) * 2
    _edge1 = (_b.nx() * _b.ny()) * 2;
    // Shared Edges = (x * (y - 1)) + ((x - 1) * y)
    _edge2 = (_b.nx() * (_b.ny() - 1)) + ((_b.nx() - 1) * _b.ny());
    // Maximum Edges filled before closure = x * y * 2
    _maxedge = _b.nx() * _b.ny() * 2;
#endif
}

// Find the first closure, i.e. a box that has 3 edges
int ALGOR::find_closure(size_t& y, size_t& x, int& dir, BOARD& b)
{
    RANDOM rdy(b.ny()), rdx(b.nx());

    for (y = rdy(); y < b.ny(); y = rdy()) {
	rdx.clear();
	for (x = rdx(); x < b.nx(); x = rdx()) {
	    BOX box(y, x, b);
	    if (box.count() == 3) {
		for (dir = BOX::first; dir < BOX::last; dir++)
		    if (!box.isset(dir))
			return 1;
		b.abort("find_closure: 3 sided box[%zu,%zu] has no free sides",
			y, x);
	    }
	}
    }
    return 0;
}

#if 0
size_t ALGOR::find_single()
{
    size_t ne;

    // Find the number of single edges in use
    for (size_t x = 0; x < b.nx(); x++) {
	BOX tbox(0, x, b);
	ne += tbox.isset(BOX::top);
	BOX bbox(b.ny() - 1, x, b);
	ne += bbox.isset(BOX::bottom);
    }
    for (size_t y = 0; y < _b.ny(); y++) {
	BOX lbox(y, 0, b);
	ne += lbox.isset(BOX::left);
	BOX rbox(y,_b.nx() - 1, b);
	ne += rbox.isset(BOX::right);
    }
    return ne;
}
#endif


// Count a closure, by counting all boxes that we can close in the current
// move
size_t ALGOR::count_closure(size_t& y, size_t& x, int& dir, BOARD& b)
{
    size_t i = 0;
    size_t tx, ty;
    int tdir, mv;

    while (find_closure(ty, tx, tdir, b)) {
	if (i == 0) {
	    // Mark the beginning of the closure
	    x = tx;
	    y = ty;
	    dir = tdir;
	}
	if ((mv = b.domove(ty, tx, tdir, getWho())) == -1)
	    b.abort("count_closure: Invalid move (%zu, %zu, %d)", y, x, dir);
	else
	    i += mv;
    }
    return i;
}


/*
 * Find the largest closure, by closing all possible closures.
 * return the number of boxes closed in the maximum closure,
 * and the first box of the maximum closure in (x, y, dir)
 */
size_t ALGOR::find_max_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
{
    BOARD nb(b);
    int maxdir = -1;
    size_t nbox, maxbox = 0;
    size_t maxx = ~0, maxy = ~0;
    size_t tx = 0, ty = 0;	/* XXX: GCC */
    int tdir = 0;		/* XXX: GCC */

    while ((nbox = count_closure(ty, tx, tdir, nb)) != 0)
	if (nbox > maxbox) {
	    // This closure is better, update max
	    maxbox = nbox;
	    maxx = tx;
	    maxy = ty;
	    maxdir = tdir;
	}

    // Return the max found
    y = maxy;
    x = maxx;
    dir = maxdir;
    return maxbox;
}


// Find if a turn does not result in a capture on the given box
// and return the direction if found.
int ALGOR::try_good_turn(BOX& box, size_t y, size_t x, int& dir, BOARD& b)
{
    // Sanity check; we must have a good box
    if (box.count() >= 2)
	b.abort("try_good_turn: box[%zu,%zu] has more than 2 sides occupied",
		y, x);

    // Make sure we don't make a closure in an adjacent box.
    // We use a random direction to randomize the game
    RANDOM rd(BOX::last);
    for (dir = rd(); dir < BOX::last; dir = rd())
	if (!box.isset(dir)) {
	    size_t by = y + BOX::edges[dir].y;
	    size_t bx = x + BOX::edges[dir].x;
	    if (!b.bounds(by, bx))
		return 1;

	    BOX nbox(by, bx, b);
	    if (nbox.count() < 2)
		return 1;
	}

    return 0;
}


// Try to find a turn that does not result in an opponent closure, and
// return it in (x, y, dir); if not found return 0.
int ALGOR::find_good_turn(size_t& y, size_t& x, int& dir, const BOARD& b)
{
    BOARD nb(b);
    RANDOM rdy(b.ny()), rdx(b.nx());

    for (y = rdy(); y < b.ny(); y = rdy()) {
	rdx.clear();
	for (x = rdx(); x < b.nx(); x = rdx()) {
	    BOX box(y, x, nb);
	    if (box.count() < 2 && try_good_turn(box, y, x, dir, nb))
		return 1;
	}
    }
    return 0;
}

// On a box with 2 edges, return the first or the last free edge, depending
// on the order specified
int ALGOR::try_bad_turn(BOX& box, size_t& y, size_t& x, int& dir, BOARD& b,
			int last)
{
    if (4 - box.count() <= last)
	b.abort("try_bad_turn: Called at [%zu,%zu] for %d with %d",
		y, x, last, box.count());
    for (dir = BOX::first; dir < BOX::last; dir++)
	if (!box.isset(dir)) {
	    if (!last)
		return 1;
	    else
		last--;
	}
    return 0;
}

// Find a box that has 2 edges and return the first free edge of that
// box or the last free edge of that box
int ALGOR::find_bad_turn(size_t& y, size_t& x, int& dir, BOARD& b, int last)
{
    RANDOM rdy(b.ny()), rdx(b.nx());
    for (y = rdy(); y < b.ny(); y = rdy()) {
	rdx.clear();
	for (x = rdx(); x < b.nx(); x = rdx()) {
	    BOX box(y, x, b);
	    if ((4 - box.count()) > last &&
		try_bad_turn(box, y, x, dir, b, last))
		return 1;
	}
    }
    return 0;
}

size_t ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b,
    int last)
{
    BOARD nb(b);
    int tdir, mindir = -1, mv;
    // number of boxes per closure
    size_t nbox, minbox = nb.nx() * nb.ny() + 1;
    size_t tx, ty, minx = ~0, miny = ~0;
    int xdir = 0;	/* XXX: GCC */

    while (find_bad_turn(ty, tx, tdir, nb, last)) {

        // Play a bad move that would cause the opponent's closure
	if ((mv = nb.domove(ty, tx, tdir, getWho())) != 0)
	    b.abort("find_min_closure1: Invalid move %d (%zu, %zu, %d)", mv,
		    ty, tx, tdir);

        // Count the opponent's closure
	if ((nbox = count_closure(y, x, xdir, nb)) == 0)
	    b.abort("find_min_closure1: no closure found");

	if (nbox <= minbox) {
	    // This closure has fewer boxes
	    minbox = nbox;
	    minx = tx;
	    miny = ty;
	    mindir = tdir;
	}
    }

    y = miny;
    x = minx;
    dir = mindir;
    return minbox;
}


// Search for the move that makes the opponent close the least number of
// boxes; returns 1 if a move found, 0 otherwise
size_t ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
{
    size_t x1, y1;
    int dir1;
    size_t count = b.ny() * b.nx() + 1, count1;

    for (size_t i = 0; i < 3; i++)
	if (count > (count1 = find_min_closure1(y1, x1, dir1, b, i))) {
	    count = count1;
	    y = y1;
	    x = x1;
	    dir = dir1;
	}

    return count != b.ny() * b.nx() + 1;
}

// Return a move in (y, x, dir)
void ALGOR::play(const BOARD& b, size_t& y, size_t& x, int& dir)
{
    // See if we can close the largest closure available
    if (find_max_closure(y, x, dir, b))
	return;

#ifdef notyet
    size_t sgl = find_single();
    size_t dbl = find_double();
#endif

    // See if we can play an edge without giving the opponent a box
    if (find_good_turn(y, x, dir, b))
	return;

    // Too bad, find the move that gives the opponent the fewer boxes
    if (find_min_closure(y, x, dir, b))
	return;
}