1 /* $NetBSD: board.cc,v 1.3 2005/08/09 15:17:41 christos Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * board.C: Board manipulations
36 RCSID("$NetBSD: board.cc,v 1.3 2005/08/09 15:17:41 christos Exp $")
42 #include "gamescreen.h"
46 BOARD::BOARD(size_t y
, size_t x
, GAMESCREEN
* scrn
) :
56 for (y
= 0; y
< _ty
; y
++)
62 BOARD::BOARD(const BOARD
& b
) :
71 for (size_t y
= 0; y
< _ty
; y
++) {
73 static_cast<void>(memcpy(_b
[y
], b
._b
[y
], _tx
* sizeof(int)));
81 for (y
= 0; y
< _ty
; y
++)
87 // Clear all boxes and reset state for a new game
88 void BOARD::init(void)
92 for (y
= 0; y
< _ny
; y
++)
93 for (x
= 0; x
< _nx
; x
++) {
100 * Make a move for player with initial 'c', adding an edge at box(x, y)
101 * and the specified direction.
104 * n: Number of closures n E [0..2]
106 int BOARD::domove(size_t y
, size_t x
, int dir
, char c
)
110 // Check if out of bounds
114 BOX
box1(y
, x
, *this);
116 // Check if the edge is already there
122 if (box1
.count() == 4) {
123 // New box; name it and count it
131 x
+= BOX::edges
[dir
].x
;
132 y
+= BOX::edges
[dir
].y
;
135 BOX
box2(y
, x
, *this);
136 if (box2
.count() == 4) {
145 // Return true if the board is full
146 int BOARD::full(void) const
148 for (size_t y
= 0; y
< _ny
; y
++)
149 for (size_t x
= 0; x
< _nx
; x
++) {
150 BOX
box(y
, x
, const_cast<BOARD
&>(*this));
151 if (box
.count() != 4)
157 // Return if the coordinates are within bounds; we don't check for < 0,
158 // since size_t is unsigned
159 int BOARD::bounds(size_t y
, size_t x
) const
161 return x
< _nx
&& y
< _ny
;
164 // Paint all boxes, effectively redrawing the board
165 void BOARD::paint(void) const
167 for (size_t y
= 0; y
< _ny
; y
++)
168 for (size_t x
= 0; x
< _nx
; x
++) {
169 BOX
box(y
, x
, const_cast<BOARD
&>(*this));
175 void BOARD::clean(void) const
182 // Move cursor to x, y
183 void BOARD::setpos(size_t y
, size_t x
) const
191 // Return character indicating move
192 int BOARD::getmove(void) const
197 return _scrn
->getinput();
201 void BOARD::bell(void) const
208 // Post the score in the current game for player i
209 void BOARD::score(size_t i
, const PLAYER
& p
)
216 // Post the number of games won for player i
217 void BOARD::games(size_t i
, const PLAYER
& p
)
224 // Post the total score for player i
225 void BOARD::total(size_t i
, const PLAYER
& p
)
232 // Post the total score for player i
233 void BOARD::ties(const PLAYER
& p
)
240 // Internal algorithm error; post and abort
241 void BOARD::abort(const char* s
, ...) const
243 for (size_t i
= 0; i
< _ny
; i
++)
244 fprintf(stderr
, "\n");
247 fprintf(stderr
, "Algorithm internal error: ");
249 vfprintf(stderr
, s
, ap
);
251 fprintf(stderr
, "\n");