board.cc 5.8 KB

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