void BOARD::iterativeDeepening() { int bestscore=-INFINITE; int bestmove =NOMOVE; int numPv=0; clearForSearch(); for(int i=1;i<=info.depth;i++) { bestscore=alphabeta(i,-INFINITE,INFINITE,true); if(info.stopped) break; numPv=getPvLine(i); bestmove=PVARRAY[0]; printf("info score cp %d depth %d nodes %lld time %d ",bestscore,i,info.nodes,getTime()-info.starttime); printf("pv"); for(int j=0;j<numPv;j++) { printf(" "); printMove(PVARRAY[j]); } printf("\n"); //printf("Ordering : %0.2f\n",info.fhf/info.fh); } printf("bestmove ");printMove(bestmove); printf("\n"); }
void printBestMove() { send("bestmove "); printMove(results.pv[0]); send(" ponder "); printMove(results.pv[1]); send("\n"); }
//NOT USED! extern int TryBearoff(ConstTanBoard b, int dice0, int dice1) { movelist ml; unsigned int i, iMove, cMoves; if (ClassifyPosition(msBoard(), VARIATION_STANDARD) > CLASS_RACE) { /* It's a contact position; don't automatically bear off */ printf("> CLASS_RACE!\n"); return -2; } GenerateMoves(&ml, b, dice0, dice1, FALSE); cMoves = (dice0 == dice1) ? 4 : 2; for (i = 0; i < ml.cMoves; i++) for (iMove = 0; iMove < cMoves; iMove++) { if ((ml.amMoves[i].anMove[iMove << 1] < 0) || (ml.amMoves[i].anMove[(iMove << 1) + 1] != -1)) { break; } else if (iMove == cMoves - 1) { printf("OK\n"); printMove(ml.amMoves[i].anMove); return 0; } } printf("KO\n"); return -1; }
// Prints a formatted version of the game board into the console window. inline void printBoard(const S_BOARD *pos) { static const char *temp = " +---+---+---+---+---+---+---+---+"; printf("\n============================ GAME BOARD " "============================\n\n"); printf(" a b c d e f g h\n"); printf("%s\n", temp); for (unsigned rank = RANK_8; rank != (unsigned)(RANK_1 - 1); rank--) { printf(" %c | ", '1' + rank); for (unsigned file = FILE_A; file <= FILE_H; file++) { const unsigned piece = pos->pieces[FR2SQ(file, rank)]; ASSERT(pieceValidEmpty(piece)); printf("%c | ", pieceChar[piece]); } printf("%c", '1' + rank); switch (rank) { case RANK_8: printf(" DROPPABLE PIECES\n%s +---+---+---+---+---+\n", temp); break; case RANK_7: printf(" | P | N | B | R | Q |"); printf("\n%s +---+---+---+---+---+---+\n", temp); break; case RANK_6: printf(" | W | %d | %d | %d | %d | %d |", pos->dropNum[wP], pos->dropNum[wN], pos->dropNum[wB], pos->dropNum[wR], pos->dropNum[wQ]); printf("\n%s +---+---+---+---+---+---+\n", temp); break; case RANK_5: printf(" | B | %d | %d | %d | %d | %d |", pos->dropNum[bP], pos->dropNum[bN], pos->dropNum[bB], pos->dropNum[bR], pos->dropNum[bQ]); printf("\n%s +---+---+---+---+---+---+\n", temp); break; default: printf("\n%s\n", temp); break; } } printf(" a b c d e f g h\n\n"); ASSERT(sideValid(pos->side)); if (pos->hisPly > 0) { printf(" LAST MOVE: %s\n", printMove(pos->history[pos->hisPly - 1]->move)); } else { printf(" LAST MOVE: NONE\n"); } printf(" SIDE TO MOVE: %s\n", pos->side == WHITE ? "WHITE" : "BLACK"); printf(" EN PASSANT SQUARE: %s\n", printSquare(pos->enPas)); printf(" CASTLING PERMISSIONS: %c%c%c%c\n", pos->castlePerm & WKCA ? 'K' : '-', pos->castlePerm & WQCA ? 'Q' : '-', pos->castlePerm & BKCA ? 'k' : '-', pos->castlePerm & BQCA ? 'q' : '-'); printf(" HASHKEY: %llX\n", pos->posKey); printf(" FEN: '%s'\n", printFEN(pos)); printf("\n========================== END GAME BOARD " "==========================\n\n"); }
static int movePiece(position *pos, const move * const m, const int player) { int piece; int castleBits; piece = m->thisPiece; /* move piece to new location */ pos->board[m->to] = pos->board[m->from]; pos->board[m->from] = 0; CLR_BIT(pos->pieces[player], m->from); SET_BIT(pos->pieces[player], m->to); if(IS_KING(piece)) { CLR_BIT(pos->king[player], m->from); SET_BIT(pos->king[player], m->to); /* king position is tracked using kingSquare too */ pos->kingSquare[player] = m->to; if(IS_CASTLE(m)) { moveCastlingExtra(pos, m); } /* king has moved, cannot castle now */ castleBits = CASTLE_BITS(player); pos->castleFlags &= ~castleBits; } else if(IS_PAWN(piece)) { /* move the pawn as usual */ CLR_BIT(pos->pawns[player], m->from); SET_BIT(pos->pawns[player], m->to); /* handle ep, promotion if required */ movePawnExtra(pos, m, player); } else if(IS_KNIGHT(piece)) { CLR_BIT(pos->knights[player], m->from); SET_BIT(pos->knights[player], m->to); } else if(IS_BISHOP(piece)) { CLR_BIT(pos->bishops[player], m->from); SET_BIT(pos->bishops[player], m->to); } else if(IS_ROOK(piece)) { CLR_BIT(pos->rooks[player], m->from); SET_BIT(pos->rooks[player], m->to); /* disable castling if required */ moveRookExtra(pos, m, player); } else if(IS_QUEEN(piece)) { CLR_BIT(pos->queens[player], m->from); SET_BIT(pos->queens[player], m->to); } else { printPosition(pos); printMove(m); fprintf(stderr, "Piece = %d\n", piece); /* Something is wrong, I don't know which piece this is. * This must NEVER happen; move generation is screwed. */ assert(0); } return 0; }
/*prints all the moves in the list*/ void printMoves(linkedList *moves){ listNode *node = moves->first; while (node!=NULL) { printMove(node->data); node = node->next; } }
/*Get Computer Move and Update Board*/ int getComputerMove(Game *game) { Move *move = getMinimaxMove(game, oppositeCol(getUserColorBit(game->settings))); int res; printf("%s", COMPUTER_MOVE); printMove(move); updateBoard(game, move); freeMove(move); res = gameOver(game, getUserColorBit(game->settings)); return res; }
/* Prints a move list */ void printMoveList(const MoveList *list) { int i; char *move; for(i = 0; i < list->numMoves; i++) { move = printMove(list->moves[i]); printf("%s\n", move); } }
inline void printMoveList(const S_MOVELIST *list) { int score; unsigned move; printf("\n============================ MOVE LIST " "=============================\n"); printf(" LIST HAS %d MOVES:\n", list->count); for (unsigned i = 0; i < list->count; i++) { move = list->moves[i].move; score = list->moves[i].score; printf("MOVE %3d: %5s (SCORE: %d)\n", i + 1, printMove(move), score); } printf("\n========================== END MOVE LIST " "===========================\n"); }
void printPv() { send("info depth %d ", results.currentDepth); send("time %lu ", msec_since(shared_results()->startTime)); if (results.mate == 0) { send("score cp %d ", results.score); } else // forced mate send("score mate %d ", results.mate); send("pv "); for (int a = 0;results.pv[a] != NULL_MOVE;a++) { printMove(results.pv[a]); send(" "); } send("\n"); }
void testPlayTurn() { int b[2][25] = { //MEGLIO NON RADDOPPIARE //{0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //{0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //PC //RICHIESTA DI RADDOPPIO SU 0 A 0 //{1, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0}, //{0, 2, 2, 3, 0, 3, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //PC //RACE GAME.. //{0, 0, 0, 0, 2, 5, 2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0}, //{0, 0, 0, 2, 0, 4, 2, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0} //PC {0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},//BLACK (HUMAN) BGV {0, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0}//WHITE (PC) }; setBoard((ConstTanBoard)b); ms.nCube = 1; ms.fCubeOwner = 1; ms.fMove = 1; ms.fTurn = 1; ms.anScore[0] = 0; ms.anScore[1] = 0; ms.nMatchTo = 1; ms.fCrawford = FALSE; printf("\n\nTEST TURNO IA...\n"); printBoard((ConstTanBoard)b); //printf("RESIGN: %d\n", askForResignation()); //printf("DOUBLING: %s\n", askForDoubling()?"YES":"NO"); int dices[2] = {6, 4}; int move[8]; //rollDice(dices); printDices(dices); evaluateBestMove(dices, move); printMove(move); printf("AIlevel: %d\n\n", currentAILevel); }
static char* isLegal_test() { assert(gs.initialized); printf("Running isLegal_test\n"); char fen[] = "rn1qkbnr/ppp2pp1/3p4/4p2p/4P2P/3P3b/PPP2PP1/RNBQKBNR w KQkq - 0 1"; parseFen(fen); int numMoves; MOVE moves[MAX_MOVES]; generateNonCaptures(gs.color, moves, &numMoves, gs.bitboard); for (int i=0; i < numMoves; i++) { MOVE move = moves[i]; //printf("Testing move "); //printMove(move); if (!isMoveLegal(move)) { printf("Not legal "); printMove(move); isMoveLegal(move); } assert( isMoveLegal(move)); } return 0; }
void testGenerateMoves() { int _b[2][25] = { {1, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0}, {0, 2, 2, 3, 0, 3, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} //PC }; ConstTanBoard b=ms.anBoard; printf("\n\nTEST GENERATE MOVES...\n"); printBoard((ConstTanBoard)b); int dices[2] = {4, 6}; printDices(dices); int** moves; int nMoves; moves = generateMoves((ConstTanBoard)b, dices[0], dices[1], &nMoves); int i=0; for (i=0;i<nMoves;i++) { printMove(moves[i]); } printf("MOVES: %d\n", nMoves); printf("AIlevel: %d\n\n", currentAILevel); }
int Board::alphaBeta(int depthLeft, int alpha, int beta, int doNullMove) { int check = 0; int oldAlpha = alpha; int numSearched = 0; int bestMove = 0; int hashMove = 0; int stage; int i = currentState->firstMove; int result; nodes++; nodesUntilUpdate--; if(nodesUntilUpdate <= 0) { switch(game->interfaceUpdate()) { case OUT_OF_TIME: abortingSearch = 1; return 0; break; case STOP_SEARCH: abortingSearch = 1; return 0; break; } nodesUntilUpdate = nodesPerUpdate; } if(isRepetition()) { returnMove = 0; return DRAW; } HashEntry *e; e = hash->probe(currentState->hashKey); if(e!=NULL) { hashHits++; //Is it useful for replacing this search - otherwise just find a best move hashMove = e->bestMove; returnMove = hashMove; #ifndef DISABLE_HASH if(depth > 0 && e->depth >= depthLeft) { switch(e->flags & 3) { case HASH_EXACT: return e->score; break; case HASH_LOWER: if(e->score >= beta) return e->score; if(e->score > alpha) alpha = e->score; break; case HASH_UPPER: if(e->score <= alpha) return e->score; if(e->score < beta) beta = e->score; break; } if(e->flags & HASH_NO_NULL) { doNullMove = 0; } } #endif } #ifdef DEBUG_HASH if(hashMove > 0 && !testLegality(hashMove)) { print(); cout << "Illegal hash move! "; printMove(hashMove); cout << endl; } #endif if(depthLeft == 0) { int value = quiescence(alpha, beta); //hash->store(0, value, 0, HASH_EXACT); return value; } check = inCheck(); /* if(doNullMove && !check && !nullRisk() && depth >= 2) { int nullDepth = depthLeft - 2; makeNullMove(); if(nullDepth > 0) { result = alphaBeta(nullDepth, -beta, -beta+1, NO_NULL_MOVE); } else { result = quiescence(-beta, -beta+1); } retractNullMove(); if(nullDepth > 0 && result >= beta) { result = alphaBeta(nullDepth, beta-1, beta, DO_NULL_MOVE); numMoves = currentState->firstMove; } if(result >= beta) { returnMove = 0; return beta; } } */ if(!hashMove && depthLeft >= 3) { result = alphaBeta(depthLeft - 2, alpha, beta, DO_NULL_MOVE); numMoves = currentState->firstMove; //Failed low so have to research with new bounds if(result <= alpha) result = alphaBeta(depthLeft - 2, -MATE, alpha + 1, DO_NULL_MOVE); numMoves = currentState->firstMove; hashMove = returnMove; } if(hashMove > 0) { stage = HASH_MOVE; } else { stage = ALL_MOVES; } while(stage != FINISHED) { switch(stage) { case HASH_MOVE: moveStack[numMoves++] = hashMove; stage = ALL_MOVES; break; case ALL_MOVES: if(check) { generateCheckEvasions(); sortNonCaptures(i, hashMove); stage = FINISHED; } else { generateCaptures(); sortCaptures(i, hashMove); stage = NON_CAPTURES; } break; case NON_CAPTURES: generateNonCaptures(); sortNonCaptures(i, hashMove); stage = FINISHED; break; } for(; i<numMoves; i++) { int move = moveStack[i]; makeMove(move); #ifdef DEBUG_BITBOARDS if(isMessedUp()) { printMoves(); print(); cout << "Hash move = "; printMove(hashMove); cout << endl; cout << "MakeMove" << endl; exit(0); } #endif if(isLegal()) { numSearched++; result = -alphaBeta(depthLeft - 1, -beta, -alpha, DO_NULL_MOVE); retractMove(move); if(abortingSearch) { returnMove = bestMove; return result; } if(result > alpha) { if(result >= beta) { returnMove = move; hash->store(move, result, depthLeft, HASH_LOWER); #ifdef EXTRA_STATS cutoffs++; if(i == currentState->firstMove) firstCutoffs++; #endif return result; } /* if(depth == 0) { printMove(move); cout << " "; printScore(result); cout << " hashMove: "; printMove(hashMove); cout << endl; }*/ alpha = result; bestMove = move; } } else { //Illegal move retractMove(move); } #ifdef DEBUG_BITBOARDS if(isMessedUp()) { printMoves(); print(); cout << "Hash move = "; printMove(hashMove); cout << endl; cout << "RetractMove" << endl; exit(0); } #endif } } //While we have moves //No legal moves.. if(numSearched ==0) { returnMove = 0; if(check) { return -(MATE - depth); } else { return DRAW; } } if(alpha == oldAlpha) { hash->store(bestMove, alpha, depthLeft, HASH_UPPER); } else { hash->store(bestMove, alpha, depthLeft, HASH_EXACT); } returnMove = bestMove; return alpha; }
void printCurrMove() { send("info depth %d seldepth %d ", results.currentDepth, results.selDepth); send("currmove "); printMove(results.currentMove); send(" currmovenumber %d\n", results.currentMoveNumber); }
void runIt(){ //declare the number of disks for tower of Hanoi int numDisks; printf("Enter the number of Disks: "); scanf("%d",&numDisks); int source[numDisks], spare[numDisks], dest[numDisks]; //initialize pegs for (int i = 0; i < numDisks; i++) { source[i] = i+1; //Smallest element is 1 and largest is N dest[i] = spare[i] = 0; //Empty } //This formulaSteps counts the minimum number of steps //needed for tower of hanoi problem int steps = formulaSteps(numDisks); //This variable is used to check //the move needed from one peg to other int mvPegs = -1; //used for printMove function bool isEven; if (numDisks%2 == 0) isEven = true; else isEven = false; //These variable will keep track of the head of the peg int head1 = 0, head2 = numDisks-1, head3 = numDisks - 1; for (int step = 1; step <= steps; step++) { mvPegs = step%3; // Result = 1, 2, 0; 1, 2, 0 ... //I am using simple array; Just a small check if (inBound(head1, numDisks) && inBound(head2, numDisks) && inBound(head3, numDisks)){ if (mvPegs == 1) //Peg 1 -> Peg 3 or Peg 3 -> Peg 1 { moveDisk(numDisks, & head1, & head3, source, dest); printMove(source, spare, dest, numDisks, isEven); } else if (mvPegs == 2) // Peg 1 -> Peg 2 or Peg 2 -> Peg 1 { moveDisk(numDisks, & head1, & head2, source, spare); printMove(source, spare, dest, numDisks, isEven); } else if (mvPegs == 0) // Peg 2 -> Peg 3 or Peg 3 -> Peg 2 { moveDisk(numDisks, & head2, & head3, spare, dest); printMove(source, spare, dest, numDisks, isEven); }else { cout << "CODE 1111" << endl; } } else { break; } } }