Starlight-30036225 / Ai-Assignment

Assets from Code the Classics
0 stars 0 forks source link

AStar algorithm for player to hunt fruit #2

Open Starlight-30036225 opened 5 months ago

Starlight-30036225 commented 5 months ago

I want the p[layer to use path finding to track to the closest fruit, with respect for fruit lifespan

Starlight-30036225 commented 5 months ago

Some things I need to keep in mind Increasing Y is moving down accessing map is [Y][X]

Player jumps 4 spaces up

going under the map teleports to the top.

Starlight-30036225 commented 5 months ago

I need to normalise start and target locations to board coords. This is done by multiplying the coordinates by (NUM_COLUMNS / WIDTH) then truncating

For now I will only move to fruits, so I will find a fruit that has landed so I can guarantee it can be moved too.

When investigating a node, the available adjacent squares is investiaged under a few conditions. By default, the player can move left and right. Then if there is a floor 3 spaces above, the player can jump to get there. So add up4. If there is a space below, the player will fall, so only move is to move down, remove all other options.

`` AdjactentSquares = [(1,0), (-1,0)] if currentLocation.position[1] - 3 < 0 and map[currentLocation.position[1] - 3][currentLocation.position[0]] == 1: AdjactentSquares.append(0,-4) if currentLocation.position[1] + 1 < (NUM_ROWS - 1) and map[currentLocation.position[1] + 1][currentLocation.position[0]] == 0:

print(currentLocation.position, " has gap under")

AdjactentSquares.clear()
AdjactentSquares.append((0,1))

When adjacent squares are found, iterate through them and check their validity. for Adjacent in AdjactentSquares: # Adjacent squares

Get node position

            NextLocation = (currentLocation.position[0] + Adjacent[0], currentLocation.position[1] + Adjacent[1])

            #checks location is in legal range
            if NextLocation[0] > (NUM_COLUMNS - 1) or NextLocation[0] < 0 or NextLocation[1] > (NUM_ROWS - 1) or NextLocation[1] < 0:
                continue

            # Make sure walkable terrain
            if not map[NextLocation[1]][NextLocation[0]] == 0:
                continue

            child = None
            child = Square(currentLocation, NextLocation)

            # Append
            children.append(child)

``

Then the children are iterated through, If they are already in the evaluated path, ignore it and move on. Otherwise calculate its values then add it to the open list to be evaluated next.

``

        for child in children:
            if child in closedList:
                continue
            child.heuristic = ((target[0]-child.position[0]) ** 2 + (target[1]-child.position[1]) ** 2)
            child.distance = currentLocation.distance + 1
            child.combined = child.heuristic + child.distance
            for OpenChild in openList:
                if child == OpenChild and child.distance >= OpenChild.distance:
                    continue
            openList.append(child)

``

Starlight-30036225 commented 5 months ago

The algorithm starts with the current location, checks all adjacent nodes, then from the adjacent nodes picks the one with the lowest combined value. then continues from there.

It continues until it finds a path. I need to make an exit clause if the test is taking too long.