/aller/src/strategy/MoveGen.cpp
C++ | 186 lines | 163 code | 13 blank | 10 comment | 54 complexity | e02c8603b97cfe5c5f6edc33b16bdb94 MD5 | raw file
- /*
- * MoveGen.cpp
- *
- * Created on: Jun 3, 2011
- * Author: hongyang
- */
- #include "MoveGen.h"
- #include "../evaluation/Evaluation.h"
- #include <algorithm>
- namespace {
- static const bool DEBUG_MOVEGEN = false;
- }
- MoveGen::MoveGen(const RegionBoard& r) :
- regionBoard(r), board(r.getBoard()), pm(board) {
- }
- MoveGen::~MoveGen() {
- // TODO Auto-generated destructor stub
- }
- void MoveGen::genMoveList(SgPoint* plist) {
- int M = 40;
- int psize = 0;
- SgBlackWhite color = board.toPlay();
- int initialEva = Evaluation(regionBoard).value();
- if (color == SG_WHITE)
- initialEva = -initialEva;
- while (plist[psize] != SG_NULLPOINT)
- ++psize;
- if (DEBUG_MOVEGEN)
- SgDebug() << "Psize: " << psize << "\n";
- int size = 0;
- MoveListValue list[SG_MAX_SIZE * SG_MAX_SIZE];
- int plus = 5;
- if (board.stage() == START && psize > 0) {
- listSize = 1;
- Move* move = new Move(color, plist[0]);
- moveList[0] = MoveListValue(*move, 1000);
- return;
- }
- for (int i = 0; i < M && i < psize; ++i)
- if (board.isLegal(plist[i], color) && !board.isFill(plist[i], color)) {
- Move move(color, plist[i]);
- Board nextBoard;
- nextBoard.copy(board);
- RegionBoard nextRegionBoard(nextBoard);
- nextRegionBoard.copy(regionBoard);
- nextBoard.play(move);
- nextRegionBoard.onExecMove(move);
- int eva = Evaluation(nextRegionBoard).value();
- // int eva = Evaluation(regionBoard).value();
- if (color == SG_WHITE)
- eva = -eva;
- list[size++] = MoveListValue(move, eva + plus);
- if (DEBUG_MOVEGEN)
- SgDebug() << "P: " << SgWritePoint(plist[i]) << eva + plus
- << "\n";
- }
- for (int ttt = 1; ttt < M - psize; ttt++) {
- int x, y;
- SgPoint pp;
- int count = 0;
- do {
- x = rand() % SG_MAX_SIZE + 1;
- y = rand() % SG_MAX_SIZE + 1;
- pp = SgPointUtil::Pt(x, y);
- } while (count++ < 100 && (!board.isLegal(pp, color) || board.isFill(
- pp, color)));
- SgPoint point = SgPointUtil::Pt(x, y);
- if (board.isLegal(point, color) && !board.isFill(point, color)) {
- Move move(color, point);
- Board nextBoard;
- nextBoard.copy(board);
- RegionBoard nextRegionBoard(nextBoard);
- nextRegionBoard.copy(regionBoard);
- nextBoard.play(move);
- nextRegionBoard.onExecMove(move);
- int eva = Evaluation(nextRegionBoard).value();
- // int eva = Evaluation(regionBoard).value();
- if (color == SG_WHITE)
- eva = -eva;
- list[size++] = MoveListValue(move, eva);
- }
- }
- std::sort(list, list + size);
- for (int i = size - 1; i >= 0; --i)
- if (list[i].eva <= initialEva)
- --size;
- if (size < 5) {
- for (int i = 0; i < size; ++i)
- moveList[i] = list[i];
- listSize = size;
- } else {
- for (int i = 0; i < 5; ++i)
- moveList[i] = list[i];
- listSize = 5;
- }
- if (DEBUG_MOVEGEN)
- SgDebug() << "---------------initial " << initialEva << "\n";
- }
- Move MoveGen::nextMove() {
- // try kill
- Move m = killFirst();
- if (m.Point() != SG_NULLPOINT)
- return m;
- return mc.nextMove(regionBoard);
- SgPoint tmplist[2];
- tmplist[0] = SG_NULLPOINT;
- SgPoint* plist = tmplist;
- if (board.stage() != DENOUMONE)
- plist = pm.match();
- genMoveList(plist);
- if (listSize != 0) {
- int tmp = rand() % listSize;
- if (DEBUG_MOVEGEN)
- SgDebug() << "----------------------------------------------- "
- << moveList[tmp].eva << "\n";
- return moveList[tmp].move;
- }
- m = fillLast();
- if (m.Point() != SG_NULLPOINT)
- return m;
- return Move(board.toPlay(), SG_PASS);
- }
- Move MoveGen::mcNextMove() {
- }
- void MoveGen::findMoves() {
- SgPoint tmplist[2];
- tmplist[0] = SG_NULLPOINT;
- SgPoint* plist = tmplist;
- if (board.stage() != DENOUMONE)
- plist = pm.match();
- genMoveList(plist);
- }
- Move MoveGen::killFirst() {
- SgBlackWhite c = board.toPlay(), opp = SgOppBW(c);
- const BlockList& list = board.getAllBlocks(opp);
- int num = 0;
- SgPoint p = SG_NULLPOINT;
- for (BlockList::Iterator bit(list); bit; ++bit) {
- const Block& b = *bit;
- if (b.color() == opp && b.numLib() == 1 && b.numPoints() > num) {
- SgPoint k = b.getLibs()[0];
- if (board.isLegal(k, c)) {
- p = k;
- num = b.numPoints();
- }
- }
- }
- return Move(c, p);
- }
- Move MoveGen::fillLast() {
- SgBlackWhite c = board.toPlay();
- const BlockList& list = board.getAllBlocks(c);
- SgPoint p = SG_NULLPOINT;
- int num = 0;
- for (BlockList::Iterator bit(list); bit; ++bit) {
- const Block& b = *bit;
- if (b.numLib() == 1 && b.numPoints() > num) {
- SgPoint f = b.getLibs()[0];
- if (board.isLegal(f, c)) {
- p = f;
- num = b.numPoints();
- }
- }
- }
- return Move(c, p);
- }
- MoveList MoveGen::getMoveList() {
- MoveList ret;
- ret.Clear();
- for (int i = 0; i < listSize; ++i)
- ret.PushBack(moveList[i].move);
- return ret;
- }