分享下找到的Github上大神的EmpireofCode進攻策略:反正我是用了沒反應,改了代碼後單位不進攻,蠢站在那裡,我自己的策略調調能打敗不少人,這個日後慢慢研究吧,Github上暫時找到的唯一策略

来源:http://www.cnblogs.com/fengbo1113/archive/2017/12/24/8095185.html
-Advertisement-
Play Games

from queue import PriorityQueue from enum import Enum from battle import commander from battle import ROLE class Map(object): def __init__(self, enemy... ...


from queue import PriorityQueue
from enum import Enum

from battle import commander
from battle import ROLE


class Map(object):
    def __init__(self, enemy_items, diagonal_movement=True):
        self.diags = diagonal_movement
        self.width = 20
        self.height = 40
        self._data = [[TILE.EMPTY for y in range(self.height)]for x in range(self.width)]
        self.populate_map(enemy_items)
        self.evil_callback = 0
        
    def populate_map(self, enemy_items):
        valid = lambda w:  any([isinstance(w, int), isinstance(w, float)])
        for entity in enemy_items:
            size = entity.get('size', 0)
            x,y = entity['coordinates']
            role = entity['role']
            if x+size>=self.width or y+size>=self.height:
                self.extend_map((x+size,y+size))
            if not valid(x) or not valid(y):
                self.evil_callback+=1
                continue
            
            x = int(x)
            y = int(y)
            for x0, y0 in self.get_rect_from_center((x,y), size):
                self._data[x0][y0] = MappedRoles.get(role, TILE.UNKNOWN)

    def extend_map(self,point):
        w,h = point
        if h>self.height:
            for y in self._data:
                y.extend([TILE.EMPTY for _ in range(h-self.height)])
            self.height = h
        if w>self.width:
            for _ in range(w-self.width):
                self._data.append([TILE.EMPTY for _ in range(self.height)])
            self.width = w

            
    def find_path(self, start, end, size=1, heuristic=None):
        """
        A*
        """
        if not heuristic:
            heuristic = lambda x,y:abs(x-end[0])+abs(y-end[1])

        dest = set(self.get_rect_from_center(end, size))
        start = tuple(start)
        fringe = PriorityQueue()
        fringe.put(start, 0)
        total_cost = {start:0}

        origin = {start:None}
        while not fringe.empty():
            current = fringe.get()
            if current in dest:
                break
            cost = total_cost[current]+1
            nodes = self.get_adjacent(current[0], current[1])
            for node in nodes:
                x,y = node
                node_type = self._data[x][y]
                if (node_type == TILE.EMPTY or node in dest) and (node not in total_cost or cost < total_cost[node]):
                    total_cost[node] = cost 
                    origin[node] = current
                    fringe.put(node, cost+heuristic(x, y))
        else:
            return []

        path = []
        n = current
        while n is not None:
            path.append(n)
            n = origin[n]
        path.reverse()
        print(path)
        return path

    def update(self, info):
        self.init_map(info)
        
    def get_bounds(self, x0,y0,x1,y1):
        xmin, xmax = (max(0, x0), min(self.width, x1))
        ymin, ymax = (max(0, y0), min(self.height, y1))
        return(xmin,ymin, xmax,ymax)
    
    def get_rect_from_center(self, pos, size): ## dist 0: single-point
        if size < 2:                        ## and single-tile bulidings are
            return [pos]                    ## handled in the same manner as of now
            
        even = size %2 == 0
        dist = int(size/2)
        px,py = pos
        if even: ##x/ymax will be exclusive in the for loop!
            xmin, ymin, xmax, ymax = self.get_bounds(px-dist+1, py-dist+1, px+dist+1, py+dist+1)
        else:
            xmin, ymin, xmax, ymax = self.get_bounds(px-dist, py-dist, px+dist+1, py+dist+1)
        print('for {} dist:{} we get a rect ({})'.format(pos, size, (xmin, ymin, xmax-1, ymax-1)))
        cells = []
        for x in range(xmin, xmax):
            for y in range(ymin, ymax):
                cells.append((x,y))
        return cells
        
    def get_rect(self, top_left, size):
        cells = []
        x0,y0 = top_left
        xmin,ymin, xmax,ymax = self.get_bounds(x0,y0,x0+size, y0+size)
        for x in range(xmin, xmax):
            for y in range(ymin, ymax):
                cells.append((x,y))
        return cells

    def get_adjacent(self, x0,y0, diag = True):
        """
        Returns a list of tuples of coords adjacent to (x0,y0)
            7 8 9
            4 @ 6
            1 2 3
        """
        res =[]
        if x0>0:
            res.append((x0-1,y0))       # 4
            if y0>0 and diag:
              res.append((x0-1,y0-1))   # 7
            if y0 < self.height-1 and diag:
              res.append((x0-1,y0+1))   # 1

        if y0>0:
            res.append((x0,y0-1))       # 8

        if x0 < self.width-1:
            res.append((x0+1,y0))       # 6
            if y0 < self.height-1 and diag:
                res.append((x0+1,y0+1)) # 3
        if y0>0 and diag:
            res.append((x0+1,y0-1))     # 9
    
        if y0 < self.height-1:
            res.append((x0,y0+1))       # 2
        
        return [tuple(x) for x in res]
          
    def get_info(self):
        for x in range(self.width):
            for y in range(self.height):
                a = self._data[x][y]
                if a != TILE.EMPTY :
                    print("[{},{}],{}".format(x,y,a))
                    
    def __getitem__(self, index):
        return self._data[index]


class Brain(object):
    """
    (0,0) is located in the northen corner of the map.
    X N->SE
    Y N-SW
    """
    def __init__(self,client, init_terrain=True):
        self.me = client
        self.update()
        if init_terrain:
            self.terrain = Map(self.me.ask_enemy_items())
    
    def update(self):
        info = self.me.ask_my_info()
        self.fire_range = info['firing_range']
        self.pos = info['coordinates']
        self.size = info.get('size', 0)
        
    def goto(self, pos, size = 1):
        return self.terrain.find_path(self.pos, pos, size)

def midpoint(p1, p2):
    return ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)
    
