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: #include "defs.h"
43: RCSID("$NetBSD: board.cc,v 1.1 2003/12/27 01:16:55 christos Exp $")
44:
45: #include <stdio.h>
46: #include <string.h>
47: #include <stdarg.h>
48: #include "board.h"
49: #include "gamescreen.h"
50: #include "box.h"
51: #include "player.h"
52:
53: BOARD::BOARD(size_t y, size_t x, GAMESCREEN* scrn) :
54: _ny(y),
55: _nx(x),
56: _scrn(scrn)
57: {
58: _ty = 2 * _ny + 1;
59: _tx = 2 * _nx + 1;
60:
61: _b = new int*[_ty];
62:
63: for (y = 0; y < _ty; y++)
64: _b[y] = new int[_tx];
65:
66: init();
67: }
68:
69: BOARD::BOARD(const BOARD& b) :
70: _ty(b._ty),
71: _tx(b._tx),
72: _ny(b._ny),
73: _nx(b._nx),
74: _scrn(NULL)
75: {
76: _b = new int*[_ty];
77:
78: for (size_t y = 0; y < _ty; y++) {
79: _b[y] = new int[_tx];
80: (void) memcpy(_b[y], b._b[y], _tx * sizeof(int));
81: }
82: }
83:
84: BOARD::~BOARD()
85: {
86: size_t y;
87:
88: for (y = 0; y < _ty; y++)
89: delete[] _b[y];
90:
91: delete[] _b;
92: }
93:
94:
95: void BOARD::init(void)
96: {
97: size_t x, y;
98:
99: for (y = 0; y < _ny; y++)
100: for (x = 0; x < _nx; x++) {
101: BOX box(y, x, *this);
102: box.reset();
103: }
104: }
105:
106:
107:
108:
109:
110:
111:
112:
113: int BOARD::domove(size_t y, size_t x, int dir, char c)
114: {
115: int closed = 0;
116:
117:
118: if (!bounds(y, x))
119: return -1;
120:
121: BOX box1(y, x, *this);
122:
123:
124: if (box1.isset(dir))
125: return -1;
126:
127: box1.set(dir);
128:
129: if (box1.count() == 4) {
130:
131: box1.name() = c;
132: closed++;
133: }
134:
135: box1.paint();
136:
137:
138: x += BOX::edges[dir].x;
139: y += BOX::edges[dir].y;
140:
141: if (bounds(y, x)) {
142: BOX box2(y, x, *this);
143: if (box2.count() == 4) {
144: box2.name() = c;
145: box2.paint();
146: closed++;
147: }
148: }
149: return closed;
150: }
151:
152:
153: int BOARD::full(void) const
154: {
155: for (size_t y = 0; y < _ny; y++)
156: for (size_t x = 0; x < _nx; x++) {
157: BOX box(y, x, (BOARD&) *this);
158: if (box.count() != 4)
159: return 0;
160: }
161: return 1;
162: }
163:
164:
165:
166: int BOARD::bounds(size_t y, size_t x) const
167: {
168: return x < _nx && y < _ny;
169: }
170:
171:
172: void BOARD::paint(void) const
173: {
174: for (size_t y = 0; y < _ny; y++)
175: for (size_t x = 0; x < _nx; x++) {
176: BOX box(y, x, (BOARD&) *this);
177: box.paint();
178: }
179: }
180:
181:
182: void BOARD::clean(void) const
183: {
184: if (!_scrn)
185: return;
186: _scrn->clean();
187: }
188:
189:
190: void BOARD::setpos(size_t y, size_t x) const
191: {
192: if (!_scrn)
193: return;
194: _scrn->moveto(y, x);
195: _scrn->redraw();
196: }
197:
198:
199: int BOARD::getmove(void) const
200: {
201: if (!_scrn)
202: return 'q';
203: _scrn->redraw();
204: return _scrn->getinput();
205: }
206:
207:
208: void BOARD::bell(void) const
209: {
210: if (!_scrn)
211: return;
212: _scrn->bell();
213: }
214:
215:
216: void BOARD::score(size_t i, const PLAYER& p)
217: {
218: if (_scrn == NULL)
219: return;
220: _scrn->score(i, p);
221: }
222:
223:
224: void BOARD::games(size_t i, const PLAYER& p)
225: {
226: if (_scrn == NULL)
227: return;
228: _scrn->games(i, p);
229: }
230:
231:
232: void BOARD::total(size_t i, const PLAYER& p)
233: {
234: if (_scrn == NULL)
235: return;
236: _scrn->total(i, p);
237: }
238:
239:
240: void BOARD::ties(const PLAYER& p)
241: {
242: if (_scrn == NULL)
243: return;
244: _scrn->ties(p);
245: }
246:
247:
248: void BOARD::abort(const char* s, ...) const
249: {
250: for (size_t i = 0; i < _ny; i++)
251: fprintf(stderr, "\n");
252:
253: va_list ap;
254: fprintf(stderr, "Algorithm internal error: ");
255: va_start(ap, s);
256: vfprintf(stderr, s, ap);
257: va_end(ap);
258: fprintf(stderr, "\n");
259: ::abort();
260: }