def minimax(self, me, enemy, depth, state, alpha, beta): helper = Help() helper.enemy = self.enemy helper.me = self.me if (depth == 0 or helper.calc_heurestic2(state, me, enemy) >= 50): score = helper.calc_heurestic2(state, me, enemy) return score if (me == self.me): num = me chosen_map = None max = -9999 helper2 = Help2() children = helper2.make_children(self.me, state) for child in children: if (me == 1): num = child.control.count(1) num = int(num / 3) if (num < 3): num = 3 child.player1Available += num else: num = child.control.count(2) num = int(num / 3) if (num < 3): num = 3 child.player2Available += num next = self.minimax(self.enemy, self.me, depth - 1, child, alpha, beta) if (next > max): max = next child.player1Available -= num chosen_map = helper.deep_copy(child) if (next > alpha): alpha = next if (alpha >= beta): break if (children.__len__() == 0): chosen_map = helper.deep_copy(state) min = helper.calc_heurestic2(state, me, enemy) if (depth == 4): return chosen_map return max if (me == self.enemy): num = me chosen_map = None min = 999999 helper2 = Help2() children = helper2.make_children(self.enemy, state) for child in children: if (me == 1): num = child.control.count(1) num = int(num / 3) if (num < 3): num = 3 child.player1Available += num else: num = child.control.count(2) num = int(num / 3) if (num < 3): num = 3 child.player2Available += num next = self.minimax(self.me, self.enemy, depth - 1, child, alpha, beta) if (next < min): min = next child.player1Available -= num chosen_map = helper.deep_copy(child) if (next < beta): beta = next if (alpha >= beta): break if (children.__len__() == 0): chosen_map = helper.deep_copy(state) min = helper.calc_heurestic2(state, me, enemy) if (depth == 4): return chosen_map return min
def make_children(self, num, parents): adj = parents.adjacency ctrl = parents.control numList = parents.troopNum self.me = num if (num == 1): self.troops = parents.player1Available self.enemy = 2 if (num == 2): self.troops = parents.player2Available self.enemy = 1 helper = Help() parent = parents best_score = 99999999999 self.cost += 1 best_children = [] number = 0 children = [] visited = [] children.append(parent) while (1): children.remove(parent) current_children = helper.make_children(num, parent) for mapA in current_children: for mapB in visited: if (mapA.control == mapB.control and mapA.troopNum == mapB.troopNum): current_children.remove(mapA) break if (current_children.__len__() != 0): visited = visited + current_children.copy() children = children + current_children.copy() if children.__len__() != 0: best = helper.best_child(children) best_children.append(helper.deep_copy(best)) score = helper.calc_heurestic(best) + self.cost else: break best_score = score parent = best if (helper.calc_heurestic(parent) <= -50 or number >= 21): break number += 1 l_best = [] q = 0 while (q < best_children.__len__() and q < 2): l_best.append(helper.deep_copy(helper.best_child(best_children))) best_children.remove(helper.best_child(best_children)) q += 1 return l_best
def move(self, num, infoText, listL): adj = globals.map.adjacency ctrl = globals.map.control numList = globals.map.troopNum self.me = num if (num == 1): self.troops = globals.map.player1Available self.enemy = 2 if (num == 2): self.troops = globals.map.player2Available self.enemy = 1 helper = Help() parent = globals.map best_score = 99999999999 self.cost += 1 best_children = [] number = 0 children = [] visited = [] children.append(parent) while (1): children.remove(parent) current_children = helper.make_children(num, parent) for mapA in current_children: for mapB in visited: if (mapA.control == mapB.control and mapA.troopNum == mapB.troopNum): current_children.remove(mapA) break if (current_children.__len__() != 0): visited = visited + current_children.copy() children = children + current_children.copy() if children.__len__() != 0: best = helper.best_child(children) best_children.append(helper.deep_copy(best)) score = helper.calc_heurestic(best) + self.cost else: break best_score = score parent = best if (helper.calc_heurestic(parent) <= -50 or number >= 99): break number += 1 parent = helper.best_child(best_children) if (parent != None): globals.map.troopNum = parent.troopNum.copy() globals.map.adjacency = parent.adjacency.copy() globals.map.control = parent.control.copy() globals.map.player1Available = parent.player1Available globals.map.player2Available = parent.player2Available