Beispiel #1
0
int answerWithMe(board *b)
{
  searchData d;
  initSearchData(b, &d);
  d.fr = manhattanDistance(b);
  int nowSearching = endCheck(b);
  while(d.fr > 15){
    mvRoot(&d, nowSearching);
    nowSearching++;
    d.fr = manhattanDistance(&d.b);
  }
  return 0;
}
Beispiel #2
0
bool PacMan::findPacman(int *ghostX, int *ghostY, int *sniff) {
	//Ghosts pursue pacman for 5 timesteps or less.
	if (*sniff > 0) {
		return true;
	} else if (manhattanDistance(pacmanX, pacmanY, *ghostX, *ghostY) <= 5) {
		*sniff = 5;
		return true;
	} else {
		return false;
	}
}
/** Function to find the heuristic value */
int heuristicUnweightedValue(int state[SIZE])
{
    /** Position and heuristic value */
    int position;
    int heuristicValue = 0;

    /** Loop through the array */
    for(position = 0; position < SIZE; position++)
    {
        heuristicValue += manhattanDistance(state[position], position);
    }

    return heuristicValue;
}
int TMXPathFinding::getDistance(TileNode *start, TileNode *end) {
	//return euclideanDistance(start, end);
	return manhattanDistance(start, end);
}
Beispiel #5
0
//Update the observation we return to the agent.
//Make binary array that represents a 16-bit value.
void PacMan::updateObservation() {
	//bool binary[16];
	binaryObservation[0] = false;
	binaryObservation[1] = false;
	binaryObservation[2] = false;
	binaryObservation[3] = false;
	binaryObservation[4] = false;
	binaryObservation[5] = false;
	binaryObservation[6] = false;
	binaryObservation[7] = false;
	binaryObservation[8] = false;
	binaryObservation[9] = false;
	binaryObservation[10] = false;
	binaryObservation[11] = false;
	binaryObservation[12] = false;
	binaryObservation[13] = false;
	binaryObservation[14] = false;
	binaryObservation[15] = false;

	//Get the surrounding spaces around pacman
	if (pacmanY - 1 < 0 || map[pacmanY - 1][pacmanX] == '*') {
		binaryObservation[0] = true;
	}

	if (pacmanX + 1 > 16 || map[pacmanY][pacmanX + 1] == '*') {
		binaryObservation[1] = true;
	}

	if (pacmanY + 1 > 18 || map[pacmanY + 1][pacmanX] == '*') {
		binaryObservation[2] = true;
	}

	if (pacmanX - 1 < 0 || map[pacmanY][pacmanX] == '*') {
		binaryObservation[3] = true;
	}

	//See if pacman can find a ghost or a pellet north
	int upY = pacmanY - 1;
	while (upY > 0 && map[upY][pacmanX] != '*') {
		if (map[upY][pacmanX] == 'A' || map[upY][pacmanX] == 'B'
				|| map[upY][pacmanX] == 'C' || map[upY][pacmanX] == 'D') {

			binaryObservation[4] = true;
		}

		if (map[upY][pacmanX] == '.') {
			binaryObservation[11] = true;
		}

		upY--;
	}

	//See if pacman can find a ghost or a pellet east.
	int rightX = pacmanX + 1;
	while (rightX < 17 && map[pacmanY][rightX] != '*') {
		if (map[pacmanY][rightX] == 'A' || map[pacmanY][rightX] == 'B'
				|| map[pacmanY][rightX] == 'C' || map[pacmanY][rightX] == 'D') {

			binaryObservation[5] = true;
		}

		if (map[pacmanY][rightX] == '.') {
			binaryObservation[12] = true;
		}

		rightX++;
	}

	//See if pacman can find a ghost or a pellet south.
	int downY = pacmanY + 1;
	while (downY < 19 && map[downY][pacmanX] != '*') {

		if (map[downY][pacmanX] == 'A' || map[downY][pacmanX] == 'B'
				|| map[downY][pacmanX] == 'C' || map[downY][pacmanX] == 'D') {

			binaryObservation[6] = true;
		}
		if (map[downY][pacmanX] == '.') {
			binaryObservation[13] = true;
		}
		downY++;
	}

	//See if pacman can find a ghost or a pellet east.
	int leftX = pacmanX - 1;
	while (leftX > 0 && map[pacmanY][leftX] != '*') {
		if (map[pacmanY][leftX] == 'A' || map[pacmanY][leftX] == 'B'
				|| map[pacmanY][leftX] == 'C' || map[pacmanY][leftX] == 'D') {

			binaryObservation[7] = true;
		}

		if (map[pacmanY][leftX] == '.') {
			binaryObservation[14] = true;
		}

		leftX--;
	}

	//Check if there's a pellet nearby
	for (int y = 0; y < 19; y++) {
		for (int x = 0; x < 17; x++) {
			int distance = manhattanDistance(pacmanX, pacmanY, x, y);

			if (!binaryObservation[8] && distance <= 2 && map[y][x] == '.') {
				binaryObservation[8] = true;
			}

			if (!binaryObservation[9] && distance <= 3 && map[y][x] == '.') {
				binaryObservation[9] = true;
			}

			if (!binaryObservation[10] && distance <= 4 && map[y][x] == '.') {
				binaryObservation[10] = true;
			}
		}
	}

	//See if pacman is under the effects of a power pellet
	binaryObservation[15] = poweredUp;

	m_observation = binaryToDecimal(binaryObservation);
}
Beispiel #6
0
void PacMan::ghostPursuitMove(int *ghostX, int *ghostY, int *covers, char ghost) {
	int newX, newY;

	int currentDistance = manhattanDistance(pacmanX, pacmanY, *ghostX, *ghostY);

	bool pursuing = false;

	//Go up
	int upX = *ghostX;
	int upY = (*ghostY) - 1;
	if (isValidGhostMove(upX, upY) && manhattanDistance(pacmanX, pacmanY, upX,
			upY) < currentDistance) {
		pursuing = true;
		newX = upX;
		newY = upY;
	}

	//Go right
	int rightX = (*ghostX) + 1;
	int rightY = *ghostY;
	if (isValidGhostMove(rightX, rightY) && manhattanDistance(pacmanX, pacmanY,
			rightX, rightY) < currentDistance) {
		pursuing = true;
		newX = rightX;
		newY = rightY;
	}

	//Go down
	int downX = *ghostX;
	int downY = (*ghostY) + 1;
	if (isValidGhostMove(downX, downY) && manhattanDistance(pacmanX, pacmanY,
			downX, downY) < currentDistance) {
		pursuing = true;
		newX = downX;
		newY = downY;
	}
	// Go Left
	int leftX = (*ghostX) - 1;
	int leftY = *ghostY;
	if (isValidGhostMove(leftX, leftY) && manhattanDistance(pacmanX, pacmanY,
			leftX, leftY) < currentDistance) {
		pursuing = true;
		newX = leftX;
		newY = leftY;
	}

	//Pursuit move not valid (wall?) Get a random move instead.
	if (!pursuing) {
		ghostRandomMove(ghostX, ghostY, covers, ghost);
		return;
	}

	//Ghost got pacman. But it shouldn't move
	if (newX == pacmanX && newY == pacmanY) {
		if (poweredUp) {
			//m_reward += 50;
			resetGhost(ghost);
		} else {
			m_reward -= 50;
			reset = true;
		}
		return;
	}

	//If ghost covered something, put it back in.
	if (*covers == 0) {
		map[*ghostY][*ghostX] = ' ';
	} else if (*covers == 1) {
		map[*ghostY][*ghostX] = '.';
	} else if (*covers == 2) {
		map[*ghostY][*ghostX] = 'O';
	} else if (*covers == 3) {
		map[*ghostY][*ghostX] = 'x';
	} else {
		//Should not get here unless P=NP. Uh, right.
		std::cout << "WTF?\n";
	}

	//If ghost is gonna cover something, save it somewhere first
	if (map[newY][newX] == ' ') {
		*covers = 0;
	} else if (map[newY][newX] == '.') {
		*covers = 1;
	} else if (map[newY][newX] == 'O') {
		*covers = 2;
	} else if (map[newY][newX] == 'x') {
		*covers = 3;
	} else {
		//Should not get here unles 2+2=5.
		//For extremely large values of 2.
		std::cout << "Pakshit\n";
	}

	*ghostX = newX;
	*ghostY = newY;

	map[newY][newX] = ghost;
}
Beispiel #7
0
	bool Pathfinder::search(const Vector2i &start, Vector2i end, NodePath &path, Map *map, const GameObject *forObj)
	{
		if (!map || 
			start.x < 0 || start.x >= map->getMapWidth() ||
			start.y < 0 || start.y >= map->getMapHeight() ||
			end.x < 0 || end.x >= map->getMapWidth() ||
			end.y < 0 || end.y >= map->getMapHeight() ||
			(start.x == end.x && start.y == end.y))
		{
			return false;
		}

		mMap = map;
		int startGroup = map->mMapData[start.x][start.y].group;
		int endGroup = map->mMapData[end.x][end.y].group;
		if(startGroup != endGroup)
		{
			mMap = NULL;
			return false;
		}

		if (!map->isValidGridLocation(end.x, end.y, forObj))
		{
			// Move back towards the start position until we do find a passable location.
			Engine *engine = Engine::getEngine();
			Vector2f fstart(start);
			Vector2f fend(end);
			Vector2f toStart(fend.sub(fstart));
			int numSteps = static_cast<int>(toStart.length() * 2.0f);
			toStart.normalise();
			toStart.scale(0.5f);

			for (int step = 0; step < numSteps; step++)
			{
				fend = fend.sub(toStart);
				Vector2i grid(fend);
				if (map->isValidGridLocation(grid.x, grid.y, forObj))
				{
					break;
				}
			}

			end.x = round(fend.x);
			end.y = round(fend.y);

			endGroup = map->mMapData[end.x][end.y].group;
			if(startGroup != endGroup)
			{
				mMap = NULL;
				return false;
			}
		}

		mOpenList.clear();
		mClosedList.clear();

		mNodeUseCounter++;

		mOpenList.push_back(&map->mMapData[start.x][start.y]);

		AStarNode *endNode = &map->mMapData[end.x][end.y];
		endNode->parent = NULL;

		while(!mOpenList.empty())
		{
			AStarNode *node = mOpenList.front();
			if(node->useCounter < mNodeUseCounter)
			{
				node->g = 0;
				node->useCounter = mNodeUseCounter;
				node->parent = NULL;
			}
		
			if (node == endNode)
			{
				// Complete
				getPath(node, path);
				mMap = NULL;
				return true;
			}
			else
			{
				mOpenList.erase(mOpenList.begin());
				mClosedList.push_back(node);

				mNeighbors.clear();
				getNeighbors(node->gridPosition, forObj);
				for(auto iter = mNeighbors.begin(); iter != mNeighbors.end(); ++iter)
				{
					AStarNode *n = *iter;
					if (!Utils::listContains(mOpenList, n) &&
						!Utils::listContains(mClosedList, n))
					{
						if (n->useCounter < mNodeUseCounter)
						{
							n->g = 0;
							n->useCounter = mNodeUseCounter;
						}
						n->g += node->g;

						n->f = n->g + manhattanDistance(n->position, endNode->position);

						n->parent = node;
						mOpenList.push_back(n);
					}
				}

				mNeighbors.clear();
				sortAStarList(mOpenList);
			}
		}
		// NO PATH! D:
		mMap = NULL;
		return false;
	}