static int GetUserMove(int nCoins) { int nTaken,limit; while(TRUE){ printf("How many would you like? "); nTaken=GetInteger(); if(MoveIsLegal(nTaken,nCoins)) break; limit=(nCoins<MaxTake)?nCoins:MaxTake; printf("That's cheating! Please choose a number"); printf(" between 1 and %d.\n",limit); printf("There are %d coins in the pile.\n",nCoins); } return nTaken; }
int getMoveForPosition(ChessBoard * board) { std::pair<std::multimap<HASHKEY, Coord>::iterator, std::multimap<HASHKEY, Coord>::iterator> positionStartEnd; cerr << board_to_string(board) << endl; positionStartEnd = openingBook.equal_range(board->zobristHashKey); std::vector<Coord> coords; int totalWeight = 0; for(std::multimap<HASHKEY, Coord>::iterator itPosition = positionStartEnd.first; itPosition != positionStartEnd.second; ++itPosition) { coords.push_back(itPosition->second); totalWeight += itPosition->second.weight; } // select a move given the weights if (coords.empty()) return -1; Coord theCoord; if (coords.size() == 1) theCoord = coords.front(); else { int whichOne = rand() % totalWeight; int weightSoFar = 0; for(std::vector<Coord>::iterator itCoords = coords.begin(); itCoords != coords.end(); ++itCoords) { if (itCoords->weight + weightSoFar > whichOne) { theCoord = *itCoords; break; } weightSoFar += itCoords->weight; } // convert it to a move } if (!MoveIsLegal(board, theCoord.move)) { cerr << "move from opening book: " << MoveToString(theCoord.move) << " is not legal!" << endl; return -1; } LOG4CPLUS_DEBUG(logger, "book move " << MoveToString(theCoord.move)); return theCoord.move; }