Exemplo n.º 1
0
    def __call__(self) -> tuple:
        """
		Returns keyboard input, ignores vision.

		Parameters
		----------
		direction: tuple
			Current global direction Snake is facing

		Returns
		-------
		tuple: (new global direction, move necessary to have Snake oriented to this direction)
		"""
        if keyboard.is_pressed("w") or keyboard.is_pressed("up"):
            move = UP
        elif keyboard.is_pressed("a") or keyboard.is_pressed("left"):
            move = LEFT
        elif keyboard.is_pressed("s") or keyboard.is_pressed("down"):
            move = DOWN
        elif keyboard.is_pressed("d") or keyboard.is_pressed("right"):
            move = RIGHT
        else:
            move = self.direction

        newDirection = {
            False: move,
            True: self.direction
        }[move == (-self.direction[0], -self.direction[1])]
        move = brain.getOrientedDirection(self.direction, newDirection,
                                          "global")

        return newDirection, move
Exemplo n.º 2
0
def getDistances(origin, direction, target):
    distances = {(-1, 0): 0, (0, 1): 0, (1, 0): 0}
    for turnDirection in distances:
        newDirection = brain.getOrientedDirection(direction, turnDirection,
                                                  "local")
        probe = (origin[0] + newDirection[0], origin[1] + newDirection[1])
        distances[turnDirection] = brain.dist(probe, target)
    return distances
Exemplo n.º 3
0
def getMoveFromPath(path, body, direction):
    if path:
        moveTo = path.pop()
        nextDirection = (moveTo[0] - body[0][0], moveTo[1] - body[0][1])
        nextMove = brain.getOrientedDirection(direction, nextDirection,
                                              "global")
    else:
        nextDirection, nextMove = direction, (0, 1)
    return nextDirection, nextMove
Exemplo n.º 4
0
def getOpenness(body, direction, environment, depth=25):
    openness = {(-1, 0): 0, (0, 1): 0, (1, 0): 0}
    for turnDirection in openness:
        newDirection = brain.getOrientedDirection(direction, turnDirection,
                                                  "local")
        if environment[(probe :=
                        (body[0][0] + newDirection[0], body[0][1] +
                         newDirection[1]))] != -1:  # if adjacent space is open
            openness[turnDirection] = brain.floodFillCount(
                environment.copy(), probe,
                depth=depth)  # might have to switch to deep copy
Exemplo n.º 5
0
 def calcMoves(self,
               body,
               direction,
               awareness,
               environment,
               hunger,
               noise=0):
     if self.memories:
         self.nextDirection = self.memories.pop()
         self.nextMove = brain.getOrientedDirection(direction,
                                                    self.nextDirection,
                                                    "global")
     else:
         self.nextMove, self.nextDirection = (0, 0), (0, 0)
Exemplo n.º 6
0
def getShieldedNetworkDecision(
    network, body, direction, vision
):  # split into shielded and non shielded function options to avoid unnecesary conditional
    decision = np.argmax(network.feedForward(vision))
    nextMove = {0: (-1, 0), 1: (0, 1), 2: (1, 0)}[decision]  # local direction
    lethalMoves = {
        direction
        for direction, danger in zip([(-1, 0), (0, 1), (1, 0)], [
            vision[11] == 1 or vision[19] == 1, vision[8] == 1
            or vision[16] == 1, vision[9] == 1 or vision[17] == 1
        ]) if danger
    }
    nextMove = smartShield(body[0], nextMove, lethalMoves)
    nextDirection = brain.getOrientedDirection(direction, nextMove, "local")
    return nextDirection, nextMove
Exemplo n.º 7
0
def getCloseSafeMove(openness, distances, direction, threshold=1):
    safeMove = max(openness, key=openness.get)
    maxSafety = openness[safeMove]

    rewardMove = min(distances, key=distances.get)
    #print()
    #print(openness)
    #print(distances)
    if maxSafety > 0 and openness[rewardMove] / maxSafety >= threshold:
        #print("Reward")
        nextMove = rewardMove
    else:
        nextMove = safeMove
        #print("Safe")

    nextDirection = brain.getOrientedDirection(direction, nextMove, "local")
    return nextDirection, nextMove
Exemplo n.º 8
0
def getClosestMove(distances, direction):
    nextMove = min(distances, key=distances.get)
    nextDirection = brain.getOrientedDirection(direction, nextMove, "local")

    return nextDirection, nextMove
Exemplo n.º 9
0
def getNetworkDecision(network, body, direction, vision):
    decision = np.argmax(network.feedForward(vision))
    nextMove = {0: (-1, 0), 1: (0, 1), 2: (1, 0)}[decision]
    nextDirection = brain.getOrientedDirection(direction, nextMove, "local")
    return nextDirection, nextMove
Exemplo n.º 10
0
def getSafestMove(openness, direction):
    nextMove = max(openness, key=openness.get)
    nextDirection = brain.getOrientedDirection(direction, nextMove, "local")

    return nextDirection, nextMove