]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - dab/board.cc
1 /* $NetBSD: board.cc,v 1.2 2005/08/09 02:38:32 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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * board.C: Board manipulations
43 RCSID("$NetBSD: board.cc,v 1.2 2005/08/09 02:38:32 christos Exp $")
49 #include "gamescreen.h"
53 BOARD::BOARD(size_t y
, size_t x
, GAMESCREEN
* scrn
) :
63 for (y
= 0; y
< _ty
; y
++)
69 BOARD::BOARD(const BOARD
& b
) :
78 for (size_t y
= 0; y
< _ty
; y
++) {
80 (void) memcpy(_b
[y
], b
._b
[y
], _tx
* sizeof(int));
88 for (y
= 0; y
< _ty
; y
++)
94 // Clear all boxes and reset state for a new game
95 void BOARD::init(void)
99 for (y
= 0; y
< _ny
; y
++)
100 for (x
= 0; x
< _nx
; x
++) {
101 BOX
box(y
, x
, *this);
107 * Make a move for player with initial 'c', adding an edge at box(x, y)
108 * and the specified direction.
111 * n: Number of closures n E [0..2]
113 int BOARD::domove(size_t y
, size_t x
, int dir
, char c
)
117 // Check if out of bounds
121 BOX
box1(y
, x
, *this);
123 // Check if the edge is already there
129 if (box1
.count() == 4) {
130 // New box; name it and count it
138 x
+= BOX::edges
[dir
].x
;
139 y
+= BOX::edges
[dir
].y
;
142 BOX
box2(y
, x
, *this);
143 if (box2
.count() == 4) {
152 // Return true if the board is full
153 int BOARD::full(void) const
155 for (size_t y
= 0; y
< _ny
; y
++)
156 for (size_t x
= 0; x
< _nx
; x
++) {
157 BOX
box(y
, x
, const_cast<BOARD
&>(*this));
158 if (box
.count() != 4)
164 // Return if the coordinates are within bounds; we don't check for < 0,
165 // since size_t is unsigned
166 int BOARD::bounds(size_t y
, size_t x
) const
168 return x
< _nx
&& y
< _ny
;
171 // Paint all boxes, effectively redrawing the board
172 void BOARD::paint(void) const
174 for (size_t y
= 0; y
< _ny
; y
++)
175 for (size_t x
= 0; x
< _nx
; x
++) {
176 BOX
box(y
, x
, const_cast<BOARD
&>(*this));
182 void BOARD::clean(void) const
189 // Move cursor to x, y
190 void BOARD::setpos(size_t y
, size_t x
) const
198 // Return character indicating move
199 int BOARD::getmove(void) const
204 return _scrn
->getinput();
208 void BOARD::bell(void) const
215 // Post the score in the current game for player i
216 void BOARD::score(size_t i
, const PLAYER
& p
)
223 // Post the number of games won for player i
224 void BOARD::games(size_t i
, const PLAYER
& p
)
231 // Post the total score for player i
232 void BOARD::total(size_t i
, const PLAYER
& p
)
239 // Post the total score for player i
240 void BOARD::ties(const PLAYER
& p
)
247 // Internal algorithm error; post and abort
248 void BOARD::abort(const char* s
, ...) const
250 for (size_t i
= 0; i
< _ny
; i
++)
251 fprintf(stderr
, "\n");
254 fprintf(stderr
, "Algorithm internal error: ");
256 vfprintf(stderr
, s
, ap
);
258 fprintf(stderr
, "\n");