Home | History | Annotate | Line # | Download | only in dab
board.cc revision 1.2
      1  1.2  christos /*	$NetBSD: board.cc,v 1.2 2005/08/09 02:38:32 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos /*
     40  1.1  christos  * board.C: Board manipulations
     41  1.1  christos  */
     42  1.1  christos #include "defs.h"
     43  1.2  christos RCSID("$NetBSD: board.cc,v 1.2 2005/08/09 02:38:32 christos Exp $")
     44  1.1  christos 
     45  1.1  christos #include <stdio.h>
     46  1.1  christos #include <string.h>
     47  1.1  christos #include <stdarg.h>
     48  1.1  christos #include "board.h"
     49  1.1  christos #include "gamescreen.h"
     50  1.1  christos #include "box.h"
     51  1.1  christos #include "player.h"
     52  1.1  christos 
     53  1.1  christos BOARD::BOARD(size_t y, size_t x, GAMESCREEN* scrn) :
     54  1.1  christos     _ny(y),
     55  1.1  christos     _nx(x),
     56  1.1  christos     _scrn(scrn)
     57  1.1  christos {
     58  1.1  christos     _ty = 2 * _ny + 1;
     59  1.1  christos     _tx = 2 * _nx + 1;
     60  1.1  christos 
     61  1.1  christos     _b = new int*[_ty];
     62  1.1  christos 
     63  1.1  christos     for (y = 0; y < _ty; y++)
     64  1.1  christos 	_b[y] = new int[_tx];
     65  1.1  christos 
     66  1.1  christos     init();
     67  1.1  christos }
     68  1.1  christos 
     69  1.1  christos BOARD::BOARD(const BOARD& b) :
     70  1.1  christos     _ty(b._ty),
     71  1.1  christos     _tx(b._tx),
     72  1.1  christos     _ny(b._ny),
     73  1.1  christos     _nx(b._nx),
     74  1.1  christos     _scrn(NULL)
     75  1.1  christos {
     76  1.1  christos     _b = new int*[_ty];
     77  1.1  christos 
     78  1.1  christos     for (size_t y = 0; y < _ty; y++) {
     79  1.1  christos 	_b[y] = new int[_tx];
     80  1.1  christos 	(void) memcpy(_b[y], b._b[y], _tx * sizeof(int));
     81  1.1  christos     }
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos BOARD::~BOARD()
     85  1.1  christos {
     86  1.1  christos     size_t y;
     87  1.1  christos 
     88  1.1  christos     for (y = 0; y < _ty; y++)
     89  1.1  christos 	delete[] _b[y];
     90  1.1  christos 
     91  1.1  christos     delete[] _b;
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos // Clear all boxes and reset state for a new game
     95  1.1  christos void BOARD::init(void)
     96  1.1  christos {
     97  1.1  christos     size_t x, y;
     98  1.1  christos 
     99  1.1  christos     for (y = 0; y < _ny; y++)
    100  1.1  christos 	for (x = 0; x < _nx; x++) {
    101  1.1  christos 	    BOX box(y, x, *this);
    102  1.1  christos 	    box.reset();
    103  1.1  christos 	}
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos /*
    107  1.1  christos  * Make a move for player with initial 'c', adding an edge at box(x, y)
    108  1.1  christos  * and the specified direction.
    109  1.1  christos  * returns:
    110  1.1  christos  *	-1:	Invalid move
    111  1.1  christos  *	 n:	Number of closures n E [0..2]
    112  1.1  christos  */
    113  1.1  christos int BOARD::domove(size_t y, size_t x, int dir, char c)
    114  1.1  christos {
    115  1.1  christos     int closed = 0;
    116  1.1  christos 
    117  1.1  christos     // Check if out of bounds
    118  1.1  christos     if (!bounds(y, x))
    119  1.1  christos 	return -1;
    120  1.1  christos 
    121  1.1  christos     BOX box1(y, x, *this);
    122  1.1  christos 
    123  1.1  christos     // Check if the edge is already there
    124  1.1  christos     if (box1.isset(dir))
    125  1.1  christos 	return -1;
    126  1.1  christos 
    127  1.1  christos     box1.set(dir);
    128  1.1  christos 
    129  1.1  christos     if (box1.count() == 4) {
    130  1.1  christos 	// New box; name it and count it
    131  1.1  christos 	box1.name() = c;
    132  1.1  christos 	closed++;
    133  1.1  christos     }
    134  1.1  christos 
    135  1.1  christos     box1.paint();
    136  1.1  christos 
    137  1.1  christos     // Check other box
    138  1.1  christos     x += BOX::edges[dir].x;
    139  1.1  christos     y += BOX::edges[dir].y;
    140  1.1  christos 
    141  1.1  christos     if (bounds(y, x)) {
    142  1.1  christos 	BOX box2(y, x, *this);
    143  1.1  christos 	if (box2.count() == 4) {
    144  1.1  christos 	    box2.name() = c;
    145  1.1  christos 	    box2.paint();
    146  1.1  christos 	    closed++;
    147  1.1  christos 	}
    148  1.1  christos     }
    149  1.1  christos     return closed;
    150  1.1  christos }
    151  1.1  christos 
    152  1.1  christos // Return true if the board is full
    153  1.1  christos int BOARD::full(void) const
    154  1.1  christos {
    155  1.1  christos     for (size_t y = 0; y < _ny; y++)
    156  1.1  christos 	for (size_t x = 0; x < _nx; x++) {
    157  1.2  christos 	    BOX box(y, x, const_cast<BOARD&>(*this));
    158  1.1  christos 	    if (box.count() != 4)
    159  1.1  christos 		return 0;
    160  1.1  christos 	}
    161  1.1  christos     return 1;
    162  1.1  christos }
    163  1.1  christos 
    164  1.1  christos // Return if the coordinates are within bounds; we don't check for < 0,
    165  1.1  christos // since size_t is unsigned
    166  1.1  christos int BOARD::bounds(size_t y, size_t x) const
    167  1.1  christos {
    168  1.1  christos     return x < _nx && y < _ny;
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos // Paint all boxes, effectively redrawing the board
    172  1.1  christos void BOARD::paint(void) const
    173  1.1  christos {
    174  1.1  christos     for (size_t y = 0; y < _ny; y++)
    175  1.1  christos 	for (size_t x = 0; x < _nx; x++) {
    176  1.2  christos 	    BOX box(y, x, const_cast<BOARD&>(*this));
    177  1.1  christos 	    box.paint();
    178  1.1  christos 	}
    179  1.1  christos }
    180  1.1  christos 
    181  1.1  christos // Clear the screen
    182  1.1  christos void BOARD::clean(void) const
    183  1.1  christos {
    184  1.1  christos     if (!_scrn)
    185  1.1  christos 	return;
    186  1.1  christos     _scrn->clean();
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos // Move cursor to x, y
    190  1.1  christos void BOARD::setpos(size_t y, size_t x) const
    191  1.1  christos {
    192  1.1  christos     if (!_scrn)
    193  1.1  christos 	return;
    194  1.1  christos     _scrn->moveto(y, x);
    195  1.1  christos     _scrn->redraw();
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos // Return character indicating move
    199  1.1  christos int BOARD::getmove(void) const
    200  1.1  christos {
    201  1.1  christos     if (!_scrn)
    202  1.1  christos 	return 'q';
    203  1.1  christos     _scrn->redraw();
    204  1.1  christos     return _scrn->getinput();
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos // Ring the bell
    208  1.1  christos void BOARD::bell(void) const
    209  1.1  christos {
    210  1.1  christos     if (!_scrn)
    211  1.1  christos 	return;
    212  1.1  christos     _scrn->bell();
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos // Post the score in the current game for player i
    216  1.1  christos void BOARD::score(size_t i, const PLAYER& p)
    217  1.1  christos {
    218  1.1  christos     if (_scrn == NULL)
    219  1.1  christos 	return;
    220  1.1  christos     _scrn->score(i, p);
    221  1.1  christos }
    222  1.1  christos 
    223  1.1  christos // Post the number of games won for player i
    224  1.1  christos void BOARD::games(size_t i, const PLAYER& p)
    225  1.1  christos {
    226  1.1  christos     if (_scrn == NULL)
    227  1.1  christos 	return;
    228  1.1  christos     _scrn->games(i, p);
    229  1.1  christos }
    230  1.1  christos 
    231  1.1  christos // Post the total score for player i
    232  1.1  christos void BOARD::total(size_t i, const PLAYER& p)
    233  1.1  christos {
    234  1.1  christos     if (_scrn == NULL)
    235  1.1  christos 	return;
    236  1.1  christos     _scrn->total(i, p);
    237  1.1  christos }
    238  1.1  christos 
    239  1.1  christos // Post the total score for player i
    240  1.1  christos void BOARD::ties(const PLAYER& p)
    241  1.1  christos {
    242  1.1  christos     if (_scrn == NULL)
    243  1.1  christos 	return;
    244  1.1  christos     _scrn->ties(p);
    245  1.1  christos }
    246  1.1  christos 
    247  1.1  christos // Internal algorithm error; post and abort
    248  1.1  christos void BOARD::abort(const char* s, ...) const
    249  1.1  christos {
    250  1.1  christos     for (size_t i = 0; i < _ny; i++)
    251  1.1  christos 	fprintf(stderr, "\n");
    252  1.1  christos 
    253  1.1  christos     va_list ap;
    254  1.1  christos     fprintf(stderr, "Algorithm internal error: ");
    255  1.1  christos     va_start(ap, s);
    256  1.1  christos     vfprintf(stderr, s, ap);
    257  1.1  christos     va_end(ap);
    258  1.1  christos     fprintf(stderr, "\n");
    259  1.1  christos     ::abort();
    260  1.1  christos }
    261