我是新人 今天花了大概3个小时读了一下这个Excel的VBA源码 核心的规则设定在Computer_Player模块下的strComputer_Selects_Role等几个方法中,完全是hardcode,但是参数组很复杂,所以读代码还是没办法知道如何很容易的赢电脑。 另外在AI进化上,用了标准的遗传算法通过变异,交叉基因的过程得到更优质的子代样本(也就是p1-p那几个页了,每条数据就是一个具备个性的电脑玩家。),然后通过更多的模拟比赛淘汰劣质样本,这就是电脑强大的原因。模拟的次数越多电脑就越强。 每次人机对战的时候,会随机从每个位置分数前6中选择一个对手。如果没做大量的模拟,你会发现每个位置前6的基因(也就是excel表p1-p5里那一串串的字符)都是差不多的。 选角,选资源,选建筑都分为3个阶段,前(6轮前后2),中(10前后轮2),每次阶段切换也是按各个电脑玩家的基因数据来确定的,所以没有固定的轮数,对于前中后三个阶段,每个角色都有不同的基因。 其中有一段注释可以帮助大家理解基因段的大概含义 ' Role priorities: ' List of things computer must do in order ' 1 - 6 have manned production capacity (or quarry) ' G to W have manned building of this type ' z = Have best available special building (the one which scores most points) ' p = Best affordable production building for which already have a non-producing plantation ' " " spaces are ignored but make total length up to 20 characters ' Then "]" ' Then priority list of actions and triggers: ' mX = Mayor action if have at least X more spaces (building and plantation) than men ' mf = Take mayor action if there are 6 or 11 men on the ship ' sX = Settler action if plantation type X is available (1 to 6 or d=Different,s=Production space) ' bX = Builder action if have at least X cash plus manned quarries ' cX = Craftsman action if produce at least X goods (or s=manned warehouse (storage),w=manned wharf,e=either and 4+ goods) ' tX = Trader action if can earn at least X gold ' tf = Trade if can sell a good for at least 2 and nobody else can trade ' tt = Trade if can sell a good for at least 2 and only one other person can trade ' tu = Trade EITHER on tt basis or if get at least 4 gold ' tg = Trade EITHER on tf basis or if get at least 4 gold ' tm = Trade if get the most gold (and get at least 2) ' dX = Captain action if have at least X goods ' pX = Prospector action if earn at least X gold ' gX = Take best cash option if it provides at least X gold ' Then "]0" ' Then the default actions in order, i.e. msbctdp ' ' Example: ' "555 ]g2p3t3d3m3b3s6c4]0pdtsbmc"
电脑会优先过滤当前不可行的选项,然后按照必须,优先,默认的顺序来做选择,中间还会有一些输入值,比如剩下的分数,建筑,空间,人力,假定生产/上船获得的利益。
个人认为难度在于这些“基因”不具备可读性,仅仅只是输入值,需要配合约束条件(也就是strComputer_Selects_Role等方法)才能得到最后的选择,这也是为何读懂代码也无用的语原因。由于参数群过于庞大,即便弄清楚算法,依然无法保证赢过电脑,不过可以通过这些数据看学习一下起始的选择和各个位置的差异。 可尝试的是将其中几个主要的函数方法剥离出来,然后为自己制定一个基因段,每次选择前用这些方法看一下选择结果,当然那样就不是你玩游戏了,是电脑上你身玩游戏。。。不会有太多的乐趣。
写的比较乱,不知道有没有有兴趣的朋友可以一起研究研究。 |