本文整理汇总了C++中MoveList::getNext方法的典型用法代码示例。如果您正苦于以下问题:C++ MoveList::getNext方法的具体用法?C++ MoveList::getNext怎么用?C++ MoveList::getNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MoveList
的用法示例。
在下文中一共展示了MoveList::getNext方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: randomMove
Move Board::randomMove()
{
Move m;
MoveList list;
generateMoves(list);
int l = list.getLength();
int j = (::rand() % l) +1;
while(j != 0) {
list.getNext(m, Move::none);
j--;
}
return m;
}
示例2: randomMove
Move Board::randomMove()
{
Move m;
MoveList list;
generateMoves(list);
int l = list.getLength();
// FIXME: start with random seed using qsrand() somewhere
int j = (qrand() % l) +1;
while(j != 0) {
list.getNext(m, Move::none);
j--;
}
return m;
}
示例3: randomMove
Move Board::randomMove()
{
static int i = 999;
unsigned int j,l;
Move m;
MoveList list;
/* we prefer to use QT... */
j = (QTime::currentTime()).msec();
generateMoves(list);
l = list.getLength();
j = (j + i) % l +1;
if ( (i+=7)>10000) i-=10000;
while(j != 0) {
list.getNext(m, Move::none);
j--;
}
return m;
}
示例4: minimax
int MinimaxStrategy::minimax()
{
Move m;
MoveList list;
// int maxEval, minEval;
int bestEval;
int eval;
int sign;
if(current_depth == MAX_DEPTH)
return evaluate();
bestEval = -17000;
// maxEval = -17000;
// minEval = 17000;
generateMoves(list);
if(evaluate() == 16000)
{
if(current_depth == 0)
finishedNode(0,0);
pretty_print("current_depth", current_depth);
return ((current_depth % 2) ==0) ? -16000 : 16000;
}
if((MAX_DEPTH-current_depth)%2 == 1)
sign = 1;
else
sign = -1;
while(list.getNext(m))
{
if(current_depth < MAX_DEPTH)
{
current_depth++;
playMove(m);
eval=minimax();
takeBack();
current_depth--;
}
if(sign*eval > bestEval)
{
bestEval = sign*eval;
if(unlikely(current_depth == 0)) {
pretty_print("Eval", bestEval);
foundBestMove(0, m, eval);
}
}
#if 0
if((MAX_DEPTH - current_depth +1) % 2 == 0)
{
if(eval > maxEval)
{
maxEval=eval;
if(current_depth == 0) {
pretty_print("Eval", eval);
foundBestMove(0, m, eval);
}
}
}
else
{
if(eval < minEval)
{
minEval=eval;
if(current_depth == 0) {
pretty_print("Eval2", eval);
foundBestMove(0, m, eval);
}
}
}
#endif
}
bestEval = sign*bestEval;
if(current_depth == 0)
finishedNode(0,0);
#if 0
if((MAX_DEPTH - current_depth +1) % 2 == 0)
return maxEval;
else
return minEval;
#endif
return bestEval;
}
示例5: alphabeta
/*
* Alpha/Beta search
*
* - first, start with principal variation
* - depending on depth, we only do depth search for some move types
*/
int ABIDStrategy::alphabeta(int depth, int alpha, int beta)
{
int currentValue = -14999+depth, value;
Move m;
Move bestMove;
MoveList list;
bool depthPhase, doDepthSearch;
int i=0;
int movecounter =-1;
int flag=0;
/* We make a depth search for the following move types... */
int maxType = (depth < _currentMaxDepth-1) ? Move::maxMoveType :
(depth < _currentMaxDepth) ? Move::maxPushType :
Move::maxOutType;
_board->generateMoves(list);
if (_sc && _sc->verbose()) {
char tmp[100];
sprintf(tmp, "Alpha/Beta [%d;%d], %d moves (%d depth)", alpha, beta,
list.count(Move::none), list.count(maxType));
_sc->startedNode(depth, tmp);
}
/* check for an old best move in principal variation */
if (_inPV) {
m = _pv[depth];
if ((m.type != Move::none) &&
(!list.isElement(m, 0, true)))
m.type = Move::none;
if (m.type == Move::none) _inPV = false;
}
// first, play all moves with depth search
depthPhase = true;
while (1) {
pretty_print("movecounter", movecounter);
movecounter++;
// get next move
if (m.type == Move::none) {
if (depthPhase)
depthPhase = list.getNext(m, maxType);
if (!depthPhase)
if (!list.getNext(m, Move::none)) break;
}
if((thread_rank == movecounter% num_threads) || (depth > 0))// we could start with a non-depth move from principal variation
{
doDepthSearch = depthPhase && (m.type <= maxType);
_board->playMove(m);
/* check for a win position first */
if (!_board->isValid()) {
/* Shorter path to win position is better */
value = 14999-depth;
}
else {
if (doDepthSearch) {
/* opponent searches for its maximum; but we want the
* minimum: so change sign (for alpha/beta window too!)
*/
value = -alphabeta(depth+1, -beta, -alpha);
}
else {
value = evaluate();
}
}
_board->takeBack();
/* best move so far? */
if (value > currentValue) {
currentValue = value;
_pv.update(depth, m);
if (_sc) _sc->foundBestMove(depth, m, currentValue);
if (depth == 0)
_currentBestMove = m;
/* alpha/beta cut off or win position ... */
if (currentValue>14900 || currentValue >= beta) {
if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
flag = 1;
break;
//return currentValue;
}
//.........这里部分代码省略.........
示例6: search
/*
* We always try the maximize the board value
*/
int Board::search(int depth, int alpha, int beta)
{
int actValue= -14999+depth, value;
Move m;
MoveList list;
bool depthPhase, doDepthSearch;
searchCalled++;
/* We make a depth search for the following move types... */
int maxType = (depth < maxDepth-1) ? Move::maxMoveType() :
(depth < maxDepth) ? Move::maxPushType() :
Move::maxOutType();
generateMoves(list);
#ifdef MYTRACE
int oldRatedPositions;
int oldWonPositions;
int oldSearchCalled;
int oldMoveCount;
int oldNormalCount;
int oldPushCount;
int oldOutCount;
int oldCutoffCount;
spyDepth = depth;
moveCount += list.getLength();
/*
if (spyLevel>1) {
indent(depth);
qDebug("%s (%6d .. %6d) MaxType %s\n", depth,
(color==color1)?"O":"X", alpha,beta,
(depth < maxDepth-1) ? "Moving" :
(depth < maxDepth)? "Pushing" : "PushOUT" );
}
*/
#endif
/* check for a old best move in main combination */
if (inPrincipalVariation) {
m = pv[depth];
if ((m.type != Move::none) &&
(!list.isElement(m, 0, true)))
m.type = Move::none;
if (m.type == Move::none)
inPrincipalVariation = false;
#ifdef MYTRACE
else {
if (spyLevel>1) {
indent(spyDepth);
qDebug("Got from pv !\n" );
}
}
#endif
}
// first, play all moves with depth search
depthPhase = true;
while (1) {
// get next move
if (m.type == Move::none) {
if (depthPhase)
depthPhase = list.getNext(m, maxType);
if (!depthPhase)
if (!list.getNext(m, Move::none)) break;
}
// we could start with a non-depth move from principal variation
doDepthSearch = depthPhase && (m.type <= maxType);
#ifdef MYTRACE
if (m.isOutMove()) outCount++;
else if (m.isPushMove()) pushCount++;
else normalCount++;
if (doDepthSearch) {
oldRatedPositions = ratedPositions;
oldWonPositions = wonPositions;
oldSearchCalled = searchCalled;
oldMoveCount = moveCount;
oldNormalCount = normalCount;
oldPushCount = pushCount;
oldOutCount = outCount;
oldCutoffCount = cutoffCount;
if (spyLevel>1) {
indent(spyDepth);
qDebug("%s [%6d .. %6d] ",
//.........这里部分代码省略.........
示例7: pv_split
int ABIDStrategy::pv_split(int alpha0, int beta0)
{
bool cutoff;
int depth;
int value;
int currentValue = -15000;
int slave_id;
int num_slaves;
int pending_jobs;
Slave_Input slave_input;
Slave_Output *slave_output;
MPI_Request *rcv_rq;
MoveList list;
Move m, *movechain;
int *alpha, *beta;
rcv_rq = (MPI_Request *) malloc (num_threads * sizeof(MPI_Request));
slave_output = (Slave_Output *) malloc (num_threads * sizeof(Slave_Output));
movechain = (Move*) malloc ((_currentMaxDepth+1)* sizeof (Move));
alpha = (int*) malloc((_currentMaxDepth+2)*sizeof(int));
beta = (int*) malloc((_currentMaxDepth+2)*sizeof(int));
alpha[0] = alpha0;
beta[0] = beta0;
_currentBestMove.type = Move::none;
// Play moves from the pv until you reach the lowest level
// If pv-moves are not possible use random moves
// Store the sequence of moves in movechain
depth = 0;
while (depth < (_currentMaxDepth-1))
{
list.clear();
_board->generateMoves(list);
m = _pv[depth];
// check if pv-move is possible
if ((m.type != Move::none) && (!list.isElement(m, 0, false)))
m.type = Move::none;
// get random move if pv-move is not possible
if (m.type == Move::none)
list.getNext(m, Move::none);
_board->playMove(m);
movechain[depth] = m;
alpha[depth+1] = -beta[depth];
beta[depth+1] = -alpha[depth];
depth++;
}
// Start at the second lowest level and move back up the gametree
// Devide the work at each level
depth = _currentMaxDepth-1;
while (depth >= 0)
{
slave_id = 0;
list.clear();
_board->generateMoves(list);
// delete the move we already checked from the list
if (depth < _currentMaxDepth-1)
list.isElement(movechain[depth], 0, true);
strcpy(slave_input.boardstate,_board->getState());
slave_input.alpha = -beta[depth];
slave_input.beta = -alpha[depth];
slave_input.depth = depth+1;
slave_input.currentMaxDepth = _currentMaxDepth;
printf("Thread 0 testing %d moves at depth = %d\n",list.getLength(), depth);
while ( list.getNext(m, Move::none) )
{
slave_input.move = m;
MPI_Send(&slave_input, sizeof(Slave_Input), MPI_BYTE, slave_id+1, 10, MPI_COMM_WORLD);
MPI_Irecv(&slave_output[slave_id], sizeof(Slave_Output), MPI_BYTE, slave_id+1,
10, MPI_COMM_WORLD, &rcv_rq[slave_id]);
slave_id++;
if (slave_id >= (num_threads-1))
break;
}
num_slaves = slave_id;
pending_jobs = num_slaves;
cutoff = false;
while (pending_jobs > 0)
{
MPI_Waitany(num_slaves, rcv_rq, &slave_id, MPI_STATUS_IGNORE);
_sc->_leavesVisited += slave_output[slave_id].num_leaves;
_sc->_nodesVisited += slave_output[slave_id].num_nodes;
value = slave_output[slave_id].eval;
if (value > currentValue)
{
currentValue = value;
_pv = slave_output[slave_id].pv;
//.........这里部分代码省略.........
示例8: alphabeta
/*
* Alpha/Beta search
*
* - first, start with principal variation
* - depending on depth, we only do depth search for some move types
*/
int ABIDStrategy::alphabeta(int depth, int alpha, int beta)
{
int currentValue = -14999+depth, value;
Move m;
MoveList list;
bool depthPhase, doDepthSearch;
/* We make a depth search for the following move types... */
int maxType = (depth < _currentMaxDepth-1) ? Move::maxMoveType :
(depth < _currentMaxDepth) ? Move::maxPushType :
Move::maxOutType;
_board->generateMoves(list);
if (_sc && _sc->verbose()) {
char tmp[100];
sprintf(tmp, "Alpha/Beta [%d;%d], %d moves (%d depth)", alpha, beta,
list.count(Move::none), list.count(maxType));
_sc->startedNode(depth, tmp);
}
// don't use moves from the principal variation
m.type = Move::none;
// first, play all moves with depth search
depthPhase = true;
while (1) {
// get next move
if (m.type == Move::none) {
if (depthPhase)
depthPhase = list.getNext(m, maxType);
if (!depthPhase)
if (!list.getNext(m, Move::none)) break;
}
// we could start with a non-depth move from principal variation
doDepthSearch = depthPhase && (m.type <= maxType);
_board->playMove(m);
/* check for a win position first */
if (!_board->isValid()) {
/* Shorter path to win position is better */
value = 14999-depth;
}
else {
if (doDepthSearch) {
/* opponent searches for its maximum; but we want the
* minimum: so change sign (for alpha/beta window too!)
*/
value = -alphabeta(depth+1, -beta, -alpha);
}
else {
value = evaluate();
}
}
_board->takeBack();
/* best move so far? */
if (value > currentValue) {
currentValue = value;
_pv.update(depth, m);
/* alpha/beta cut off or win position ... */
if (currentValue>14900 || currentValue >= beta) {
if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
return currentValue;
}
/* maximize alpha */
if (currentValue > alpha) alpha = currentValue;
}
if (_stopSearch) break; // depthPhase=false;
m.type = Move::none;
}
if (_sc) _sc->finishedNode(depth, _pv.chain(depth));
return currentValue;
}
示例9: moveToReach
Move Board::moveToReach(Board* b, bool fuzzy)
{
Move m;
/* can not move from invalid position */
int state = validState();
if ((state != valid1) && (state != valid2))
return m;
if (!fuzzy) {
if (b->moveNo() != _moveNo+1) {
if (_verbose)
printf("Board::moveToReach: moveNo %d => %d ?!\n",
_moveNo, b->moveNo());
return m;
}
/* only time left for player can have decreased */
int opponent = (color == color1) ? color2 : color1;
if (_msecsToPlay[opponent] != b->msecsToPlay(opponent)) {
if (_verbose)
printf("Board::moveToReach: Opponent time changed ?!\n");
return m;
}
if (_msecsToPlay[color] < b->msecsToPlay(color)) {
if (_verbose)
printf("Board::moveToReach: Player time increased ?!\n");
return m;
}
}
/* detect move drawn */
MoveList l;
generateMoves(l);
if (_verbose) {
printf("Board::moveToReach - %d allowed moves:\n",
l.getLength());
Move found;
int type = Move::none;
while(l.getNext(m, Move::maxMoveType)) {
playMove(m);
bool isSame = hasSameFields(b);
takeBack();
if (isSame) found = m;
if (m.type != type) {
type = m.type;
printf(" %s:\n", m.typeName());
}
printf(" %s%s\n", m.name(), isSame ? " <== Choosen":"");
}
m = found;
}
else {
while(l.getNext(m, Move::maxMoveType)) {
playMove(m);
bool isSame = hasSameFields(b);
takeBack();
if (isSame) break;
}
}
return m;
}
示例10: minimax
int MinimaxStrategy::minimax()
{
Move m;
Move bestestMove; //;-p This is the cumulative best move among all threads.
MoveList list;
int bestEval;
int eval;
int sign;
int move_counter=0;
int i=0;
if(current_depth == MAX_DEPTH)
return evaluate();
bestEval = -17000;
generateMoves(list);
if(evaluate() == 16000)
{
if(current_depth == 0)
finishedNode(0,0);
pretty_print("current_depth", current_depth);
return ((current_depth % 2) ==0) ? -16000 : 16000;
}
if(current_depth == 0)
{
for(i=0;i<thread_rank;i++)
list.getNext(m);
}
if((MAX_DEPTH-current_depth)%2 == 1)
sign = 1;
else
sign = -1;
while(list.getNext(m))
{
if(current_depth < MAX_DEPTH)
{
current_depth++;
playMove(m);
eval=minimax();
takeBack();
current_depth--;
}
if(sign*eval > bestEval)
{
bestEval = sign*eval;
if(unlikely(current_depth == 0)) {
pretty_print("Eval", bestEval);
foundBestMove(0, m, eval);
}
}
if(current_depth == 0)
{
for(i=1;i<num_threads;i++)
list.getNext(m);
}
}
bestEval = sign*bestEval;
if(current_depth == 0)
{
if(thread_rank==0)
{
Move *moves=NULL;
int *eval_results;
moves=(Move*)malloc((num_threads -1)*sizeof(Move));
eval_results=(int*)malloc((num_threads - 1)*sizeof(int));
//all threads send value to thread 0
for(int i=1;i<num_threads;i++)
{
MPI_Status status;
MPI_Recv((void*)&moves[i-1], sizeof(Move), MPI_BYTE, i, 10,
MPI_COMM_WORLD, &status);
MPI_Recv(&eval_results[i-1], 1, MPI_INT, i, 10,
MPI_COMM_WORLD, &status);
}
bestestMove=_bestMove;
for(int i=0;i<num_threads-1;i++)
{
if(sign*eval_results[i] > sign*bestEval)
{
bestEval = eval_results[i];
bestestMove=moves[i];
}
}
for(int i=1;i<num_threads;i++)
{
MPI_Send (&bestestMove, sizeof(Move), MPI_BYTE, i, 10,
MPI_COMM_WORLD);
MPI_Send (&bestEval, 1, MPI_INT, i, 10,
MPI_COMM_WORLD);
//.........这里部分代码省略.........
示例11: search
/*
* We always try the maximize the board value
*/
int Board::search(int depth, int alpha, int beta)
{
int actValue=-16000, value;
Move m;
MoveList list;
bool stop = false;
/* We make a depth search for the following move types... */
int maxType = (depth < maxDepth/2) ? Move::maxMoveType() :
(depth < maxDepth) ? Move::maxPushType() :
Move::maxOutType();
generateMoves(list);
#ifdef MYTRACE
printf(">>>>>>>> Depth %d\n", depth);
#endif
/* check for a old best move in main combination */
if (inMainCombination) {
m = mc[depth];
if (!list.isElement(m, 0))
m.type = Move::none;
if (m.type == Move::none)
inMainCombination = false;
if (m.type > maxType)
m.type = Move::none;
}
if (m.type == Move::none)
stop = !list.getNext(m, maxType);
/* depth first search */
while(!stop) {
#ifdef MYTRACE
indent(depth);
m.print();
printf("\n");
#endif
#ifdef SPION
if (bUpdateSpy) emit update(depth, 0, m, false);
#endif
playMove(m);
if (!isValid())
value = ((depth < maxDepth) ? 15999:14999) - depth;
else {
/* opponent searches for his maximum; but we won't the
* minimum: so change sign (for alpha/beta window too!)
*/
value = - search(depth+1,-beta,-alpha);
}
takeBack();
/* For GUI response */
if (maxDepth - depth >2)
emit searchBreak();
#ifdef MYTRACE
indent(depth);
printf("=> (%d - %d): Value %d [ %d ] for ",
alpha, beta, value,actValue);
m.print();
printf("\n");
#endif
#ifdef SPION
if (bUpdateSpy) {
if (value > actValue)
emit updateBest(depth, value, m, value >= beta);
emit update(depth, value, m, true);
}
#endif
if (value > actValue) {
actValue = value;
mc.update(depth, m);
if (bUpdateSpy && depth == 0)
emit updateBestMove(m, actValue);
if (actValue >= beta) {
#ifdef MYTRACE
indent(depth);
printf("CUTOFF\n");
#endif
return actValue;
}
if (actValue > alpha) alpha = actValue;
}
stop = (!list.getNext(m, maxType)) || breakOut;
}
/* other moves: calculate rating */
while(list.getNext(m, Move::none)) {
playMove(m);
//.........这里部分代码省略.........