void CDynamicProgrammingStrategy::calculatePaths() {
    while (!stateQueue.empty()) {
        PlayerState currentState = stateQueue.front();

        for (int xDeviation = -1; xDeviation <= 1; ++xDeviation) {
            for (int yDeviation = -1; yDeviation <= 1; ++yDeviation) {
                PlayerState newState(
                        currentState.GetX() + currentState.GetXVelocity() + xDeviation,
                        currentState.GetY() + currentState.GetYVelocity() + yDeviation,
                        currentState.GetXVelocity() + xDeviation,
                        currentState.GetYVelocity() + yDeviation
                );

                if (newState.GetX() < 0 || newState.GetX() >= map.sizeOnXaxis()
                        || newState.GetY() < 0 || newState.GetY() >= map.sizeOnYaxis()
                        || !map.canPlayerStayOnCell(newState.GetX(), newState.GetY())
                        || map.hasBarrierOnPath(currentState.GetX(), currentState.GetY(),
                                newState.GetX(), newState.GetY())) {
                    continue;
                }

                if (minPath.GetStepCount(newState) == -1 ||
                        minPath.GetStepCount(newState) > minPath.GetStepCount(currentState) + 1) {
                    minPath.SetStepCount(newState, minPath.GetStepCount(currentState) + 1);
                    minPath.SetPreviousState(newState, currentState);
                    stateQueue.push(newState);
                }
            }
        }

        stateQueue.pop();
    }
}
Exemplo n.º 2
0
EMovementDirection Game::nextMoveForPlayer(int playerID) {
    PlayerState currentPlayer = (*players)[playerID];
    if (currentPlayer.GetX() == finishPoint.first && currentPlayer.GetY() == finishPoint.second) {
        //std::cout << "FINISH" << std::endl;
        return EMovementDirection::FINISH_POSITION;
    } else {
        SNode currentStart(currentPlayer.GetX(), currentPlayer.GetY(), currentPlayer.GetXVelocity(), currentPlayer.GetYVelocity());
        SNode finish(finishPoint.first, finishPoint.second, 0, 0);
        SNode resultNode = aStarStrategyOnYAGSBPL->searchPath(currentStart, finish);
        (*players)[playerID].changePosition(std::make_pair(resultNode.position.first, resultNode.position.second));
        (*players)[playerID].changeVelocityVector(std::make_pair(resultNode.velocityVector.first, resultNode.velocityVector.second));
        return resultNode.direction;
    }
}
bool CDynamicProgrammingStrategy::canIntersectFinishLine(PlayerState state, int &xDeviation_, int &yDeviation_) const {
	for (int xDeviation = -1; xDeviation <= 1; ++xDeviation) {
		for (int yDeviation = -1; yDeviation <= 1; ++yDeviation) {
			int newX = state.GetX() + state.GetXVelocity() + xDeviation;
			int newY = state.GetY() + state.GetYVelocity() + yDeviation;

			if (newX < 0 || newX >= map.sizeOnXaxis() ||
				newY < 0 || newY >= map.sizeOnYaxis()) {
				continue;
			}

			if (map.intersectFinishLine(state.GetX(), state.GetY(), newX, newY) &&
				!map.hasBarrierOnPath(state.GetX(), state.GetY(), newX, newY))
			{
				xDeviation_ = xDeviation;
				yDeviation_ = yDeviation;
				return true;
			}
		}
	}
	
	return false;
}
std::pair<int, int> CDynamicProgrammingStrategy::GetNextPosition() {
    PlayerState nextState = optimalPath.top();
    optimalPath.pop();
    return std::make_pair(nextState.GetX(), nextState.GetY());
}