PageRenderTime 40ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/aller/src/strategy/MoveGen.cpp

http://aller.googlecode.com/
C++ | 186 lines | 163 code | 13 blank | 10 comment | 54 complexity | e02c8603b97cfe5c5f6edc33b16bdb94 MD5 | raw file
  1. /*
  2. * MoveGen.cpp
  3. *
  4. * Created on: Jun 3, 2011
  5. * Author: hongyang
  6. */
  7. #include "MoveGen.h"
  8. #include "../evaluation/Evaluation.h"
  9. #include <algorithm>
  10. namespace {
  11. static const bool DEBUG_MOVEGEN = false;
  12. }
  13. MoveGen::MoveGen(const RegionBoard& r) :
  14. regionBoard(r), board(r.getBoard()), pm(board) {
  15. }
  16. MoveGen::~MoveGen() {
  17. // TODO Auto-generated destructor stub
  18. }
  19. void MoveGen::genMoveList(SgPoint* plist) {
  20. int M = 40;
  21. int psize = 0;
  22. SgBlackWhite color = board.toPlay();
  23. int initialEva = Evaluation(regionBoard).value();
  24. if (color == SG_WHITE)
  25. initialEva = -initialEva;
  26. while (plist[psize] != SG_NULLPOINT)
  27. ++psize;
  28. if (DEBUG_MOVEGEN)
  29. SgDebug() << "Psize: " << psize << "\n";
  30. int size = 0;
  31. MoveListValue list[SG_MAX_SIZE * SG_MAX_SIZE];
  32. int plus = 5;
  33. if (board.stage() == START && psize > 0) {
  34. listSize = 1;
  35. Move* move = new Move(color, plist[0]);
  36. moveList[0] = MoveListValue(*move, 1000);
  37. return;
  38. }
  39. for (int i = 0; i < M && i < psize; ++i)
  40. if (board.isLegal(plist[i], color) && !board.isFill(plist[i], color)) {
  41. Move move(color, plist[i]);
  42. Board nextBoard;
  43. nextBoard.copy(board);
  44. RegionBoard nextRegionBoard(nextBoard);
  45. nextRegionBoard.copy(regionBoard);
  46. nextBoard.play(move);
  47. nextRegionBoard.onExecMove(move);
  48. int eva = Evaluation(nextRegionBoard).value();
  49. // int eva = Evaluation(regionBoard).value();
  50. if (color == SG_WHITE)
  51. eva = -eva;
  52. list[size++] = MoveListValue(move, eva + plus);
  53. if (DEBUG_MOVEGEN)
  54. SgDebug() << "P: " << SgWritePoint(plist[i]) << eva + plus
  55. << "\n";
  56. }
  57. for (int ttt = 1; ttt < M - psize; ttt++) {
  58. int x, y;
  59. SgPoint pp;
  60. int count = 0;
  61. do {
  62. x = rand() % SG_MAX_SIZE + 1;
  63. y = rand() % SG_MAX_SIZE + 1;
  64. pp = SgPointUtil::Pt(x, y);
  65. } while (count++ < 100 && (!board.isLegal(pp, color) || board.isFill(
  66. pp, color)));
  67. SgPoint point = SgPointUtil::Pt(x, y);
  68. if (board.isLegal(point, color) && !board.isFill(point, color)) {
  69. Move move(color, point);
  70. Board nextBoard;
  71. nextBoard.copy(board);
  72. RegionBoard nextRegionBoard(nextBoard);
  73. nextRegionBoard.copy(regionBoard);
  74. nextBoard.play(move);
  75. nextRegionBoard.onExecMove(move);
  76. int eva = Evaluation(nextRegionBoard).value();
  77. // int eva = Evaluation(regionBoard).value();
  78. if (color == SG_WHITE)
  79. eva = -eva;
  80. list[size++] = MoveListValue(move, eva);
  81. }
  82. }
  83. std::sort(list, list + size);
  84. for (int i = size - 1; i >= 0; --i)
  85. if (list[i].eva <= initialEva)
  86. --size;
  87. if (size < 5) {
  88. for (int i = 0; i < size; ++i)
  89. moveList[i] = list[i];
  90. listSize = size;
  91. } else {
  92. for (int i = 0; i < 5; ++i)
  93. moveList[i] = list[i];
  94. listSize = 5;
  95. }
  96. if (DEBUG_MOVEGEN)
  97. SgDebug() << "---------------initial " << initialEva << "\n";
  98. }
  99. Move MoveGen::nextMove() {
  100. // try kill
  101. Move m = killFirst();
  102. if (m.Point() != SG_NULLPOINT)
  103. return m;
  104. return mc.nextMove(regionBoard);
  105. SgPoint tmplist[2];
  106. tmplist[0] = SG_NULLPOINT;
  107. SgPoint* plist = tmplist;
  108. if (board.stage() != DENOUMONE)
  109. plist = pm.match();
  110. genMoveList(plist);
  111. if (listSize != 0) {
  112. int tmp = rand() % listSize;
  113. if (DEBUG_MOVEGEN)
  114. SgDebug() << "----------------------------------------------- "
  115. << moveList[tmp].eva << "\n";
  116. return moveList[tmp].move;
  117. }
  118. m = fillLast();
  119. if (m.Point() != SG_NULLPOINT)
  120. return m;
  121. return Move(board.toPlay(), SG_PASS);
  122. }
  123. Move MoveGen::mcNextMove() {
  124. }
  125. void MoveGen::findMoves() {
  126. SgPoint tmplist[2];
  127. tmplist[0] = SG_NULLPOINT;
  128. SgPoint* plist = tmplist;
  129. if (board.stage() != DENOUMONE)
  130. plist = pm.match();
  131. genMoveList(plist);
  132. }
  133. Move MoveGen::killFirst() {
  134. SgBlackWhite c = board.toPlay(), opp = SgOppBW(c);
  135. const BlockList& list = board.getAllBlocks(opp);
  136. int num = 0;
  137. SgPoint p = SG_NULLPOINT;
  138. for (BlockList::Iterator bit(list); bit; ++bit) {
  139. const Block& b = *bit;
  140. if (b.color() == opp && b.numLib() == 1 && b.numPoints() > num) {
  141. SgPoint k = b.getLibs()[0];
  142. if (board.isLegal(k, c)) {
  143. p = k;
  144. num = b.numPoints();
  145. }
  146. }
  147. }
  148. return Move(c, p);
  149. }
  150. Move MoveGen::fillLast() {
  151. SgBlackWhite c = board.toPlay();
  152. const BlockList& list = board.getAllBlocks(c);
  153. SgPoint p = SG_NULLPOINT;
  154. int num = 0;
  155. for (BlockList::Iterator bit(list); bit; ++bit) {
  156. const Block& b = *bit;
  157. if (b.numLib() == 1 && b.numPoints() > num) {
  158. SgPoint f = b.getLibs()[0];
  159. if (board.isLegal(f, c)) {
  160. p = f;
  161. num = b.numPoints();
  162. }
  163. }
  164. }
  165. return Move(c, p);
  166. }
  167. MoveList MoveGen::getMoveList() {
  168. MoveList ret;
  169. ret.Clear();
  170. for (int i = 0; i < listSize; ++i)
  171. ret.PushBack(moveList[i].move);
  172. return ret;
  173. }