BOOL BOARD::canCastle(MOVETYPE whichCastle, PIECECOLOR color) { int col = color == WHITE ? 0 : 7; int row, rowStep; BOARDMETRIC metric; if (whichCastle == QUEENSIDECASTLE) { row = 0; rowStep = 1; } else { row = 7; rowStep = -1; } // make sure king & rook in initial positions and have never been // moved. if (!brd[row][col]) return(FALSE); if (brd[row][col]->hasBeenMoved()) return(FALSE); if (!brd[4][col]) return(FALSE); if (brd[4][col]->hasBeenMoved()) return(FALSE); // make sure no pieces in between for ( ; ; ) { row += rowStep; if (row == 4) break; if (brd[row][col]) return(FALSE); } // make sure king is not in check findBestMoves(1, OtherColor(color), metric, (BESTMOVES *) 0); if (metric.kingSituation[color] == KINGLOST) return(FALSE); // make sure king would not be in check in intermediate position brd[4 - rowStep][col] = brd[4][col]; brd[4][col] = (PIECE *) 0; findBestMoves(1, OtherColor(color), metric, (BESTMOVES *) 0); brd[4][col] = brd[4 - rowStep][col]; brd[4 - rowStep][col] = (PIECE *) 0; return(metric.kingSituation[color] == KINGOK); }
/** * Makes the BRUE move */ int makeBrueMove(rep_t rep, int *side, int numIterations, heuristics_t heuristic, int* bestMoves, int* numBestMoves, int budget) { const int horizon = HORIZON; int i, switchingPoint; treeNode* root = createNode(rep,*side, NULL); *numBestMoves = 0; // reset size of set of best moves for (i = 0; i < numIterations; i++) { switchingPoint = getSwitchingPoint(i,horizon); performIteration(root,switchingPoint, heuristic, budget); } findBestMoves(root, bestMoves, numBestMoves); freeTree(root); return selectBestMove(bestMoves,*numBestMoves); }
MOVESTATUS BOARD::doUserMove ( POSITION start, POSITION end ) { PIECE *p = whatPiece(start.row, start.col); POSITIONLIST moves; BOARDMETRIC metric; BESTMOVES bestMoves; MOVEUNDODATA undoData; int m; if (!p) return(MOVESTATUS(ILLEGALMOVE)); p->legalMoves(start, *this, moves); for (m = 0; m < moves.nMoves; m++) { if ((moves.end[m].row == end.row) && (moves.end[m].col == end.col)) break; } if (m == moves.nMoves) return(MOVESTATUS(ILLEGALMOVE)); doMove(start, end, undoData); findBestMoves(1, OtherColor(p->whatColor()), metric, &bestMoves); if (metric.kingSituation[p->whatColor()] == KINGLOST) { undoMove(end, start, undoData); return(MOVESTATUS(WOULDLOSEKING, bestMoves.move[0].start)); } if (undoData.capturedPiece) delete undoData.capturedPiece; if (undoData.enPassantEffect == ENPASSANTCAPTURE) return(MOVESTATUS(MOVEENPASSANT)); else return(MOVESTATUS(MOVEDONE)); }
BOOL BOARD::userCanCastle ( MOVETYPE whichCastle, PIECECOLOR color ) { BOARDMETRIC metric; MOVEUNDODATA undoData; if (!canCastle(whichCastle, color)) return(FALSE); castle(whichCastle, color, undoData); // make sure king is not in check in final position findBestMoves(1, OtherColor(color), metric, (BESTMOVES *) 0); undoCastle(whichCastle, color, undoData); return(metric.kingSituation[color] == KINGOK); }
/** * The exploitation policy for selecting the next node */ int selectMoveExploitation(treeNode* current){ int bestMoves[_DOM->getNumOfChildren(current->rep, current->side)]; int numBestMoves=0; findBestMoves(current,bestMoves,&numBestMoves); return selectBestMove(bestMoves,numBestMoves); }