示例#1
0
    def act(self, game: Game, snake_idx: int) -> Optional[Direction]:
        snake = game.get_snake(snake_idx)
        all_snacks = game.snakes
        grid_map = game.get_grid()
        opponent = None

        for i, s in enumerate(all_snacks):
            if i == snake_idx:
                continue
            opponent = s

        head = snake.get_head()
        # print('position of head [%d, %d] and direction: %s' % (head.x, head.y, head.direction))
        fruits = game.get_fruits()
        direction = None
        for fruit in fruits:
            cost, path = KILabAgent.a_star_search(game, head, head.direction, fruit, grid_map)
            field = path[0]
            if not opponent.is_dead():
                head_op = opponent.get_head()
                op_to_fruit = abs(head_op.x - fruit.x) + abs(head_op.y - fruit.y)
                to_fruit = abs(head.x - fruit.x) + abs(head.y - fruit.y)
                if snake.length() < opponent.length():
                    if to_fruit > op_to_fruit:
                        f = grid_map.get_object_at(int(game.width/2), int(game.height/2))
                        while game.is_obstacle(f):
                            f = grid_map.get_object_at(f.x + 1, f.y + 1)
                        field = f

            delta_x = head.x - field.x
            delta_y = head.y - field.y
            if delta_y == 0:
                if delta_x > 0:
                    direction = Direction.LEFT
                else:
                    direction = Direction.RIGHT
            if delta_x == 0:
                if delta_y > 0:
                    direction = Direction.UP
                else:
                    direction = Direction.DOWN

        return direction
