Пример #1
0
int GameState::getScore(Player* maximizingPlayer)
{
    int score = 0;

    int position;
    for (position=0; position<50; position++)
    {
        int row = board->getRow(position);
        int col = board->getCol(position);

        int pieceScore = 0;
        pieceType piece = board->getPiece(position);

        // Piece Count
        if (vectorContains(maximizingPlayer->getPieces(), piece)) {
            pieceScore = 10;
        }
        if (vectorContains(maximizingPlayer->getOpponentPieces(), piece)) {
            pieceScore = -10;
        }

        // King Count
        if ((piece == pieceType::white_crown) || (piece == pieceType::black_crown)) {
            pieceScore *= 3;
        }

        // Weigthing using Grid
        pieceScore *= std::max(abs(row-5), abs(col-5));

        score += pieceScore;
    }

    return score;
}
Пример #2
0
// Calculates the path to the goal from the AI using the A* algorithm.
// Returns a sf::vector2f vector where all elements represent the
// position of a Cell.
Pathfinder::PositionVector Pathfinder::getPath(sf::Vector2f startPos, sf::Vector2f endPos){
	sf::Clock clock;
	Cell* endCell = GridManager::getInstance()->getClosestCell(endPos);
	GridManager::CellVector openList;
	GridManager::CellVector closedList;
	openList.push_back(GridManager::getInstance()->getClosestCell(startPos));
	sf::Vector2f gridPos(*openList.back()->getGridPosition());

	while ((closedList.empty() || closedList.back() != endCell) && !openList.empty()){
		Cell* currentCell = getCellWithLowestFValue(&openList);
		closedList.push_back(currentCell);

		int cellIndex = closedList.back()->getIndex();
		int index = closedList.back()->getIndex();
		index -= GridManager::getInstance()->getGridSize2f().x + 1;
		for (int i = 0; i < 3; i++){
			for (int j = 0; j < 3; j++){
				if (indexIsInsideGrid(index)){
					Cell* cell = GridManager::getInstance()->getCell(index);
					if (cell->getIsWalkable() && !vectorContains(&closedList, cell)){
						if (!vectorContains(&openList, cell)){
							cell->setParentCell(closedList.back());
							cell->setGCost(calculateGValue(cell, i, j));
							openList.push_back(cell);
							cell->setSpriteTexture(TextureManager::getInstance()->getTexture("Grid_B"));
						}
						else{
							int gCost = calculateGValue(cell, i, j);
							if (gCost < cell->getGCost()){
								cell->setParentCell(closedList.back());
								cell->setGCost(gCost);
							}
						}
					}
					index++;
				}
			}
			index += GridManager::getInstance()->getGridSize2f().x - 3;
		}
	}

	PositionVector* path = getPath(endCell);

	int ms = clock.getElapsedTime().asMilliseconds();
	std::cout << "Pathfinding took " << ms << "ms" << std::endl;

	GridManager::getInstance()->clearValues();
	return *path;
}
Пример #3
0
std::vector<Move> GameState::getMovesAtPosition(int row, int col, Player* player)
{
    std::vector<Move> moves;

    pieceType piece = board->getPiece(row,col);

    bool isCrowned = (piece == pieceType::white_crown) || (piece == pieceType::black_crown);
    bool isWhite   = vectorContains(player->getPieces(), pieceType::white);
    bool isBlack   = vectorContains(player->getPieces(), pieceType::black);

    getMovesInDirection(row, col, isWhite, true, !isCrowned, player, moves);
    getMovesInDirection(row, col, isWhite, false, !isCrowned, player, moves);

    return moves;
}
Пример #4
0
static bool vectorsIntersect(const std::vector<std::string>& vectorA, const std::vector<std::string>& vectorB) {
    for (const auto& a : vectorA) {
        if (vectorContains(vectorB, a)) {
            return true;
        }
    }
    return false;
}
Пример #5
0
std::vector<T> getTargetsNotAtPositions(const std::vector<T>& vec,
		const std::vector<uint32_t>& positions) {
	std::vector<T> ans;
	uint32_t currentPos = 0;
	for (const auto& element : vec) {
		if (!vectorContains(positions, currentPos)) {
			ans.push_back(element);
		}
		++currentPos;
	}
	return ans;
}
void TBranchCollection::setBranchVariable(std::string branchLabel, float value) {
    if ( contains(branchLabel) ) {
        *varMap_[branchLabel] = value;
    }
    else if ( vectorContains(branchLabel) ) {
        varVectorMap_[branchLabel]->push_back(value);
    }
    else {
        cout << "WARNING : Trying to fill branch that doesn't exist in this folder" << endl;
        cout << "Branch label : " << branchLabel << endl;
    }
}
Пример #7
0
std::vector<std::vector<T>> collapseUniqueVectors(
		const std::vector<std::vector<T>>& vec) {
	std::vector<std::vector<T>> ans;
	int count = 0;
	for (const auto& iter : vec) {
		if (count == 0) {
			ans.push_back(iter);
		} else {
			if (!vectorContains(ans, iter)) {
				ans.push_back(iter);
			}
		}
		++count;
	}
	return ans;
}
Пример #8
0
std::vector<Move> GameState::mergeMoves(Move first, std::vector<Move> second)
{
    std::vector<Move> moves;

    std::vector<int> firstRemovedPositions;
    for (Piece piece : first.getRemovedPieces()) {
        firstRemovedPositions.push_back(piece.position);
    }

    if (!second.empty()) {
        std::vector<Move>::iterator moveIt;
        for (moveIt = second.begin(); moveIt < second.end(); moveIt++)
        {
            //Dont merge moves if they cancel eachother
            if (false == vectorContains(firstRemovedPositions, moveIt->getNewPiece().position))
            {
                Move move;
                move.setNewPiece(moveIt->getNewPiece());
                move.setRemovedPieces(first.getRemovedPieces());

                std::vector<Piece> removed = moveIt->getRemovedPieces();

                std::vector<Piece>::iterator removedIt;
                for (removedIt = removed.begin(); removedIt < removed.end(); removedIt++)
                {
                    if (removedIt->position != first.getNewPiece().position)
                    {
                        move.addRemovedPiece(*removedIt);
                    }
                }

                moves.push_back(move);
            }
        }
    }
    else {
        moves.push_back(first);
    }

    return moves;
}
Пример #9
0
void FeatureIndex::addFeature(
    std::unordered_map<std::string, std::vector<Feature>>& result,
    const IndexedSubfeature& indexedFeature,
    const GeometryCollection& queryGeometry,
    const optional<std::vector<std::string>>& filterLayerIDs,
    const GeometryTile& geometryTile,
    const CanonicalTileID& tileID,
    const style::Style& style,
    const float bearing,
    const float pixelsToTileUnits) const {

    auto& layerIDs = bucketLayerIDs.at(indexedFeature.bucketName);
    if (filterLayerIDs && !vectorsIntersect(layerIDs, *filterLayerIDs)) {
        return;
    }

    auto sourceLayer = geometryTile.getLayer(indexedFeature.sourceLayerName);
    assert(sourceLayer);

    auto geometryTileFeature = sourceLayer->getFeature(indexedFeature.index);
    assert(geometryTileFeature);

    for (const auto& layerID : layerIDs) {
        if (filterLayerIDs && !vectorContains(*filterLayerIDs, layerID)) {
            continue;
        }

        auto styleLayer = style.getLayer(layerID);
        if (!styleLayer ||
            (!styleLayer->is<style::SymbolLayer>() &&
             !styleLayer->baseImpl->queryIntersectsGeometry(queryGeometry, geometryTileFeature->getGeometries(), bearing, pixelsToTileUnits))) {
            continue;
        }

        result[layerID].push_back(convertFeature(*geometryTileFeature, tileID));
    }
}
Пример #10
0
std::vector<Move> GameState::getForcedMoves(Player* player)
{
    std::vector<Move> moves;
    std::vector<Move> movesAtPosition;

    std::vector<pieceType> playerPieces = player->getPieces();

    std::vector<pieceType>::iterator tile;
    for (tile = board->getBegin(); tile < board->getEnd(); tile++)
    {
        int key = std::distance(board->getBegin(), tile);

        int row = board->getRow(key);
        int col = board->getCol(key);

        if (vectorContains(playerPieces, *tile))
        {
            movesAtPosition = getForcedMovesAtPosition(row, col, player);
            moves.insert(moves.end(), movesAtPosition.begin(), movesAtPosition.end());
        }
    }

    return moves;
}
Пример #11
0
void lmHandlerReceiveLargeMessage(NodeManagerInterface nmi, JausMessage message)
{
	LargeMessageList msgList;
	JausMessage tempMessage;
	JausMessage outMessage;
	int i;
	unsigned long sequenceNumber;
	unsigned long index;
	JausUnsignedInteger newDataSize = 0;
	JausUnsignedInteger bufferIndex = 0;
	char address[128] = {0};
	
	switch(message->dataFlag)
	{
		case JAUS_FIRST_DATA_PACKET:
			// Check for valid SeqNumber(0), else Error
			if(message->sequenceNumber) 
			{
				cError("LargeMessageHandler: Received First Data Packet with invalid Sequence Number(%d)\n", message->sequenceNumber);
				jausMessageDestroy(message);
				return;
			}
			
			// Check if LargeMessageList exists (same CC & Source)
			msgList = lmHandlerGetMessageList(nmi->lmh, message);
			if(msgList)
			{
				// Destroy the list and all messages
				vectorRemove(nmi->lmh->messageLists, msgList, (void *)lmHandlerMessageListEqual);
				lmListDestroy(msgList);		
			}

			// create LargeMessageList
			msgList = lmListCreate();
			vectorAdd(nmi->lmh->messageLists, msgList);
				
			// add message to LargeMessageList at first position
			msgList->commandCode = message->commandCode;
			msgList->source->id = message->source->id;
			vectorAdd(msgList->messages, message);
			break;
		
		case JAUS_NORMAL_DATA_PACKET:
			// Check if LargeMessageList exists, error if not
			msgList = lmHandlerGetMessageList(nmi->lmh, message);
			if(msgList)
			{
				// Check if item exists in LargeMessageList with seqNumber
				if(vectorContains(msgList->messages, message, (void *)lmHandlerLargeMessageCheck) != -1)
				{
					cError("LargeMessageHandler: Received duplicate NORMAL_DATA_PACKET\n");
					jausMessageDestroy(message);
				}
				else
				{
					// insert to Vector
					vectorAdd(msgList->messages, message);
				}
			}
			else
			{
				// Destroy Message
				cError("LargeMessageHandler: Received NORMAL_DATA_PACKET (0x%4X) for unknown Large Message Set (never received JAUS_FIRST_DATA_PACKET)\n", message->commandCode);
				jausMessageDestroy(message);
			}
			break;
			
		case JAUS_RETRANSMITTED_DATA_PACKET:
			// Check if LargeMessageList exists, error if not
			msgList = lmHandlerGetMessageList(nmi->lmh, message);
			if(msgList)
			{
				// Check if item exists in LargeMessageList with seqNumber
				if(vectorContains(msgList->messages, message, (void *)lmHandlerLargeMessageCheck) != -1)
				{
					tempMessage = (JausMessage) vectorRemove(msgList->messages, message, (void *)lmHandlerLargeMessageCheck);
					jausMessageDestroy(tempMessage);
				}
				// insert to Vector
				vectorAdd(msgList->messages, message);
			}
			else
			{
				cError("LargeMessageHandler: Received RETRANSMITTED_DATA_PACKET for unknown Large Message Set (never received JAUS_FIRST_DATA_PACKET)\n");
				jausMessageDestroy(message);				
			}
			break;
		
		case JAUS_LAST_DATA_PACKET:
			// Check if LargeMessageList exists, error if not
			msgList = lmHandlerGetMessageList(nmi->lmh, message);
			if(msgList)
			{
				// insert message to end of list
				vectorAdd(msgList->messages, message);

				// Create JausMessage object
				outMessage = jausMessageCreate();

				// Calculate new message size
				newDataSize = 0;
				for(i = 0; i < msgList->messages->elementCount; i++)
				{
					tempMessage = (JausMessage)msgList->messages->elementData[i];
					newDataSize += tempMessage->dataSize;
				}

				// Setup Header and Data Buffer
				outMessage->properties = tempMessage->properties;
				outMessage->commandCode = tempMessage->commandCode;
				outMessage->destination->id = tempMessage->destination->id;
				outMessage->source->id = tempMessage->source->id;
				outMessage->dataControl = tempMessage->dataControl;
				outMessage->sequenceNumber = tempMessage->sequenceNumber;
				outMessage->data = (unsigned char *) malloc(newDataSize);
				
				// Populate new message
				sequenceNumber = 0;
				bufferIndex = 0;
				while(sequenceNumber <= message->sequenceNumber)
				{
					index = 0;
					do
					{
						tempMessage = (JausMessage)msgList->messages->elementData[index];
						index++;
						if(index > msgList->messages->elementCount)
						{
							// Invalid set of messages
							//TODO: Here is when you would request a retransmittal if ACK/NAK is set and ask the sending component to resend some packets
	
							// Received LAST_DATA_PACKET, but do not have proper sequence of messages
							// Destroy the list and all messages
							vectorRemove(nmi->lmh->messageLists, msgList, (void *)lmHandlerMessageListEqual);
							lmListDestroy(msgList);
						
							cError("LargeMessageHandler: Received LAST_DATA_PACKET, but do not have proper sequence of messages\n");						
							jausMessageDestroy(outMessage);
							return;
						}
					}
					while(tempMessage->sequenceNumber != sequenceNumber);
					
					// Move data from tempMessage to outMessage
					memcpy(outMessage->data + bufferIndex, tempMessage->data, tempMessage->dataSize);
					bufferIndex += tempMessage->dataSize;
					sequenceNumber++; // TOM: switched from i++
				}

				// Set DataSize
				// Set proper header flags (dataFlag JAUS_SINGLE_DATA_PACKET)
				outMessage->dataSize = newDataSize;
				outMessage->dataFlag = JAUS_SINGLE_DATA_PACKET;

				if(outMessage->scFlag)
				{
					scManagerReceiveMessage(nmi, outMessage);
				}
				else
				{
					queuePush(nmi->receiveQueue, (void *)outMessage);
				}
				
				// Destroy LargeMessageList
				vectorRemove(nmi->lmh->messageLists, msgList, (void *)lmHandlerMessageListEqual);
				lmListDestroy(msgList);
			}
			else
			{
				cError("LargeMessageHandler: Received LAST_DATA_PACKET for unknown Large Message Set (never received JAUS_FIRST_DATA_PACKET)\n");
				jausMessageDestroy(message);				
			}
			break;
		
		default:
			jausAddressToString(message->source, address);
			cError("lmHandler: Received (%s) with improper dataFlag (%d) from %s\n", jausMessageCommandCodeString(message), message->dataFlag, address);
			jausMessageDestroy(message);
			break;
	}
}
Пример #12
0
std::vector<Move> GameState::getForcedMovesAtPosition(int row, int col, Player* player)
{
    std::vector<Move> moves;
    Move move;

    std::vector<pieceType> opponentPieces = player->getOpponent()->getPieces();

    pieceType piece = board->getPiece(row,col);

    bool isCrowned = (piece == pieceType::white_crown) || (piece == pieceType::black_crown);

    int i, j;
    //check left down
    for (i=0; (i==0) || (isCrowned && (board->getPiece(row-i,col-i) == pieceType::empty)); i++)
    {
        if (vectorContains(opponentPieces, board->getPiece(row-i-1,col-i-1)))
        {
            for (j=2; (board->getPiece(row-i-j, col-i-j) == pieceType::empty) && ((j==2) || isCrowned); j++)
            {
                moves = appendMoves(moves, getSuccessiveForcedMoves(board->createMove(row-i-2,col-i-2, row, col), player));
            }
        }
    }

    //check right down
    for (i=0; (i==0) || (isCrowned && (board->getPiece(row-i,col+i) == pieceType::empty)); i++)
    {
        if (vectorContains(opponentPieces, board->getPiece(row-i-1,col+i+1)))
        {
            for (j=2; (board->getPiece(row-i-j, col+i+j) == pieceType::empty) && ((j==2) || isCrowned); j++)
            {
                moves = appendMoves(moves, getSuccessiveForcedMoves(board->createMove(row-i-2,col+i+2, row, col), player));
            }
        }
    }

    //check left up
    for (i=0; (i==0) || (isCrowned && (board->getPiece(row+i,col-i) == pieceType::empty)); i++)
    {
        if (vectorContains(opponentPieces, board->getPiece(row+i+1,col-i-1)))
        {
            for (j=2; (board->getPiece(row+i+j, col-i-j) == pieceType::empty) && ((j==2) || isCrowned); j++)
            {
                moves = appendMoves(moves, getSuccessiveForcedMoves(board->createMove(row+i+2,col-i-2, row, col), player));
            }
        }
    }

    //check right
    for (i=0; (i==0) || (isCrowned && (board->getPiece(row+i,col+i) == pieceType::empty)); i++)
    {
        if (vectorContains(opponentPieces, board->getPiece(row+i+1,col+i+1)))
        {
            for (j=2; (board->getPiece(row+i+j, col+i+j) == pieceType::empty) && ((j==2) || isCrowned); j++)
            {
                moves = appendMoves(moves, getSuccessiveForcedMoves(board->createMove(row+i+2,col+i+2, row, col), player));
            }
        }
    }

    return moves;
}
Пример #13
0
bool SpatialList::contains(ObjectSpatial* object) const {
	return vectorContains(objectsVector, object);
}