def dist(p1, p2):
    x,y = p1
    ox,oy = p2
    return pow((ox-x)*(ox-x) + (oy-y)*(oy-y), 1/2)
    
def search_role(array,role):
    for e in array:
        if e['role'] == role:
            return True
    return False

def search_and_destroy(data=None, *args, **kawargs):
    brain.update()
    
    center = unit.ask_center()
    cpos = center['coordinates']
    assert brain.terrain[cpos[0]][cpos[1]] == TILE.CENTER, 'CENTER NOT FOUND'
    mypos = unit.ask_my_info()['coordinates']
    if center["hit_points"]>0:
        if dist(cpos, mypos) < brain.fire_range:
            print('attacking center')
            unit.attack_item(center['id'])
            unit.subscribe_the_item_is_dead(center['id'], search_and_destroy)
        else:
            print('walking')
            p = path.pop()
            print(p)
            unit.move_to_point(p)
            
            unit.do_move(midpoint(cpos, mypos))
            
            unit.subscribe_im_idle(search_and_destroy)
    else:
        print('attacking other')
        eid=  unit.ask_nearest_enemy()['id']
        unit.attack_item(eid)
        unit.subscribe_the_item_is_dead(eid, search_and_destroy)
    unit.move_to_point((mypos+[0,brain.terrain.evil_callback]))

#######
TILE = Enum('TileContent', 'EMPTY UNIT ROCK BUILDING TURRET CENTER UNKNOWN')
STATE = Enum("State", "ATTACKING FLEEING MOVING")
MappedRoles = {
    ROLE.UNIT:TILE.UNIT,
    ROLE.BUILDING:TILE.BUILDING,
    ROLE.OBSTACLE:TILE.ROCK,
    ROLE.TOWER:TILE.TURRET,
    ROLE.CENTER:TILE.CENTER
    }
    
if __name__ == "__main__":
    unit = commander.Client()
    brain = Brain(unit, True)
    cx,cy=unit.ask_center()['coordinates']
    path = brain.goto((cx,cy),4)
    path.reverse()

    brain.terrain.get_rect_from_center((2,2),0)
    brain.terrain.get_rect_from_center((2,2),1)
    brain.terrain.get_rect_from_center((2,2),-1)
    brain.terrain.get_rect_from_center((2,2),2)
    brain.terrain.get_rect_from_center((5,5),3)

    search_and_destroy()

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 整體思路 代碼部分 2.對左屏(相機偏移的場景)重新進行渲染(暫時解決方案,對相機外的場景同樣進行渲染,存在的問題:效率太低) 有待解決的問題 相機偏移後(左屏),應當對場景(左屏)重新進行渲染。具體指 ...
  • for(var i=0;i<as.length;i++){ as[i].onmouseover=function(){this.style.backgroundColor='grey';} as[i].onmouseout=function(){this.style.backgroundColor= ...
  • <script>window.onload = function(){ var u = navigator.userAgent; var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); if(isiOS){ if(location.search ...
  • spring mvc簡介與運行原理 Spring的模型-視圖-控制器(MVC)框架是圍繞一個DispatcherServlet來設計的,這個Servlet會把請求分發給各個處理器,並支持可配置的處理器映射、視圖渲染、本地化、時區與主題渲染等,甚至還能支持文件上傳。 原理.png (1) Http請求 ...
  • 題目描述 如題,給定一棵有根多叉樹,請求出指定兩個點直接最近的公共祖先。 輸入輸出格式 輸入格式: 第一行包含三個正整數N、M、S,分別表示樹的結點個數、詢問的個數和樹根結點的序號。 接下來N-1行每行包含兩個正整數x、y,表示x結點和y結點之間有一條直接連接的邊(數據保證可以構成樹)。 接下來M行 ...
  • 1507: [NOI2003]Editor Description Input 輸入文件editor.in的第一行是指令條數t,以下是需要執行的t個操作。其中: 為了使輸入文件便於閱讀,Insert操作的字元串中可能會插入一些回車符,請忽略掉它們(如果難以理解這句話,可以參考樣例)。 除了回車符之外 ...
  • 前言 樹鏈剖分是什麼? 樹鏈剖分,說白了就是一種讓你代碼不得不強行增加1k的數據結構-dms 個人理解:+1:joy: 有什麼用? 證明出題人非常毒瘤 可以非常友(bao)好(li)的解決一些樹上問題:grimacing: (友情提示:學樹鏈剖分之前請先掌握線段樹) 核心思想 樹鏈剖分的思想比較神奇 ...
  • 在上篇《Java IO(2)阻塞式輸入輸出(BIO)》的末尾談到了什麼是阻塞式輸入輸出,通過Socket編程對其有了大致瞭解。現在再重新回顧梳理一下,對於只有一個“客戶端”和一個“伺服器端”來講,伺服器端需要阻塞式接收客戶端的請求,這裡的阻塞式表示伺服器端的應用代碼會被掛起直到客戶端有請求過來,在高 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...