From 65c59fa0ed298c0bb306d7b6f81bc9d14cefa5b8 Mon Sep 17 00:00:00 2001 From: christos Date: Sat, 27 Dec 2003 01:16:55 +0000 Subject: Rename .C files to .cc --- dab/Makefile | 6 +- dab/algor.C | 314 ------------------------------------------------------ dab/algor.cc | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ dab/board.C | 260 -------------------------------------------- dab/board.cc | 260 ++++++++++++++++++++++++++++++++++++++++++++ dab/box.C | 157 --------------------------- dab/box.cc | 157 +++++++++++++++++++++++++++ dab/gamescreen.C | 50 --------- dab/gamescreen.cc | 50 +++++++++ dab/human.C | 151 -------------------------- dab/human.cc | 151 ++++++++++++++++++++++++++ dab/main.C | 196 ---------------------------------- dab/main.cc | 196 ++++++++++++++++++++++++++++++++++ dab/player.C | 98 ----------------- dab/player.cc | 98 +++++++++++++++++ dab/random.C | 86 --------------- dab/random.cc | 86 +++++++++++++++ dab/test.C | 57 ---------- dab/test.cc | 57 ++++++++++ dab/ttyscrn.C | 235 ---------------------------------------- dab/ttyscrn.cc | 235 ++++++++++++++++++++++++++++++++++++++++ 21 files changed, 1607 insertions(+), 1607 deletions(-) delete mode 100644 dab/algor.C create mode 100644 dab/algor.cc delete mode 100644 dab/board.C create mode 100644 dab/board.cc delete mode 100644 dab/box.C create mode 100644 dab/box.cc delete mode 100644 dab/gamescreen.C create mode 100644 dab/gamescreen.cc delete mode 100644 dab/human.C create mode 100644 dab/human.cc delete mode 100644 dab/main.C create mode 100644 dab/main.cc delete mode 100644 dab/player.C create mode 100644 dab/player.cc delete mode 100644 dab/random.C create mode 100644 dab/random.cc delete mode 100644 dab/test.C create mode 100644 dab/test.cc delete mode 100644 dab/ttyscrn.C create mode 100644 dab/ttyscrn.cc (limited to 'dab') diff --git a/dab/Makefile b/dab/Makefile index ed03c89c..d11f7d52 100644 --- a/dab/Makefile +++ b/dab/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.1.1.1 2003/12/26 17:57:02 christos Exp $ +# $NetBSD: Makefile,v 1.2 2003/12/27 01:16:55 christos Exp $ WARNS=2 DPADD+=${LIBSTDCPP} ${LIBCURSES} ${LIBTERMCAP} ${LIBM} @@ -6,7 +6,7 @@ LDADD+=-lstdc++ -lcurses -ltermcap -lm PROG=dab MAN=dab.6 -SRCS=algor.C board.C main.C human.C box.C player.C gamescreen.C \ - ttyscrn.C random.C +SRCS=algor.cc board.cc main.cc human.cc box.cc player.cc gamescreen.cc \ + ttyscrn.cc random.cc .include diff --git a/dab/algor.C b/dab/algor.C deleted file mode 100644 index ecd8ee08..00000000 --- a/dab/algor.C +++ /dev/null @@ -1,314 +0,0 @@ -/* $NetBSD: algor.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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.C,v 1.2 2003/12/26 18:03:34 christos 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[%d,%d] 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 (%d, %d, %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) - */ -int ALGOR::find_max_closure(size_t& y, size_t& x, int& dir, const BOARD& b) -{ - BOARD nb(b); - int tdir, maxdir = -1; - size_t nbox, maxbox = 0; - size_t tx, ty, maxx = ~0, maxy = ~0; - - 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[%d,%d] 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 [%d,%d] 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; -} - -int ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b, - int last) -{ - BOARD nb(b); - int tdir, mindir = -1, xdir, mv; - // number of boxes per closure - size_t nbox, minbox = nb.nx() * nb.ny() + 1; - size_t tx, ty, minx = ~0, miny = ~0; - - 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 (%d, %d, %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 -int ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b) -{ - size_t x1, y1; - int dir1; - int 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 (size_t) 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; -} diff --git a/dab/algor.cc b/dab/algor.cc new file mode 100644 index 00000000..b84a3bda --- /dev/null +++ b/dab/algor.cc @@ -0,0 +1,314 @@ +/* $NetBSD: algor.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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.1 2003/12/27 01:16:55 christos 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[%d,%d] 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 (%d, %d, %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) + */ +int ALGOR::find_max_closure(size_t& y, size_t& x, int& dir, const BOARD& b) +{ + BOARD nb(b); + int tdir, maxdir = -1; + size_t nbox, maxbox = 0; + size_t tx, ty, maxx = ~0, maxy = ~0; + + 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[%d,%d] 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 [%d,%d] 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; +} + +int ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b, + int last) +{ + BOARD nb(b); + int tdir, mindir = -1, xdir, mv; + // number of boxes per closure + size_t nbox, minbox = nb.nx() * nb.ny() + 1; + size_t tx, ty, minx = ~0, miny = ~0; + + 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 (%d, %d, %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 +int ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b) +{ + size_t x1, y1; + int dir1; + int 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 (size_t) 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; +} diff --git a/dab/board.C b/dab/board.C deleted file mode 100644 index ff265e9d..00000000 --- a/dab/board.C +++ /dev/null @@ -1,260 +0,0 @@ -/* $NetBSD: board.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * board.C: Board manipulations - */ -#include "defs.h" -RCSID("$NetBSD: board.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include -#include -#include -#include "board.h" -#include "gamescreen.h" -#include "box.h" -#include "player.h" - -BOARD::BOARD(size_t y, size_t x, GAMESCREEN* scrn) : - _ny(y), - _nx(x), - _scrn(scrn) -{ - _ty = 2 * _ny + 1; - _tx = 2 * _nx + 1; - - _b = new int*[_ty]; - - for (y = 0; y < _ty; y++) - _b[y] = new int[_tx]; - - init(); -} - -BOARD::BOARD(const BOARD& b) : - _ty(b._ty), - _tx(b._tx), - _ny(b._ny), - _nx(b._nx), - _scrn(NULL) -{ - _b = new int*[_ty]; - - for (size_t y = 0; y < _ty; y++) { - _b[y] = new int[_tx]; - (void) memcpy(_b[y], b._b[y], _tx * sizeof(int)); - } -} - -BOARD::~BOARD() -{ - size_t y; - - for (y = 0; y < _ty; y++) - delete[] _b[y]; - - delete[] _b; -} - -// Clear all boxes and reset state for a new game -void BOARD::init(void) -{ - size_t x, y; - - for (y = 0; y < _ny; y++) - for (x = 0; x < _nx; x++) { - BOX box(y, x, *this); - box.reset(); - } -} - -/* - * Make a move for player with initial 'c', adding an edge at box(x, y) - * and the specified direction. - * returns: - * -1: Invalid move - * n: Number of closures n E [0..2] - */ -int BOARD::domove(size_t y, size_t x, int dir, char c) -{ - int closed = 0; - - // Check if out of bounds - if (!bounds(y, x)) - return -1; - - BOX box1(y, x, *this); - - // Check if the edge is already there - if (box1.isset(dir)) - return -1; - - box1.set(dir); - - if (box1.count() == 4) { - // New box; name it and count it - box1.name() = c; - closed++; - } - - box1.paint(); - - // Check other box - x += BOX::edges[dir].x; - y += BOX::edges[dir].y; - - if (bounds(y, x)) { - BOX box2(y, x, *this); - if (box2.count() == 4) { - box2.name() = c; - box2.paint(); - closed++; - } - } - return closed; -} - -// Return true if the board is full -int BOARD::full(void) const -{ - for (size_t y = 0; y < _ny; y++) - for (size_t x = 0; x < _nx; x++) { - BOX box(y, x, (BOARD&) *this); - if (box.count() != 4) - return 0; - } - return 1; -} - -// Return if the coordinates are within bounds; we don't check for < 0, -// since size_t is unsigned -int BOARD::bounds(size_t y, size_t x) const -{ - return x < _nx && y < _ny; -} - -// Paint all boxes, effectively redrawing the board -void BOARD::paint(void) const -{ - for (size_t y = 0; y < _ny; y++) - for (size_t x = 0; x < _nx; x++) { - BOX box(y, x, (BOARD&) *this); - box.paint(); - } -} - -// Clear the screen -void BOARD::clean(void) const -{ - if (!_scrn) - return; - _scrn->clean(); -} - -// Move cursor to x, y -void BOARD::setpos(size_t y, size_t x) const -{ - if (!_scrn) - return; - _scrn->moveto(y, x); - _scrn->redraw(); -} - -// Return character indicating move -int BOARD::getmove(void) const -{ - if (!_scrn) - return 'q'; - _scrn->redraw(); - return _scrn->getinput(); -} - -// Ring the bell -void BOARD::bell(void) const -{ - if (!_scrn) - return; - _scrn->bell(); -} - -// Post the score in the current game for player i -void BOARD::score(size_t i, const PLAYER& p) -{ - if (_scrn == NULL) - return; - _scrn->score(i, p); -} - -// Post the number of games won for player i -void BOARD::games(size_t i, const PLAYER& p) -{ - if (_scrn == NULL) - return; - _scrn->games(i, p); -} - -// Post the total score for player i -void BOARD::total(size_t i, const PLAYER& p) -{ - if (_scrn == NULL) - return; - _scrn->total(i, p); -} - -// Post the total score for player i -void BOARD::ties(const PLAYER& p) -{ - if (_scrn == NULL) - return; - _scrn->ties(p); -} - -// Internal algorithm error; post and abort -void BOARD::abort(const char* s, ...) const -{ - for (size_t i = 0; i < _ny; i++) - fprintf(stderr, "\n"); - - va_list ap; - fprintf(stderr, "Algorithm internal error: "); - va_start(ap, s); - vfprintf(stderr, s, ap); - va_end(ap); - fprintf(stderr, "\n"); - ::abort(); -} diff --git a/dab/board.cc b/dab/board.cc new file mode 100644 index 00000000..6cd3e957 --- /dev/null +++ b/dab/board.cc @@ -0,0 +1,260 @@ +/* $NetBSD: board.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * board.C: Board manipulations + */ +#include "defs.h" +RCSID("$NetBSD: board.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include +#include +#include +#include "board.h" +#include "gamescreen.h" +#include "box.h" +#include "player.h" + +BOARD::BOARD(size_t y, size_t x, GAMESCREEN* scrn) : + _ny(y), + _nx(x), + _scrn(scrn) +{ + _ty = 2 * _ny + 1; + _tx = 2 * _nx + 1; + + _b = new int*[_ty]; + + for (y = 0; y < _ty; y++) + _b[y] = new int[_tx]; + + init(); +} + +BOARD::BOARD(const BOARD& b) : + _ty(b._ty), + _tx(b._tx), + _ny(b._ny), + _nx(b._nx), + _scrn(NULL) +{ + _b = new int*[_ty]; + + for (size_t y = 0; y < _ty; y++) { + _b[y] = new int[_tx]; + (void) memcpy(_b[y], b._b[y], _tx * sizeof(int)); + } +} + +BOARD::~BOARD() +{ + size_t y; + + for (y = 0; y < _ty; y++) + delete[] _b[y]; + + delete[] _b; +} + +// Clear all boxes and reset state for a new game +void BOARD::init(void) +{ + size_t x, y; + + for (y = 0; y < _ny; y++) + for (x = 0; x < _nx; x++) { + BOX box(y, x, *this); + box.reset(); + } +} + +/* + * Make a move for player with initial 'c', adding an edge at box(x, y) + * and the specified direction. + * returns: + * -1: Invalid move + * n: Number of closures n E [0..2] + */ +int BOARD::domove(size_t y, size_t x, int dir, char c) +{ + int closed = 0; + + // Check if out of bounds + if (!bounds(y, x)) + return -1; + + BOX box1(y, x, *this); + + // Check if the edge is already there + if (box1.isset(dir)) + return -1; + + box1.set(dir); + + if (box1.count() == 4) { + // New box; name it and count it + box1.name() = c; + closed++; + } + + box1.paint(); + + // Check other box + x += BOX::edges[dir].x; + y += BOX::edges[dir].y; + + if (bounds(y, x)) { + BOX box2(y, x, *this); + if (box2.count() == 4) { + box2.name() = c; + box2.paint(); + closed++; + } + } + return closed; +} + +// Return true if the board is full +int BOARD::full(void) const +{ + for (size_t y = 0; y < _ny; y++) + for (size_t x = 0; x < _nx; x++) { + BOX box(y, x, (BOARD&) *this); + if (box.count() != 4) + return 0; + } + return 1; +} + +// Return if the coordinates are within bounds; we don't check for < 0, +// since size_t is unsigned +int BOARD::bounds(size_t y, size_t x) const +{ + return x < _nx && y < _ny; +} + +// Paint all boxes, effectively redrawing the board +void BOARD::paint(void) const +{ + for (size_t y = 0; y < _ny; y++) + for (size_t x = 0; x < _nx; x++) { + BOX box(y, x, (BOARD&) *this); + box.paint(); + } +} + +// Clear the screen +void BOARD::clean(void) const +{ + if (!_scrn) + return; + _scrn->clean(); +} + +// Move cursor to x, y +void BOARD::setpos(size_t y, size_t x) const +{ + if (!_scrn) + return; + _scrn->moveto(y, x); + _scrn->redraw(); +} + +// Return character indicating move +int BOARD::getmove(void) const +{ + if (!_scrn) + return 'q'; + _scrn->redraw(); + return _scrn->getinput(); +} + +// Ring the bell +void BOARD::bell(void) const +{ + if (!_scrn) + return; + _scrn->bell(); +} + +// Post the score in the current game for player i +void BOARD::score(size_t i, const PLAYER& p) +{ + if (_scrn == NULL) + return; + _scrn->score(i, p); +} + +// Post the number of games won for player i +void BOARD::games(size_t i, const PLAYER& p) +{ + if (_scrn == NULL) + return; + _scrn->games(i, p); +} + +// Post the total score for player i +void BOARD::total(size_t i, const PLAYER& p) +{ + if (_scrn == NULL) + return; + _scrn->total(i, p); +} + +// Post the total score for player i +void BOARD::ties(const PLAYER& p) +{ + if (_scrn == NULL) + return; + _scrn->ties(p); +} + +// Internal algorithm error; post and abort +void BOARD::abort(const char* s, ...) const +{ + for (size_t i = 0; i < _ny; i++) + fprintf(stderr, "\n"); + + va_list ap; + fprintf(stderr, "Algorithm internal error: "); + va_start(ap, s); + vfprintf(stderr, s, ap); + va_end(ap); + fprintf(stderr, "\n"); + ::abort(); +} diff --git a/dab/box.C b/dab/box.C deleted file mode 100644 index d969bfe3..00000000 --- a/dab/box.C +++ /dev/null @@ -1,157 +0,0 @@ -/* $NetBSD: box.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * box.C: Box computations - */ -#include "defs.h" -RCSID("$NetBSD: box.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include "box.h" -#include "board.h" -#include "gamescreen.h" -#include - -const POINT BOX::edges[BOX::last] = - { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } }; -const POINT BOX::corners[BOX::last] = - { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; -const int BOX::syms[BOX::last] = - { GAMESCREEN::GS_HLINE, GAMESCREEN::GS_HLINE, - GAMESCREEN::GS_VLINE, GAMESCREEN::GS_VLINE }; - -BOX::BOX(size_t py, size_t px, BOARD& b) : - _b(b) -{ - _centery = py * 2 + 1; - _centerx = px * 2 + 1; -} - -void BOX::addcorner(size_t y, size_t x) -{ - char sym; - _b.getScrn()->moveto(y, x); - if (x == 0) { - if (y == 0) - sym = GAMESCREEN::GS_ULCORNER; - else if (y == _b.ty() - 1) - sym = GAMESCREEN::GS_LLCORNER; - else - sym = GAMESCREEN::GS_LTEE; - } else if (x == _b.tx() - 1) { - if (y == 0) - sym = GAMESCREEN::GS_URCORNER; - else if (y == _b.ty() - 1) - sym = GAMESCREEN::GS_LRCORNER; - else - sym = GAMESCREEN::GS_RTEE; - } else if (y == 0) - sym = GAMESCREEN::GS_TTEE; - else if (y == _b.ty() - 1) - sym = GAMESCREEN::GS_BTEE; - else - sym = GAMESCREEN::GS_PLUS; - - _b.getScrn()->addedge(sym); -} - -// Paint a box -void BOX::paint(void) -{ - int e; - if (_b.getScrn() == NULL) - return; - - _b.getScrn()->moveto(_centery, _centerx); - _b.getScrn()->addsym(name()); - - for (e = BOX::first; e < BOX::last; e++) { - addcorner(_centery + corners[e].y, _centerx + corners[e].x); - _b.getScrn()->moveto(_centery + edges[e].y, _centerx + edges[e].x); - _b.getScrn()->addedge(edge((EDGE) e)); - } - _b.getScrn()->redraw(); -} - -// Return the name -int& BOX::name(void) -{ - return _b.data(_centery, _centerx); -} - -// Set an edge -void BOX::set(int e) -{ - _b.data(_centery + edges[e].y, _centerx + edges[e].x) = syms[e]; -} - -// Clear an edge -void BOX::clr(int e) -{ - _b.data(_centery + edges[e].y, _centerx + edges[e].x) = ' '; -} - -// Test an edge -int BOX::isset(int e) const -{ - return _b.data(_centery + edges[e].y, _centerx + edges[e].x) != ' '; -} - -// Return the edge -int& BOX::edge(int e) -{ - return _b.data(_centery + edges[e].y, _centerx + edges[e].x); -} - -// Count the number of edges set in the box -int BOX::count(void) const -{ - int cnt = 0; - - for (int e = BOX::first; e < BOX::last; e++) - cnt += isset((EDGE) e); - return cnt; -} - -// Clear the box -void BOX::reset(void) -{ - for (int e = BOX::first; e < BOX::last; e++) - clr((EDGE) e); - name() = ' '; -} diff --git a/dab/box.cc b/dab/box.cc new file mode 100644 index 00000000..ed9cadbc --- /dev/null +++ b/dab/box.cc @@ -0,0 +1,157 @@ +/* $NetBSD: box.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * box.C: Box computations + */ +#include "defs.h" +RCSID("$NetBSD: box.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include "box.h" +#include "board.h" +#include "gamescreen.h" +#include + +const POINT BOX::edges[BOX::last] = + { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } }; +const POINT BOX::corners[BOX::last] = + { { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 } }; +const int BOX::syms[BOX::last] = + { GAMESCREEN::GS_HLINE, GAMESCREEN::GS_HLINE, + GAMESCREEN::GS_VLINE, GAMESCREEN::GS_VLINE }; + +BOX::BOX(size_t py, size_t px, BOARD& b) : + _b(b) +{ + _centery = py * 2 + 1; + _centerx = px * 2 + 1; +} + +void BOX::addcorner(size_t y, size_t x) +{ + char sym; + _b.getScrn()->moveto(y, x); + if (x == 0) { + if (y == 0) + sym = GAMESCREEN::GS_ULCORNER; + else if (y == _b.ty() - 1) + sym = GAMESCREEN::GS_LLCORNER; + else + sym = GAMESCREEN::GS_LTEE; + } else if (x == _b.tx() - 1) { + if (y == 0) + sym = GAMESCREEN::GS_URCORNER; + else if (y == _b.ty() - 1) + sym = GAMESCREEN::GS_LRCORNER; + else + sym = GAMESCREEN::GS_RTEE; + } else if (y == 0) + sym = GAMESCREEN::GS_TTEE; + else if (y == _b.ty() - 1) + sym = GAMESCREEN::GS_BTEE; + else + sym = GAMESCREEN::GS_PLUS; + + _b.getScrn()->addedge(sym); +} + +// Paint a box +void BOX::paint(void) +{ + int e; + if (_b.getScrn() == NULL) + return; + + _b.getScrn()->moveto(_centery, _centerx); + _b.getScrn()->addsym(name()); + + for (e = BOX::first; e < BOX::last; e++) { + addcorner(_centery + corners[e].y, _centerx + corners[e].x); + _b.getScrn()->moveto(_centery + edges[e].y, _centerx + edges[e].x); + _b.getScrn()->addedge(edge((EDGE) e)); + } + _b.getScrn()->redraw(); +} + +// Return the name +int& BOX::name(void) +{ + return _b.data(_centery, _centerx); +} + +// Set an edge +void BOX::set(int e) +{ + _b.data(_centery + edges[e].y, _centerx + edges[e].x) = syms[e]; +} + +// Clear an edge +void BOX::clr(int e) +{ + _b.data(_centery + edges[e].y, _centerx + edges[e].x) = ' '; +} + +// Test an edge +int BOX::isset(int e) const +{ + return _b.data(_centery + edges[e].y, _centerx + edges[e].x) != ' '; +} + +// Return the edge +int& BOX::edge(int e) +{ + return _b.data(_centery + edges[e].y, _centerx + edges[e].x); +} + +// Count the number of edges set in the box +int BOX::count(void) const +{ + int cnt = 0; + + for (int e = BOX::first; e < BOX::last; e++) + cnt += isset((EDGE) e); + return cnt; +} + +// Clear the box +void BOX::reset(void) +{ + for (int e = BOX::first; e < BOX::last; e++) + clr((EDGE) e); + name() = ' '; +} diff --git a/dab/gamescreen.C b/dab/gamescreen.C deleted file mode 100644 index 1a87bb96..00000000 --- a/dab/gamescreen.C +++ /dev/null @@ -1,50 +0,0 @@ -/* $NetBSD: gamescreen.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * gamescreen.C: Common screen functions - */ -#include "defs.h" -RCSID("$NetBSD: gamescreen.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include "gamescreen.h" - -// Nothing to do -GAMESCREEN::~GAMESCREEN() -{ -} diff --git a/dab/gamescreen.cc b/dab/gamescreen.cc new file mode 100644 index 00000000..47a9e223 --- /dev/null +++ b/dab/gamescreen.cc @@ -0,0 +1,50 @@ +/* $NetBSD: gamescreen.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * gamescreen.C: Common screen functions + */ +#include "defs.h" +RCSID("$NetBSD: gamescreen.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include "gamescreen.h" + +// Nothing to do +GAMESCREEN::~GAMESCREEN() +{ +} diff --git a/dab/human.C b/dab/human.C deleted file mode 100644 index e99b6aaa..00000000 --- a/dab/human.C +++ /dev/null @@ -1,151 +0,0 @@ -/* $NetBSD: human.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * human.C: Human interface for dots, using rogue-like keys. - */ -#include "defs.h" -RCSID("$NetBSD: human.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include "human.h" -#include "board.h" -#include "box.h" - -#define CONTROL(a) ((a) & 037) - -HUMAN::HUMAN(const char c) : - PLAYER(c), - _curx(0), - _cury(1) -{ -} - -void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir) -{ - int mv; - b.setpos(_cury, _curx); - - for (;;) { - switch (mv = b.getmove()) { - case 'h': case 'H': - _curx -= 2; - break; - - case 'l': case 'L': - _curx += 2; - break; - - case 'k': case 'K': - _cury -= 2; - break; - - case 'j': case 'J': - _cury += 2; - break; - - case 'u': case 'U': - _curx += 1; - _cury -= 1; - break; - - case 'y': case 'Y': - _curx -= 1; - _cury -= 1; - break; - - case 'b': case 'B': - _curx -= 1; - _cury += 1; - break; - - case 'n': case 'N': - _curx += 1; - _cury += 1; - break; - - case 'q': case 'Q': - exit(0); - - case CONTROL('L'): case CONTROL('R'): - b.clean(); - b.paint(); - break; - - case ' ': - { - x = _curx / 2; - y = _cury / 2; - - if (_cury & 1) { - if (_curx == 0) - dir = BOX::left; - else { - x--; - dir = BOX::right; - } - } - - if (_curx & 1) { - if (_cury == 0) - dir = BOX::top; - else { - y--; - dir = BOX::bottom; - } - } - } - return; - - default: - break; - } - - // We add 2 before the comparison to avoid underflow - if ((2 + _curx) - (_curx & 1) < 2) - _curx = (b.nx() * 2) + (_curx & 1); - if (_curx >= (b.nx() * 2) + 1) - _curx = (_curx & 1); - - if ((2 + _cury) - (_cury & 1) < 2) - _cury = (b.ny() * 2) + (_cury & 1); - if (_cury >= (b.ny() * 2) + 1) - _cury = (_cury & 1); - - b.setpos(_cury, _curx); - } -} diff --git a/dab/human.cc b/dab/human.cc new file mode 100644 index 00000000..79ceeb74 --- /dev/null +++ b/dab/human.cc @@ -0,0 +1,151 @@ +/* $NetBSD: human.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * human.C: Human interface for dots, using rogue-like keys. + */ +#include "defs.h" +RCSID("$NetBSD: human.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include "human.h" +#include "board.h" +#include "box.h" + +#define CONTROL(a) ((a) & 037) + +HUMAN::HUMAN(const char c) : + PLAYER(c), + _curx(0), + _cury(1) +{ +} + +void HUMAN::play(const BOARD& b, size_t& y, size_t& x, int& dir) +{ + int mv; + b.setpos(_cury, _curx); + + for (;;) { + switch (mv = b.getmove()) { + case 'h': case 'H': + _curx -= 2; + break; + + case 'l': case 'L': + _curx += 2; + break; + + case 'k': case 'K': + _cury -= 2; + break; + + case 'j': case 'J': + _cury += 2; + break; + + case 'u': case 'U': + _curx += 1; + _cury -= 1; + break; + + case 'y': case 'Y': + _curx -= 1; + _cury -= 1; + break; + + case 'b': case 'B': + _curx -= 1; + _cury += 1; + break; + + case 'n': case 'N': + _curx += 1; + _cury += 1; + break; + + case 'q': case 'Q': + exit(0); + + case CONTROL('L'): case CONTROL('R'): + b.clean(); + b.paint(); + break; + + case ' ': + { + x = _curx / 2; + y = _cury / 2; + + if (_cury & 1) { + if (_curx == 0) + dir = BOX::left; + else { + x--; + dir = BOX::right; + } + } + + if (_curx & 1) { + if (_cury == 0) + dir = BOX::top; + else { + y--; + dir = BOX::bottom; + } + } + } + return; + + default: + break; + } + + // We add 2 before the comparison to avoid underflow + if ((2 + _curx) - (_curx & 1) < 2) + _curx = (b.nx() * 2) + (_curx & 1); + if (_curx >= (b.nx() * 2) + 1) + _curx = (_curx & 1); + + if ((2 + _cury) - (_cury & 1) < 2) + _cury = (b.ny() * 2) + (_cury & 1); + if (_cury >= (b.ny() * 2) + 1) + _cury = (_cury & 1); + + b.setpos(_cury, _curx); + } +} diff --git a/dab/main.C b/dab/main.C deleted file mode 100644 index 235b0f22..00000000 --- a/dab/main.C +++ /dev/null @@ -1,196 +0,0 @@ -/* $NetBSD: main.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * main.C: Main dots program - */ -#include "defs.h" -RCSID("$NetBSD: main.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include -#include -#include -#include -#include -#include "algor.h" -#include "board.h" -#include "human.h" -#include "ttyscrn.h" - -// Print the command line usage -static void usage(char* pname) -{ - char* p = strrchr(pname, '/'); - if (p) - p++; - else - p = pname; - std::cerr << "Usage: " << p - << " [-w] [-p ] [-n ] [ []]" << std::endl; -} - -// Play a single game -static void play(BOARD& b, PLAYER* p[2]) -{ - // Initialize - b.init(); - p[0]->init(); - p[1]->init(); - b.paint(); - - // Alternate turns between players, scoring each turn - for (size_t i = 0;; i = (i + 1) & 1) { - b.score(i, *p[i]); - if (!p[i]->domove(b)) { - // No more moves, game over - break; - } - b.score(i, *p[i]); - } - - // Find who won - p[0]->wl(p[1]->getScore()); - p[1]->wl(p[0]->getScore()); - - // Post scores - b.score(0, *p[0]); - b.score(1, *p[1]); - - // Post totals - b.total(0, *p[0]); - b.total(1, *p[1]); - - // Post games - b.games(0, *p[0]); - b.games(1, *p[1]); - - // Post ties - b.ties(*p[0]); -} - -int main(int argc, char** argv) -{ - size_t ny, nx, nn = 1, wt = 0; - char* nc = "ch"; - int c; - int acs = 1; - - while ((c = getopt(argc, argv, "awp:n:")) != -1) - switch (c) { - case 'a': - acs = 0; - break; - case 'w': - wt++; - break; - - case 'p': - nc = optarg; - break; - - case 'n': - nn = atoi(optarg); - break; - - default: - usage(argv[0]); - return 1; - } - - // Get the size of the board if specified - switch (argc - optind) { - case 0: - ny = nx = 3; - break; - - case 1: - ny = nx = atoi(argv[optind]); - break; - - case 2: - nx = atoi(argv[optind]); - ny = atoi(argv[optind+1]); - break; - - default: - usage(argv[0]); - return 1; - } - - - PLAYER* p[2]; - - // Allocate players - for (size_t i = 0; i < 2; i++) { - char n = nc[1] == nc[0] ? i + '0' : nc[i]; - switch (nc[i]) { - case 'c': - p[i] = new ALGOR(n); - break; - - case 'h': - p[i] = new HUMAN(n); - break; - - default: - usage(argv[0]); - return 1; - } - } - - GAMESCREEN *sc = TTYSCRN::create(acs, ny, nx); - if (sc == NULL) - ::errx(1, "Dimensions too large for current screen."); - - BOARD b(ny, nx, sc); - - // Play games - while (nn--) { - play(b, p); - if (wt) - b.getmove(); - } - - if (wt == 0) - b.getmove(); - // Cleanup - delete sc; - delete p[0]; - delete p[1]; - return 0; -} diff --git a/dab/main.cc b/dab/main.cc new file mode 100644 index 00000000..c993874f --- /dev/null +++ b/dab/main.cc @@ -0,0 +1,196 @@ +/* $NetBSD: main.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * main.C: Main dots program + */ +#include "defs.h" +RCSID("$NetBSD: main.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include +#include +#include +#include +#include +#include "algor.h" +#include "board.h" +#include "human.h" +#include "ttyscrn.h" + +// Print the command line usage +static void usage(char* pname) +{ + char* p = strrchr(pname, '/'); + if (p) + p++; + else + p = pname; + std::cerr << "Usage: " << p + << " [-w] [-p ] [-n ] [ []]" << std::endl; +} + +// Play a single game +static void play(BOARD& b, PLAYER* p[2]) +{ + // Initialize + b.init(); + p[0]->init(); + p[1]->init(); + b.paint(); + + // Alternate turns between players, scoring each turn + for (size_t i = 0;; i = (i + 1) & 1) { + b.score(i, *p[i]); + if (!p[i]->domove(b)) { + // No more moves, game over + break; + } + b.score(i, *p[i]); + } + + // Find who won + p[0]->wl(p[1]->getScore()); + p[1]->wl(p[0]->getScore()); + + // Post scores + b.score(0, *p[0]); + b.score(1, *p[1]); + + // Post totals + b.total(0, *p[0]); + b.total(1, *p[1]); + + // Post games + b.games(0, *p[0]); + b.games(1, *p[1]); + + // Post ties + b.ties(*p[0]); +} + +int main(int argc, char** argv) +{ + size_t ny, nx, nn = 1, wt = 0; + char* nc = "ch"; + int c; + int acs = 1; + + while ((c = getopt(argc, argv, "awp:n:")) != -1) + switch (c) { + case 'a': + acs = 0; + break; + case 'w': + wt++; + break; + + case 'p': + nc = optarg; + break; + + case 'n': + nn = atoi(optarg); + break; + + default: + usage(argv[0]); + return 1; + } + + // Get the size of the board if specified + switch (argc - optind) { + case 0: + ny = nx = 3; + break; + + case 1: + ny = nx = atoi(argv[optind]); + break; + + case 2: + nx = atoi(argv[optind]); + ny = atoi(argv[optind+1]); + break; + + default: + usage(argv[0]); + return 1; + } + + + PLAYER* p[2]; + + // Allocate players + for (size_t i = 0; i < 2; i++) { + char n = nc[1] == nc[0] ? i + '0' : nc[i]; + switch (nc[i]) { + case 'c': + p[i] = new ALGOR(n); + break; + + case 'h': + p[i] = new HUMAN(n); + break; + + default: + usage(argv[0]); + return 1; + } + } + + GAMESCREEN *sc = TTYSCRN::create(acs, ny, nx); + if (sc == NULL) + ::errx(1, "Dimensions too large for current screen."); + + BOARD b(ny, nx, sc); + + // Play games + while (nn--) { + play(b, p); + if (wt) + b.getmove(); + } + + if (wt == 0) + b.getmove(); + // Cleanup + delete sc; + delete p[0]; + delete p[1]; + return 0; +} diff --git a/dab/player.C b/dab/player.C deleted file mode 100644 index 044ce9a2..00000000 --- a/dab/player.C +++ /dev/null @@ -1,98 +0,0 @@ -/* $NetBSD: player.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * player.C: Player base class - */ - -#include "defs.h" -RCSID("$NetBSD: player.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include "board.h" -#include "player.h" - -PLAYER::PLAYER(char who) : - _who(who), - _score(0), - _total(0), - _games(0), - _ties(0) -{ -} - -void PLAYER::init(void) -{ - _score = 0; -} - -void PLAYER::wl(size_t sc) -{ - _total += _score; - _games += sc < _score; - _ties += sc == _score; -} - -int PLAYER::domove(BOARD& b) -{ - size_t y, x; - int dir; - int score; - - for (;;) { - if (b.full()) - return 0; - - play(b, y, x, dir); - - switch (score = b.domove(y, x, dir, _who)) { - case 0: - // No closure - return 1; - - case -1: - // Not a valid move - b.bell(); - break; - - default: - // Closure, play again - _score += score; - break; - } - } -} diff --git a/dab/player.cc b/dab/player.cc new file mode 100644 index 00000000..25ae6caa --- /dev/null +++ b/dab/player.cc @@ -0,0 +1,98 @@ +/* $NetBSD: player.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * player.C: Player base class + */ + +#include "defs.h" +RCSID("$NetBSD: player.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include "board.h" +#include "player.h" + +PLAYER::PLAYER(char who) : + _who(who), + _score(0), + _total(0), + _games(0), + _ties(0) +{ +} + +void PLAYER::init(void) +{ + _score = 0; +} + +void PLAYER::wl(size_t sc) +{ + _total += _score; + _games += sc < _score; + _ties += sc == _score; +} + +int PLAYER::domove(BOARD& b) +{ + size_t y, x; + int dir; + int score; + + for (;;) { + if (b.full()) + return 0; + + play(b, y, x, dir); + + switch (score = b.domove(y, x, dir, _who)) { + case 0: + // No closure + return 1; + + case -1: + // Not a valid move + b.bell(); + break; + + default: + // Closure, play again + _score += score; + break; + } + } +} diff --git a/dab/random.C b/dab/random.C deleted file mode 100644 index d4db412e..00000000 --- a/dab/random.C +++ /dev/null @@ -1,86 +0,0 @@ -/* $Header: /cvsroot/src/games/dab/Attic/random.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * random.C: Randomizer for the dots program - */ - -#include "defs.h" -RCSID("$NetBSD: random.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include -#include -#include "random.h" - -RANDOM::RANDOM(size_t ns) : - _bs(ns) -{ - _bm = new char[(_bs >> 3) + 1]; - clear(); -} - -RANDOM::~RANDOM() -{ - delete[] _bm; -} - -// Reinitialize -void RANDOM::clear(void) -{ - _nv = 0; - ::srand48(::time(NULL)); - (void) ::memset(_bm, 0, (_bs >> 3) + 1); -} - -// Return the next random value -size_t RANDOM::operator() (void) -{ - // No more values - if (_nv == _bs) - return _bs; - - for (;;) { - size_t r = ::lrand48(); - size_t z = r % _bs; - if (!isset(z)) { - set(z); - _nv++; - return z; - } - } -} diff --git a/dab/random.cc b/dab/random.cc new file mode 100644 index 00000000..b55f760b --- /dev/null +++ b/dab/random.cc @@ -0,0 +1,86 @@ +/* $Header: /cvsroot/src/games/dab/random.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * random.C: Randomizer for the dots program + */ + +#include "defs.h" +RCSID("$NetBSD: random.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include +#include +#include "random.h" + +RANDOM::RANDOM(size_t ns) : + _bs(ns) +{ + _bm = new char[(_bs >> 3) + 1]; + clear(); +} + +RANDOM::~RANDOM() +{ + delete[] _bm; +} + +// Reinitialize +void RANDOM::clear(void) +{ + _nv = 0; + ::srand48(::time(NULL)); + (void) ::memset(_bm, 0, (_bs >> 3) + 1); +} + +// Return the next random value +size_t RANDOM::operator() (void) +{ + // No more values + if (_nv == _bs) + return _bs; + + for (;;) { + size_t r = ::lrand48(); + size_t z = r % _bs; + if (!isset(z)) { + set(z); + _nv++; + return z; + } + } +} diff --git a/dab/test.C b/dab/test.C deleted file mode 100644 index 4491f895..00000000 --- a/dab/test.C +++ /dev/null @@ -1,57 +0,0 @@ -/* $NetBSD: test.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * test.C: Test program for randomizer - */ - -#include "defs.h" -RCSID("$NetBSD: test.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include -#include "random.h" - -int -main(void) -{ - RANDOM rd(10); - - for (size_t x = rd(); x < 10; x = rd()) - std::cout << "x=" << x << std::endl; - return 0; -} diff --git a/dab/test.cc b/dab/test.cc new file mode 100644 index 00000000..de031474 --- /dev/null +++ b/dab/test.cc @@ -0,0 +1,57 @@ +/* $NetBSD: test.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * test.C: Test program for randomizer + */ + +#include "defs.h" +RCSID("$NetBSD: test.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include +#include "random.h" + +int +main(void) +{ + RANDOM rd(10); + + for (size_t x = rd(); x < 10; x = rd()) + std::cout << "x=" << x << std::endl; + return 0; +} diff --git a/dab/ttyscrn.C b/dab/ttyscrn.C deleted file mode 100644 index e0e8190d..00000000 --- a/dab/ttyscrn.C +++ /dev/null @@ -1,235 +0,0 @@ -/* $NetBSD: ttyscrn.C,v 1.2 2003/12/26 18:03:34 christos 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. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the NetBSD - * Foundation, Inc. and its contributors. - * 4. Neither the name of The NetBSD Foundation nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * 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. - */ - -/* - * ttyscrn.C: Curses screen implementation for dots - */ - -#include "defs.h" -RCSID("$NetBSD: ttyscrn.C,v 1.2 2003/12/26 18:03:34 christos Exp $") - -#include -#include -#include - -#include "player.h" -#include "ttyscrn.h" - -void TTYSCRN::clean(void) -{ - clear(); -} - -void TTYSCRN::moveto(size_t y, size_t x) -{ - move(y + TTYSCRN::offsy, x + TTYSCRN::offsx); -} - -void TTYSCRN::addsym(const int sym) -{ - addch(sym); -} - -void TTYSCRN::addedge(const int sym) -{ - int nsym; -#ifdef A_ALTCHARSET - if (_acs) { - switch (sym) { - case GS_HLINE: - nsym = ACS_HLINE; - break; - case GS_VLINE: - nsym = ACS_VLINE; - break; - case GS_ULCORNER: - nsym = ACS_ULCORNER; - break; - case GS_URCORNER: - nsym = ACS_URCORNER; - break; - case GS_LLCORNER: - nsym = ACS_LLCORNER; - break; - case GS_LRCORNER: - nsym = ACS_LRCORNER; - break; - case GS_LTEE: - nsym = ACS_LTEE; - break; - case GS_RTEE: - nsym = ACS_RTEE; - break; - case GS_TTEE: - nsym = ACS_TTEE; - break; - case GS_BTEE: - nsym = ACS_BTEE; - break; - case GS_PLUS: - nsym = ACS_PLUS; - break; - case ' ': - addsym(' '); - return; - default: - ::abort(); - } - attron(A_ALTCHARSET); - addch(nsym); - attroff(A_ALTCHARSET); - return; - } -#endif - switch (sym) { - case GS_HLINE: - nsym = '-'; - break; - case GS_VLINE: - nsym = '|'; - break; - case GS_ULCORNER: - nsym = '.'; - break; - case GS_URCORNER: - nsym = '.'; - break; - case GS_LLCORNER: - nsym = '.'; - break; - case GS_LRCORNER: - nsym = '.'; - break; - case GS_LTEE: - nsym = '.'; - break; - case GS_RTEE: - nsym = '.'; - break; - case GS_TTEE: - nsym = '.'; - break; - case GS_BTEE: - nsym = '.'; - break; - case GS_PLUS: - nsym = '+'; - break; - case ' ': - addsym(' '); - return; - default: - ::abort(); - } - addsym(nsym); -} - -void TTYSCRN::redraw(void) -{ - refresh(); - doupdate(); -} - -void TTYSCRN::bell(void) -{ - putc('\007', stdout); -} - -int TTYSCRN::getinput(void) -{ - return getch(); -} - -void TTYSCRN::score(size_t s, const PLAYER& p) -{ - mvwprintw(stdscr, _sy + s + TTYSCRN::offsscore, _sx, "S %c:%5d", p.getWho(), - p.getScore()); -} - -void TTYSCRN::total(size_t s, const PLAYER& p) -{ - mvwprintw(stdscr, _sy + s + TTYSCRN::offstotal, _sx, "T %c:%5d", p.getWho(), - p.getTotal()); -} - -void TTYSCRN::games(size_t s, const PLAYER& p) -{ - mvwprintw(stdscr, _sy + s + TTYSCRN::offsgames, _sx, "G %c:%5d", p.getWho(), - p.getGames()); -} - -void TTYSCRN::ties(const PLAYER& p) -{ - mvwprintw(stdscr, _sy + TTYSCRN::offsties, _sx, "G =:%5d", p.getTies()); -} - -TTYSCRN* TTYSCRN::create(int acs, size_t y, size_t x) -{ - int tx, ty; - - initscr(); - - tx = getmaxx(stdscr); - ty = getmaxy(stdscr); - - if (tx == ERR || ty == ERR || (size_t)tx < x * 2 + TTYSCRN::offsx + 12 - || (size_t)ty < y * 2 + TTYSCRN::offsy) { - endwin(); - return NULL; - } - cbreak(); - noecho(); - - - TTYSCRN* that = new TTYSCRN; - - that->_tx = tx; - that->_ty = ty; - that->_sx = tx - 12; - that->_sy = TTYSCRN::offsy; - that->_acs = acs; - - return that; -} - -TTYSCRN::~TTYSCRN(void) -{ - nocbreak(); - echo(); - endwin(); -} diff --git a/dab/ttyscrn.cc b/dab/ttyscrn.cc new file mode 100644 index 00000000..9a9cd364 --- /dev/null +++ b/dab/ttyscrn.cc @@ -0,0 +1,235 @@ +/* $NetBSD: ttyscrn.cc,v 1.1 2003/12/27 01:16:55 christos 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * 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. + */ + +/* + * ttyscrn.C: Curses screen implementation for dots + */ + +#include "defs.h" +RCSID("$NetBSD: ttyscrn.cc,v 1.1 2003/12/27 01:16:55 christos Exp $") + +#include +#include +#include + +#include "player.h" +#include "ttyscrn.h" + +void TTYSCRN::clean(void) +{ + clear(); +} + +void TTYSCRN::moveto(size_t y, size_t x) +{ + move(y + TTYSCRN::offsy, x + TTYSCRN::offsx); +} + +void TTYSCRN::addsym(const int sym) +{ + addch(sym); +} + +void TTYSCRN::addedge(const int sym) +{ + int nsym; +#ifdef A_ALTCHARSET + if (_acs) { + switch (sym) { + case GS_HLINE: + nsym = ACS_HLINE; + break; + case GS_VLINE: + nsym = ACS_VLINE; + break; + case GS_ULCORNER: + nsym = ACS_ULCORNER; + break; + case GS_URCORNER: + nsym = ACS_URCORNER; + break; + case GS_LLCORNER: + nsym = ACS_LLCORNER; + break; + case GS_LRCORNER: + nsym = ACS_LRCORNER; + break; + case GS_LTEE: + nsym = ACS_LTEE; + break; + case GS_RTEE: + nsym = ACS_RTEE; + break; + case GS_TTEE: + nsym = ACS_TTEE; + break; + case GS_BTEE: + nsym = ACS_BTEE; + break; + case GS_PLUS: + nsym = ACS_PLUS; + break; + case ' ': + addsym(' '); + return; + default: + ::abort(); + } + attron(A_ALTCHARSET); + addch(nsym); + attroff(A_ALTCHARSET); + return; + } +#endif + switch (sym) { + case GS_HLINE: + nsym = '-'; + break; + case GS_VLINE: + nsym = '|'; + break; + case GS_ULCORNER: + nsym = '.'; + break; + case GS_URCORNER: + nsym = '.'; + break; + case GS_LLCORNER: + nsym = '.'; + break; + case GS_LRCORNER: + nsym = '.'; + break; + case GS_LTEE: + nsym = '.'; + break; + case GS_RTEE: + nsym = '.'; + break; + case GS_TTEE: + nsym = '.'; + break; + case GS_BTEE: + nsym = '.'; + break; + case GS_PLUS: + nsym = '+'; + break; + case ' ': + addsym(' '); + return; + default: + ::abort(); + } + addsym(nsym); +} + +void TTYSCRN::redraw(void) +{ + refresh(); + doupdate(); +} + +void TTYSCRN::bell(void) +{ + putc('\007', stdout); +} + +int TTYSCRN::getinput(void) +{ + return getch(); +} + +void TTYSCRN::score(size_t s, const PLAYER& p) +{ + mvwprintw(stdscr, _sy + s + TTYSCRN::offsscore, _sx, "S %c:%5d", p.getWho(), + p.getScore()); +} + +void TTYSCRN::total(size_t s, const PLAYER& p) +{ + mvwprintw(stdscr, _sy + s + TTYSCRN::offstotal, _sx, "T %c:%5d", p.getWho(), + p.getTotal()); +} + +void TTYSCRN::games(size_t s, const PLAYER& p) +{ + mvwprintw(stdscr, _sy + s + TTYSCRN::offsgames, _sx, "G %c:%5d", p.getWho(), + p.getGames()); +} + +void TTYSCRN::ties(const PLAYER& p) +{ + mvwprintw(stdscr, _sy + TTYSCRN::offsties, _sx, "G =:%5d", p.getTies()); +} + +TTYSCRN* TTYSCRN::create(int acs, size_t y, size_t x) +{ + int tx, ty; + + initscr(); + + tx = getmaxx(stdscr); + ty = getmaxy(stdscr); + + if (tx == ERR || ty == ERR || (size_t)tx < x * 2 + TTYSCRN::offsx + 12 + || (size_t)ty < y * 2 + TTYSCRN::offsy) { + endwin(); + return NULL; + } + cbreak(); + noecho(); + + + TTYSCRN* that = new TTYSCRN; + + that->_tx = tx; + that->_ty = ty; + that->_sx = tx - 12; + that->_sy = TTYSCRN::offsy; + that->_acs = acs; + + return that; +} + +TTYSCRN::~TTYSCRN(void) +{ + nocbreak(); + echo(); + endwin(); +} -- cgit v1.2.3-56-ge451