123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /* $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 <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #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();
- }
|