1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/* $NetBSD: board.cc,v 1.4 2008/04/28 20:22:53 martin 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.
*
* 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.4 2008/04/28 20:22:53 martin 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];
static_cast<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, const_cast<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, const_cast<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();
}
|