algor.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* $NetBSD: algor.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. * algor.C: Computer algorithm
  39. */
  40. #include "defs.h"
  41. RCSID("$NetBSD: algor.cc,v 1.1 2003/12/27 01:16:55 christos Exp $")
  42. #include "algor.h"
  43. #include "board.h"
  44. #include "box.h"
  45. #include "random.h"
  46. ALGOR::ALGOR(const char c) : PLAYER(c)
  47. {
  48. #ifdef notyet
  49. // Single Edges = (x + y) * 2
  50. _edge1 = (_b.nx() * _b.ny()) * 2;
  51. // Shared Edges = (x * (y - 1)) + ((x - 1) * y)
  52. _edge2 = (_b.nx() * (_b.ny() - 1)) + ((_b.nx() - 1) * _b.ny());
  53. // Maximum Edges filled before closure = x * y * 2
  54. _maxedge = _b.nx() * _b.ny() * 2;
  55. #endif
  56. }
  57. // Find the first closure, i.e. a box that has 3 edges
  58. int ALGOR::find_closure(size_t& y, size_t& x, int& dir, BOARD& b)
  59. {
  60. RANDOM rdy(b.ny()), rdx(b.nx());
  61. for (y = rdy(); y < b.ny(); y = rdy()) {
  62. rdx.clear();
  63. for (x = rdx(); x < b.nx(); x = rdx()) {
  64. BOX box(y, x, b);
  65. if (box.count() == 3) {
  66. for (dir = BOX::first; dir < BOX::last; dir++)
  67. if (!box.isset(dir))
  68. return 1;
  69. b.abort("find_closure: 3 sided box[%d,%d] has no free sides",
  70. y, x);
  71. }
  72. }
  73. }
  74. return 0;
  75. }
  76. #if 0
  77. size_t ALGOR::find_single()
  78. {
  79. size_t ne;
  80. // Find the number of single edges in use
  81. for (size_t x = 0; x < b.nx(); x++) {
  82. BOX tbox(0, x, b);
  83. ne += tbox.isset(BOX::top);
  84. BOX bbox(b.ny() - 1, x, b);
  85. ne += bbox.isset(BOX::bottom);
  86. }
  87. for (size_t y = 0; y < _b.ny(); y++) {
  88. BOX lbox(y, 0, b);
  89. ne += lbox.isset(BOX::left);
  90. BOX rbox(y,_b.nx() - 1, b);
  91. ne += rbox.isset(BOX::right);
  92. }
  93. return ne;
  94. }
  95. #endif
  96. // Count a closure, by counting all boxes that we can close in the current
  97. // move
  98. size_t ALGOR::count_closure(size_t& y, size_t& x, int& dir, BOARD& b)
  99. {
  100. size_t i = 0;
  101. size_t tx, ty;
  102. int tdir, mv;
  103. while (find_closure(ty, tx, tdir, b)) {
  104. if (i == 0) {
  105. // Mark the beginning of the closure
  106. x = tx;
  107. y = ty;
  108. dir = tdir;
  109. }
  110. if ((mv = b.domove(ty, tx, tdir, getWho())) == -1)
  111. b.abort("count_closure: Invalid move (%d, %d, %d)", y, x, dir);
  112. else
  113. i += mv;
  114. }
  115. return i;
  116. }
  117. /*
  118. * Find the largest closure, by closing all possible closures.
  119. * return the number of boxes closed in the maximum closure,
  120. * and the first box of the maximum closure in (x, y, dir)
  121. */
  122. int ALGOR::find_max_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
  123. {
  124. BOARD nb(b);
  125. int tdir, maxdir = -1;
  126. size_t nbox, maxbox = 0;
  127. size_t tx, ty, maxx = ~0, maxy = ~0;
  128. while ((nbox = count_closure(ty, tx, tdir, nb)) != 0)
  129. if (nbox > maxbox) {
  130. // This closure is better, update max
  131. maxbox = nbox;
  132. maxx = tx;
  133. maxy = ty;
  134. maxdir = tdir;
  135. }
  136. // Return the max found
  137. y = maxy;
  138. x = maxx;
  139. dir = maxdir;
  140. return maxbox;
  141. }
  142. // Find if a turn does not result in a capture on the given box
  143. // and return the direction if found.
  144. int ALGOR::try_good_turn(BOX& box, size_t y, size_t x, int& dir, BOARD& b)
  145. {
  146. // Sanity check; we must have a good box
  147. if (box.count() >= 2)
  148. b.abort("try_good_turn: box[%d,%d] has more than 2 sides occupied",
  149. y, x);
  150. // Make sure we don't make a closure in an adjacent box.
  151. // We use a random direction to randomize the game
  152. RANDOM rd(BOX::last);
  153. for (dir = rd(); dir < BOX::last; dir = rd())
  154. if (!box.isset(dir)) {
  155. size_t by = y + BOX::edges[dir].y;
  156. size_t bx = x + BOX::edges[dir].x;
  157. if (!b.bounds(by, bx))
  158. return 1;
  159. BOX nbox(by, bx, b);
  160. if (nbox.count() < 2)
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. // Try to find a turn that does not result in an opponent closure, and
  166. // return it in (x, y, dir); if not found return 0.
  167. int ALGOR::find_good_turn(size_t& y, size_t& x, int& dir, const BOARD& b)
  168. {
  169. BOARD nb(b);
  170. RANDOM rdy(b.ny()), rdx(b.nx());
  171. for (y = rdy(); y < b.ny(); y = rdy()) {
  172. rdx.clear();
  173. for (x = rdx(); x < b.nx(); x = rdx()) {
  174. BOX box(y, x, nb);
  175. if (box.count() < 2 && try_good_turn(box, y, x, dir, nb))
  176. return 1;
  177. }
  178. }
  179. return 0;
  180. }
  181. // On a box with 2 edges, return the first or the last free edge, depending
  182. // on the order specified
  183. int ALGOR::try_bad_turn(BOX& box, size_t& y, size_t& x, int& dir, BOARD& b,
  184. int last)
  185. {
  186. if (4 - box.count() <= last)
  187. b.abort("try_bad_turn: Called at [%d,%d] for %d with %d",
  188. y, x, last, box.count());
  189. for (dir = BOX::first; dir < BOX::last; dir++)
  190. if (!box.isset(dir)) {
  191. if (!last)
  192. return 1;
  193. else
  194. last--;
  195. }
  196. return 0;
  197. }
  198. // Find a box that has 2 edges and return the first free edge of that
  199. // box or the last free edge of that box
  200. int ALGOR::find_bad_turn(size_t& y, size_t& x, int& dir, BOARD& b, int last)
  201. {
  202. RANDOM rdy(b.ny()), rdx(b.nx());
  203. for (y = rdy(); y < b.ny(); y = rdy()) {
  204. rdx.clear();
  205. for (x = rdx(); x < b.nx(); x = rdx()) {
  206. BOX box(y, x, b);
  207. if ((4 - box.count()) > last &&
  208. try_bad_turn(box, y, x, dir, b, last))
  209. return 1;
  210. }
  211. }
  212. return 0;
  213. }
  214. int ALGOR::find_min_closure1(size_t& y, size_t& x, int& dir, const BOARD& b,
  215. int last)
  216. {
  217. BOARD nb(b);
  218. int tdir, mindir = -1, xdir, mv;
  219. // number of boxes per closure
  220. size_t nbox, minbox = nb.nx() * nb.ny() + 1;
  221. size_t tx, ty, minx = ~0, miny = ~0;
  222. while (find_bad_turn(ty, tx, tdir, nb, last)) {
  223. // Play a bad move that would cause the opponent's closure
  224. if ((mv = nb.domove(ty, tx, tdir, getWho())) != 0)
  225. b.abort("find_min_closure1: Invalid move %d (%d, %d, %d)", mv,
  226. ty, tx, tdir);
  227. // Count the opponent's closure
  228. if ((nbox = count_closure(y, x, xdir, nb)) == 0)
  229. b.abort("find_min_closure1: no closure found");
  230. if (nbox <= minbox) {
  231. // This closure has fewer boxes
  232. minbox = nbox;
  233. minx = tx;
  234. miny = ty;
  235. mindir = tdir;
  236. }
  237. }
  238. y = miny;
  239. x = minx;
  240. dir = mindir;
  241. return minbox;
  242. }
  243. // Search for the move that makes the opponent close the least number of
  244. // boxes; returns 1 if a move found, 0 otherwise
  245. int ALGOR::find_min_closure(size_t& y, size_t& x, int& dir, const BOARD& b)
  246. {
  247. size_t x1, y1;
  248. int dir1;
  249. int count = b.ny() * b.nx() + 1, count1;
  250. for (size_t i = 0; i < 3; i++)
  251. if (count > (count1 = find_min_closure1(y1, x1, dir1, b, i))) {
  252. count = count1;
  253. y = y1;
  254. x = x1;
  255. dir = dir1;
  256. }
  257. return (size_t) count != b.ny() * b.nx() + 1;
  258. }
  259. // Return a move in (y, x, dir)
  260. void ALGOR::play(const BOARD& b, size_t& y, size_t& x, int& dir)
  261. {
  262. // See if we can close the largest closure available
  263. if (find_max_closure(y, x, dir, b))
  264. return;
  265. #ifdef notyet
  266. size_t sgl = find_single();
  267. size_t dbl = find_double();
  268. #endif
  269. // See if we can play an edge without giving the opponent a box
  270. if (find_good_turn(y, x, dir, b))
  271. return;
  272. // Too bad, find the move that gives the opponent the fewer boxes
  273. if (find_min_closure(y, x, dir, b))
  274. return;
  275. }