示例#2
0
 def Te(T, game: Game, snake_idx: Tu):
     G = []
     I = TW
     for Y, snake in Tb(game.snakes):
         if not snake.body:
             continue
         if Y == snake_idx:
             e = snake
         else:
             f = snake
             G = f.body
             S = G[0].x
             J = G[0].y
     P = e.body
     N = P[0].x
     L = P[0].y
     C = P[-1].x
     a = P[-1].x
     H = game.width
     k = game.height
     w = game.get_fruits()
     v = H * 2 + k * 2
     for f in w:
         u = f.x
         W = f.y
         b = TX(L - W) + TX(N - u)
         if b < v:
             v = b
             X = f.x
             x = f.y
     o = 0
     for f in w:
         u = f.x
         W = f.y
         b = TX(L - W) + TX(N - u)
         for Y, snake in Tb(game.snakes):
             if not snake.body:
                 continue
             if Y == snake_idx:
                 continue
             else:
                 f = snake
                 G = f.body
                 S = G[0].x
                 J = G[0].y
                 B = TX(J - x) + TX(S - X)
                 if B <= b:
                     o += 0
                     continue
                 else:
                     b = TX(L - W) + TX(N - u)
                     o += 1
                     if b < v:
                         v = b
                         X = f.x
                         x = f.y
     if o == 0:
         for f in w:
             u = f.x
             W = f.y
             b = TX(L - W) + TX(N - u)
             if b < v:
                 v = b
                 X = f.x
                 x = f.y
     s = Tw((H, k))
     R = [n for n in Tx(100)]
     for s in P:
         s[s.x, s.y] = 1
     if To(game.snakes) > 1:
         for e in G:
             s[e.x, e.y] = 1
     for Y, snake in Tb(game.snakes):
         if not snake.body:
             continue
         if Y == snake_idx:
             continue
         else:
             f = snake
             G = f.body
             S = G[0].x
             J = G[0].y
             for e in G:
                 s[e.x, e.y] = 1
             if To(G) >= To(P):
                 for f in w:
                     if T.TJ(s, (S, J), (f.x, f.y)) == TB:
                         continue
                     else:
                         i = T.TJ(s, (S, J), (f.x, f.y))
                         if To(i) <= 3:
                             if To(i) < To(R):
                                 R = i
                                 z = R[-1]
                                 V = z[0]
                                 E = z[1]
                                 s[V, E] = 1
     M = [n for n in Tx(100)]
     y = [width for width in Tx(H)]
     p = [height for height in Tx(k)]
     for w in y:
         s[w, 0] = 1
         s[w, k - 1] = 1
     for h in p:
         s[0, h] = 1
         s[H - 1, h] = 1
     for f in w:
         if T.TJ(s, (N, L), (f.x, f.y)) == TB:
             continue
         else:
             n = T.TJ(s, (N, L), (f.x, f.y))
             m = Ts
             if To(n) < To(M):
                 M = n
                 T.goal_x = f.x
                 T.goal_y = f.y
     for Y, snake in Tb(game.snakes):
         if not snake.body:
             continue
         if Y == snake_idx:
             continue
         else:
             f = snake
             G = f.body
             S = G[0].x
             J = G[0].y
             if T.TJ(s, (N, L), (T.TN(S, J, T.last_enemy_head_coord_x,
                                      T.last_enemy_head_coord_y))) == TB:
                 continue
             else:
                 if T.TJ(s, (N, L),
                         (T.TN(S, J, T.last_enemy_head_coord_x,
                               T.last_enemy_head_coord_y))) == []:
                     continue
                 else:
                     if To(P) > To(G) + 1:
                         n = T.TJ(s, (N, L),
                                  (T.TN(S, J, T.last_enemy_head_coord_x,
                                        T.last_enemy_head_coord_y)))
                         if To(n) < To(M):
                             TR('ATTACKING!')
                             M = n
                             T.goal_x = S
                             T.goal_y = J
     r = [n for n in Tx(100)]
     s[T.goal_x, T.goal_y] = 9
     U = 0
     if M != [n for n in Tx(100)]:
         for f in w:
             for p in M:
                 s[p[0], p[1]] = 1
             if f.x == T.goal_x and f.y == T.goal_y:
                 U += 0
             else:
                 if T.TJ(s, (T.goal_x, T.goal_y), (f.x, f.y)) == []:
                     U += 0
                 else:
                     if T.TJ(s, (T.goal_x, T.goal_y), (f.x, f.y)) == TB:
                         U += 0
                         if T.TJ(s, (N, L), (f.x, f.y)) != TB:
                             n = T.TJ(s, (N, L), (f.x, f.y))
                             if To(n) < To(r):
                                 r = n
                         else:
                             continue
                     else:
                         U += 1
         for p in M:
             s[p[0], p[1]] = 2
         s[T.goal_x, T.goal_y] = 9
         if U == 0:
             if r != [n for n in Tx(100)]:
                 M = r
                 U += 1
     if U == 0:
         for w in y:
             s[w, 0] = 0
             s[w, k - 1] = 0
         for h in p:
             s[0, h] = 0
             s[H - 1, h] = 0
         for s in P:
             s[s.x, s.y] = 1
         if To(game.snakes) > 1:
             for e in G:
                 s[e.x, e.y] = 1
         for f in w:
             if T.TJ(s, (N, L), (f.x, f.y)) == TB:
                 continue
             else:
                 n = T.TJ(s, (N, L), (f.x, f.y))
                 m = Ts
                 if To(n) < To(M):
                     M = n
                     T.goal_x = f.x
                     T.goal_y = f.y
     if M == [n for n in Tx(100)]:
         s[C, a] = 0
         if T.TJ(s, (N, L), (C, a)) == TB:
             TR('Cant find tail!')
         else:
             n = T.TJ(s, (N, L), (C, a))
             if To(n) > 1:
                 M = n
                 TR('Chasing tail!')
     s[C, a] = 1
     if M == [n for n in Tx(100)]:
         M = []
     if M == []:
         for h in p:
             for w in y:
                 c = T.TJ(s, (N, L), (w, h))
                 if c != TB and c != []:
                     TR('found')
                     if To(c) > To(M):
                         M = c
                         TR('PATH:', M)
     for Y, snake in Tb(game.snakes):
         if not snake.body:
             continue
         if Y == snake_idx:
             continue
         else:
             T.last_enemy_head_coord_x = S
             T.last_enemy_head_coord_y = J
     if M != []:
         j = M[-1]
         d = j[0]
         A = j[1]
         I = T.TP(d, A, N, L)
     else:
         TR()
         TR('DEAD!')
         TR()
     return I
