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