diff --git a/DLS/DLS.py b/DLS/DLS.py new file mode 100644 index 0000000..cd1d5fe --- /dev/null +++ b/DLS/DLS.py @@ -0,0 +1,247 @@ +from typing import List, Tuple +import pygame +import heapq +import socket_client as s +from main import right_move, left_move +import init + +inf = 99999999 +MAP1 = init.clac_MAP1() + +def getMAP(Animals): + copyMAP = [[0 for i in range(7)] for j in range(9)] + for animalId in range(1, len(Animals)): + x, y = Animals[animalId]['old_x'], Animals[animalId]['old_y'] + copyMAP[x][y] = animalId + return copyMAP + + +class PriorityQueue: + + def __init__(self): + self.heap = [] + self.count = 0 + + def push(self, item, priority): + entry = (priority, self.count, item) + heapq.heappush(self.heap, entry) + self.count += 1 + + def pop(self): + (_, _, item) = heapq.heappop(self.heap) + return item + + def isEmpty(self): + return len(self.heap) == 0 + + def update(self, item, priority): + + for index, (p, c, i) in enumerate(self.heap): + if i == item: + if p <= priority: + break + del self.heap[index] + self.heap.append((priority, c, item)) + heapq.heapify(self.heap) + break + else: + self.push(item, priority) + + +def getMovement(Id, Animals): + MAP = getMAP(Animals) + Movements = [] + o_x, o_y = Animals[Id]['old_x'], Animals[Id]['old_y'] + for x in range(len(MAP)): + for y in range(len(MAP[0])): + if 1 <= Id <= 7: + if right_move(o_x, o_y, x, y, Id, MAP, MAP1): + move = (Id, x - o_x, y - o_y) + Movements.append(move) + else: + if right_move(o_x, o_y, x, y, Id, MAP, MAP1): + move = (Id, x - o_x, y - o_y) + Movements.append(move) + return Movements + + +def getSuccessors(Animals, turn: int): + Successors = [] + Actions = [] + if turn == 0: + # 1 ~ 7 + for Id in range(1, 8): + Move = getMovement(Id, Animals) + for oneStep in Move: + Actions.append(oneStep) + + else: + # 8 ~ 14 + for Id in range(8, 15): + Move = getMovement(Id, Animals) + for oneStep in Move: + Actions.append(oneStep) + + # update the map + for action in Actions: + Id, dx, dy = action[0], action[1], action[2] + t_Animals = Animals + x = Animals[Id]['old_x'] + dx + y = Animals[Id]['old_y'] + dy + t_Animals[Id]['x'] = x + t_Animals[Id]['y'] = y + print(t_Animals) + Successors.append(t_Animals) + # updateMAP() + return Successors + + +class Information: + def __init__(self): + self.piece_score = {1: 100, 8: 100, + 2: 70, 9: 70, + 3: 60, 10: 60, + 4: 80, 11: 80, + 5: 90, 12: 90, + 6: 120, 13: 120, + 7: 120, 14: 120 + } + self.mouse_score = [[11, 25, 50, 100000, 50, 25, 13], + [11, 20, 25, 50, 25, 20, 13], + [10, 15, 20, 20, 20, 15, 13], + [8, 9, 9, 11, 12, 13, 13], + [8, 8, 8, 9, 11, 12, 12], + [8, 8, 8, 9, 11, 12, 11], + [8, 8, 8, 9, 10, 10, 10], + [8, 8, 8, 9, 9, 9, 9], + [8, 8, 8, 0, 8, 8, 8]] + + self.eagle_score = [[11, 15, 50, 100000, 50, 15, 11], + [12, 12, 20, 50, 20, 15, 12], + [14, 15, 20, 20, 20, 14, 14], + [13, 0, 0, 13, 0, 0, 13], + [12, 0, 0, 12, 0, 0, 12], + [11, 0, 0, 11, 0, 0, 11], + [10, 10, 10, 10, 10, 10, 10], + [8, 8, 8, 8, 8, 8, 8], + [8, 8, 8, 0, 8, 8, 8]] + + self.fox_score = [[11, 15, 50, 100000, 50, 15, 11], + [12, 12, 20, 50, 20, 15, 12], + [14, 15, 20, 20, 20, 14, 14], + [13, 0, 0, 13, 0, 0, 13], + [12, 0, 0, 12, 0, 0, 12], + [11, 0, 0, 11, 0, 0, 11], + [10, 10, 10, 11, 10, 10, 10], + [8, 8, 9, 10, 9, 8, 8], + [8, 8, 10, 0, 10, 8, 8]] + + self.wolf_score = [[11, 15, 50, 100000, 50, 15, 11], + [12, 12, 20, 50, 20, 15, 12], + [14, 15, 20, 20, 20, 14, 14], + [13, 0, 0, 13, 0, 0, 13], + [12, 0, 0, 12, 0, 0, 12], + [11, 0, 0, 11, 0, 0, 11], + [10, 10, 10, 11, 10, 10, 10], + [8, 8, 9, 10, 9, 8, 8], + [8, 8, 10, 0, 10, 8, 8]] + + self.leopard_score = [[11, 15, 50, 100000, 50, 15, 11], + [12, 12, 20, 50, 20, 15, 12], + [14, 15, 20, 20, 20, 14, 14], + [13, 0, 0, 13, 0, 0, 13], + [12, 0, 0, 12, 0, 0, 12], + [11, 0, 0, 11, 0, 0, 11], + [10, 10, 10, 11, 10, 10, 10], + [8, 8, 9, 10, 9, 8, 8], + [8, 8, 10, 0, 10, 8, 8]] + + self.lion_score = [[20, 40, 50, 100000, 50, 40, 20], + [20, 25, 40, 50, 40, 25, 20], + [18, 30, 30, 20, 30, 30, 18], + [15, 0, 0, 15, 0, 0, 15], + [12, 0, 0, 15, 0, 0, 12], + [11, 0, 0, 15, 0, 0, 11], + [14, 16, 16, 9, 16, 16, 14], + [12, 12, 12, 12, 12, 12, 12], + [5, 12, 12, 0, 12, 12, 5]] + + self.elephant_score = [[20, 40, 50, 100000, 50, 40, 20], + [20, 25, 40, 50, 40, 25, 20], + [18, 30, 30, 20, 30, 30, 18], + [15, 0, 0, 15, 0, 0, 15], + [12, 0, 0, 15, 0, 0, 12], + [11, 0, 0, 15, 0, 0, 11], + [14, 16, 16, 9, 16, 16, 14], + [12, 12, 12, 12, 12, 12, 12], + [5, 12, 12, 0, 12, 12, 5]] + + self.position_score = {1: self.mouse_score, + 8: self.mouse_score[::-1], + 2: self.eagle_score, + 9: self.eagle_score[::-1], + 3: self.fox_score, + 10: self.fox_score[::-1], + 4: self.wolf_score, + 11: self.wolf_score[::-1], + 5: self.leopard_score, + 12: self.leopard_score[::-1], + 6: self.lion_score, + 13: self.lion_score[::-1], + 7: self.elephant_score, + 14: self.elephant_score[::-1] + } + + +def Heuristic(Animals, pTurn): + score = 0 + information = Information() + piece_score = information.piece_score + for animalId in range(1, len(Animals)): + x, y = Animals[animalId]['x'], Animals[animalId]['y'] + + if 1 <= animalId <= 14: + animalIdValue = information.position_score[animalId][x][y] + if pTurn == 0: + if 1 <= animalId <= 7: + score += (animalIdValue + piece_score[animalId]) + else: + score -= (animalIdValue + piece_score[animalId]) + else: + if 1 <= animalId <= 7: + score -= (animalIdValue + piece_score[animalId]) + else: + score += (animalIdValue + piece_score[animalId]) + return score + + +def depthLimitedSearch2(curAnimals, turn): # depth = 2 + Successors = getSuccessors(curAnimals, turn) + tempNextMove = [(Successor, inf) for Successor in Successors] + nextMove = [] + if turn: + turn2 = 0 + else: + turn2 = 1 + for Move in tempNextMove: + Successors2 = getSuccessors(Move[0], turn2) + nextMove2 = [(Successor2, Heuristic(Successor2, turn2)) for Successor2 in Successors2] + max2 = inf + for action2 in nextMove2: + if action2[1] < max2: + max2 = action2[1] + nextMove.append((Move, max2)) + min1 = -inf + index = 0 + for i in range(len(nextMove)): + if nextMove[i][1] > min1: + min1 = nextMove[i][1] + index = i + return nextMove[index][0] + + +axis = [(0, 0), (2, 6), (1, 1), (1, 5), (2, 2), (2, 4), (2, 3), (2, 0), (6, 0), (7, 5), (7, 1), (6, 4), (6, 2), + (6, 3), (6, 6)] +Axis = s.Chessmen_axis(axis) +Animals = Axis.axis +print(len(axis))