示例#3
0
 def act(QDKrSREt, game: Game, snake_idx: int):
     QDKrSREC = []
     QDKrSREe = None
     for QDKrSREf, snake in enumerate(game.snakes):
         if not snake.body:
             continue
         if QDKrSREf == snake_idx:
             QDKrSREM = snake
         else:
             QDKrSREn = snake
             QDKrSREC = QDKrSREn.body
             QDKrSREF = QDKrSREC[0].x
             QDKrSREL = QDKrSREC[0].y
     QDKrSREm = QDKrSREM.body
     QDKrSREz = QDKrSREm[0].x
     QDKrSREG = QDKrSREm[0].y
     QDKrSREo = QDKrSREm[-1].x
     QDKrSREW = QDKrSREm[-1].x
     QDKrSREg = game.width
     QDKrSREU = game.height
     QDKrSREs = game.get_fruits()
     QDKrSREJ = QDKrSREg * 2 + QDKrSREU * 2
     for QDKrSREV in QDKrSREs:
         QDKrSREx = QDKrSREV.x
         QDKrSREd = QDKrSREV.y
         QDKrSREN = abs(QDKrSREG - QDKrSREd) + abs(QDKrSREz - QDKrSREx)
         if QDKrSREN < QDKrSREJ:
             QDKrSREJ = QDKrSREN
             QDKrSREv = QDKrSREV.x
             QDKrSREX = QDKrSREV.y
     QDKrSREj = 0
     for QDKrSREV in QDKrSREs:
         QDKrSREx = QDKrSREV.x
         QDKrSREd = QDKrSREV.y
         QDKrSREN = abs(QDKrSREG - QDKrSREd) + abs(QDKrSREz - QDKrSREx)
         for QDKrSREf, snake in enumerate(game.snakes):
             if not snake.body:
                 continue
             if QDKrSREf == snake_idx:
                 continue
             else:
                 QDKrSREn = snake
                 QDKrSREC = QDKrSREn.body
                 QDKrSREF = QDKrSREC[0].x
                 QDKrSREL = QDKrSREC[0].y
                 QDKrSREy = abs(QDKrSREL - QDKrSREX) + abs(QDKrSREF -
                                                           QDKrSREv)
                 if QDKrSREy <= QDKrSREN:
                     QDKrSREj += 0
                     continue
                 else:
                     QDKrSREN = abs(QDKrSREG - QDKrSREd) + abs(QDKrSREz -
                                                               QDKrSREx)
                     QDKrSREj += 1
                     if QDKrSREN < QDKrSREJ:
                         QDKrSREJ = QDKrSREN
                         QDKrSREv = QDKrSREV.x
                         QDKrSREX = QDKrSREV.y
     if QDKrSREj == 0:
         for QDKrSREV in QDKrSREs:
             QDKrSREx = QDKrSREV.x
             QDKrSREd = QDKrSREV.y
             QDKrSREN = abs(QDKrSREG - QDKrSREd) + abs(QDKrSREz - QDKrSREx)
             if QDKrSREN < QDKrSREJ:
                 QDKrSREJ = QDKrSREN
                 QDKrSREv = QDKrSREV.x
                 QDKrSREX = QDKrSREV.y
     QDKrSREP = numpy.zeros((QDKrSREg, QDKrSREU))
     QDKrSREY = [n for n in range(100)]
     for QDKrSREA in QDKrSREm:
         QDKrSREP[QDKrSREA.x, QDKrSREA.y] = 1
     if len(game.snakes) > 1:
         for QDKrSREH in QDKrSREC:
             QDKrSREP[QDKrSREH.x, QDKrSREH.y] = 1
     for QDKrSREf, snake in enumerate(game.snakes):
         if not snake.body:
             continue
         if QDKrSREf == snake_idx:
             continue
         else:
             QDKrSREn = snake
             QDKrSREC = QDKrSREn.body
             QDKrSREF = QDKrSREC[0].x
             QDKrSREL = QDKrSREC[0].y
             for QDKrSREH in QDKrSREC:
                 QDKrSREP[QDKrSREH.x, QDKrSREH.y] = 1
             if len(QDKrSREC) >= len(QDKrSREm):
                 for QDKrSREV in QDKrSREs:
                     if not QDKrSREt.astar(QDKrSREP, (QDKrSREF, QDKrSREL),
                                           (QDKrSREV.x, QDKrSREV.y)):
                         continue
                     else:
                         QDKrSREp = QDKrSREt.astar(QDKrSREP,
                                                   (QDKrSREF, QDKrSREL),
                                                   (QDKrSREV.x, QDKrSREV.y))
                         if len(QDKrSREp) <= 3:
                             if len(QDKrSREp) < len(QDKrSREY):
                                 QDKrSREY = QDKrSREp
                                 QDKrSRET = QDKrSREY[-1]
                                 QDKrSREi = QDKrSRET[0]
                                 QDKrSREc = QDKrSRET[1]
                                 QDKrSREP[QDKrSREi, QDKrSREc] = 1
     QDKrSREa = [n for n in range(100)]
     QDKrSREw = [QDKrSREu for QDKrSREu in range(QDKrSREg)]
     QDKrSREl = [QDKrSREI for QDKrSREI in range(QDKrSREU)]
     for QDKrSREu in QDKrSREw:
         QDKrSREP[QDKrSREu, 0] = 1
         QDKrSREP[QDKrSREu, QDKrSREU - 1] = 1
     for QDKrSREI in QDKrSREl:
         QDKrSREP[0, QDKrSREI] = 1
         QDKrSREP[QDKrSREg - 1, QDKrSREI] = 1
     for QDKrSREV in QDKrSREs:
         if not QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                               (QDKrSREV.x, QDKrSREV.y)):
             continue
         else:
             QDKrSREh = QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                                       (QDKrSREV.x, QDKrSREV.y))
             QDKrSREb = True
             if len(QDKrSREh) < len(QDKrSREa):
                 QDKrSREa = QDKrSREh
                 QDKrSREt.goal_x = QDKrSREV.x
                 QDKrSREt.goal_y = QDKrSREV.y
     for QDKrSREf, snake in enumerate(game.snakes):
         if not snake.body:
             continue
         if QDKrSREf == snake_idx:
             continue
         else:
             QDKrSREn = snake
             QDKrSREC = QDKrSREn.body
             QDKrSREF = QDKrSREC[0].x
             QDKrSREL = QDKrSREC[0].y
             if not QDKrSREt.astar(
                     QDKrSREP, (QDKrSREz, QDKrSREG),
                 (QDKrSREt.calculate_next_move(
                     QDKrSREF, QDKrSREL, QDKrSREt.last_enemy_head_coord_x,
                     QDKrSREt.last_enemy_head_coord_y))):
                 continue
             else:
                 if not QDKrSREt.astar(
                         QDKrSREP, (QDKrSREz, QDKrSREG),
                     (QDKrSREt.calculate_next_move(
                         QDKrSREF, QDKrSREL,
                         QDKrSREt.last_enemy_head_coord_x,
                         QDKrSREt.last_enemy_head_coord_y))):
                     continue
                 else:
                     if len(QDKrSREm) > len(QDKrSREC) + 1:
                         QDKrSREh = QDKrSREt.astar(
                             QDKrSREP, (QDKrSREz, QDKrSREG),
                             (QDKrSREt.calculate_next_move(
                                 QDKrSREF, QDKrSREL,
                                 QDKrSREt.last_enemy_head_coord_x,
                                 QDKrSREt.last_enemy_head_coord_y)))
                         if len(QDKrSREh) < len(QDKrSREa):
                             print('ATTACKING!')
                             QDKrSREa = QDKrSREh
                             QDKrSREt.goal_x = QDKrSREF
                             QDKrSREt.goal_y = QDKrSREL
     QDKrSREk = [n for n in range(100)]
     QDKrSREP[QDKrSREt.goal_x, QDKrSREt.goal_y] = 9
     QDKrSREB = 0
     if QDKrSREa != [n for n in range(100)]:
         for QDKrSREV in QDKrSREs:
             for QDKrSREO in QDKrSREa:
                 QDKrSREP[QDKrSREO[0], QDKrSREO[1]] = 1
             if QDKrSREV.x == QDKrSREt.goal_x and QDKrSREV.y == QDKrSREt.goal_y:
                 QDKrSREB += 0
             else:
                 if QDKrSREt.astar(QDKrSREP,
                                   (QDKrSREt.goal_x, QDKrSREt.goal_y),
                                   (QDKrSREV.x, QDKrSREV.y)) == []:
                     QDKrSREB += 0
                 else:
                     if QDKrSREt.astar(QDKrSREP,
                                       (QDKrSREt.goal_x, QDKrSREt.goal_y),
                                       (QDKrSREV.x, QDKrSREV.y)) == False:
                         QDKrSREB += 0
                         if QDKrSREt.astar(
                                 QDKrSREP, (QDKrSREz, QDKrSREG),
                             (QDKrSREV.x, QDKrSREV.y)) != False:
                             QDKrSREh = QDKrSREt.astar(
                                 QDKrSREP, (QDKrSREz, QDKrSREG),
                                 (QDKrSREV.x, QDKrSREV.y))
                             if len(QDKrSREh) < len(QDKrSREk):
                                 QDKrSREk = QDKrSREh
                         else:
                             continue
                     else:
                         QDKrSREB += 1
         for QDKrSREO in QDKrSREa:
             QDKrSREP[QDKrSREO[0], QDKrSREO[1]] = 2
         QDKrSREP[QDKrSREt.goal_x, QDKrSREt.goal_y] = 9
         if QDKrSREB == 0:
             if QDKrSREk != [n for n in range(100)]:
                 QDKrSREa = QDKrSREk
                 QDKrSREB += 1
     if QDKrSREB == 0:
         for QDKrSREu in QDKrSREw:
             QDKrSREP[QDKrSREu, 0] = 0
             QDKrSREP[QDKrSREu, QDKrSREU - 1] = 0
         for QDKrSREI in QDKrSREl:
             QDKrSREP[0, QDKrSREI] = 0
             QDKrSREP[QDKrSREg - 1, QDKrSREI] = 0
         for QDKrSREA in QDKrSREm:
             QDKrSREP[QDKrSREA.x, QDKrSREA.y] = 1
         if len(game.snakes) > 1:
             for QDKrSREH in QDKrSREC:
                 QDKrSREP[QDKrSREH.x, QDKrSREH.y] = 1
         for QDKrSREV in QDKrSREs:
             if QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                               (QDKrSREV.x, QDKrSREV.y)) == False:
                 continue
             else:
                 QDKrSREh = QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                                           (QDKrSREV.x, QDKrSREV.y))
                 QDKrSREb = True
                 if len(QDKrSREh) < len(QDKrSREa):
                     QDKrSREa = QDKrSREh
                     QDKrSREt.goal_x = QDKrSREV.x
                     QDKrSREt.goal_y = QDKrSREV.y
     if QDKrSREa == [n for n in range(100)]:
         QDKrSREP[QDKrSREo, QDKrSREW] = 0
         if QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                           (QDKrSREo, QDKrSREW)) == False:
             print('Cant find tail!')
         else:
             QDKrSREh = QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                                       (QDKrSREo, QDKrSREW))
             if len(QDKrSREh) > 1:
                 QDKrSREa = QDKrSREh
                 print('Chasing tail!')
     QDKrSREP[QDKrSREo, QDKrSREW] = 1
     if QDKrSREa == [n for n in range(100)]:
         QDKrSREa = []
     if QDKrSREa == []:
         for QDKrSREI in QDKrSREl:
             for QDKrSREu in QDKrSREw:
                 QDKrSREq = QDKrSREt.astar(QDKrSREP, (QDKrSREz, QDKrSREG),
                                           (QDKrSREu, QDKrSREI))
                 if QDKrSREq != False and QDKrSREq != []:
                     print('found')
                     if len(QDKrSREq) > len(QDKrSREa):
                         QDKrSREa = QDKrSREq
                         print('PATH:', QDKrSREa)
     for QDKrSREf, snake in enumerate(game.snakes):
         if not snake.body:
             continue
         if QDKrSREf == snake_idx:
             continue
         else:
             QDKrSREt.last_enemy_head_coord_x = QDKrSREF
             QDKrSREt.last_enemy_head_coord_y = QDKrSREL
     if QDKrSREa != []:
         QDKrSRtE = QDKrSREa[-1]
         QDKrSRtC = QDKrSRtE[0]
         QDKrSRte = QDKrSRtE[1]
         QDKrSREe = QDKrSREt.calculate_direction(QDKrSRtC, QDKrSRte,
                                                 QDKrSREz, QDKrSREG)
     else:
         print()
         print('DEAD!')
         print()
     return QDKrSREe