def applyHeuristic(self, game: Game): self.stepstoWin += 1 q = PriorityQueue() bonusSoldiers = game.bonusSoldiers(self.isRed) cityListId = game.citiesOf(self.isRed) game = self.bonusArmyPlacing(bonusSoldiers, game, self.isRed) randomId = random.choice(cityListId) canAttack = False for cityId in cityListId: for neighbourId in game.map.graph[cityId]: neighbour = game.cityList[neighbourId] city = game.cityList[cityId] if (city.armyCount > neighbour.armyCount + 1 and city.isRedArmy != neighbour.isRedArmy): canAttack = True q.put((self.GreedyHeurictic(city, neighbour, game), city, neighbour)) if (canAttack == False): return game if (q.not_empty): next_item = q.get() fromCity = next_item[1] toCity = next_item[2] print(fromCity) print(toCity) self.attack(fromCity.id, toCity.id, fromCity.armyCount - 1, game) self.evaluate() return game
def applyHeuristic(self, game: Game) -> Game: cityListId = game.citiesOf(self.isRedPlayer) bonusArmy = game.bonusSoldiers(self.isRedPlayer) minimumTerritor = self.calculateMinimumTerritory(cityListId, game) if minimumTerritor != None: game.addSoldiersToCity(minimumTerritor.id, bonusArmy) return game
def heuristicFromCity(self, maxIsRed, myCityId: int, game: Game): result = 0 myCity = game.cityList[myCityId] if (myCity.isRedArmy == maxIsRed): bonusSoldiers = game.bonusSoldiers(not maxIsRed) maxCityId = myCityId for minCityId in game.map.graph[maxCityId]: if (game.cityList[minCityId].isRedArmy != game.cityList[maxCityId].isRedArmy): if (game.canAttack2(maxCityId, 0, minCityId, 0)): result += 1 if (game.canAttack2(maxCityId, 0, minCityId, bonusSoldiers)): result += 2 if (not game.canAttack2(minCityId, 0, maxCityId, 0)): result += 3 if (not game.canAttack2(minCityId, bonusSoldiers, maxCityId, 0)): result += 4 else: bonusSoldiers = game.bonusSoldiers(maxIsRed) minCityId = myCityId for maxCityId in game.map.graph[minCityId]: if (game.cityList[minCityId].isRedArmy != game.cityList[maxCityId].isRedArmy): if (game.canAttack2(maxCityId, bonusSoldiers, minCityId, 0)): result += 1 if (game.canAttack2(maxCityId, 0, minCityId, 0)): result += 2 if (not game.canAttack2(minCityId, 0, maxCityId, bonusSoldiers)): result += 3 if (not game.canAttack2(minCityId, 0, maxCityId, 0)): result += 4 return result
def applyHeuristic(self, game: Game) -> Game: """ the actions taken are: 1. place all bonus army in my city with minimum number of soldiers 2. attack enemy city with minimum number of soldiers that can be attacked """ myCities = game.citiesOf(self.isRedPlayer) myMinArmyCityId = self.minArmyCityId(game, myCities) game.placeBonusSoldiers(myMinArmyCityId, game.bonusSoldiers(self.isRedPlayer)) bestUId, bestVId = self.hisMinArmyCityToAttack(game, myCities) if bestUId != -1 and bestVId != -1: game.move(bestUId, bestVId, game.cityList[bestVId].armyCount + 1) return game
def applyHeuristic(self, game: Game) -> Game: cityListId = game.citiesOf(self.isRedPlayer) bonusArmy = game.bonusSoldiers(self.isRedPlayer) maxCity = self.calculateMaxCity(cityListId, game) if maxCity != None: game.addSoldiersToCity(maxCity.id, bonusArmy) # attack all neighbour cities with most armies for cityId in cityListId: for neighbourId in game.map.graph[cityId]: neighbour = game.cityList[neighbourId] city = game.cityList[cityId] if neighbour.armyCount < city.armyCount - 1 and neighbour.isRedArmy != city.isRedArmy: game.move(city.id, neighbour.id, city.armyCount - 1) return game
def adjacentStates(self, game: Game): bonusArmyPossibilities = list() cityListId = game.citiesOf(self.isRedPlayer) bonusArmy = game.bonusSoldiers(self.isRedPlayer) for cityId in cityListId: gameCopy = deepcopy(game) gameCopy.addSoldiersToCity(cityId, bonusArmy) bonusArmyPossibilities.append(gameCopy) attackingPossibilities = list() for gameStatee in bonusArmyPossibilities: cityListId = gameStatee.citiesOf(self.isRedPlayer) for cityId in cityListId: city = gameStatee.cityList[cityId] for neighbourId in gameStatee.map.graph[cityId]: neighbour = gameStatee.cityList[neighbourId] if city.armyCount > neighbour.armyCount + 1 and city.isRedArmy != neighbour.isRedArmy: gameCopy = deepcopy(gameStatee) gameCopy.move(cityId, neighbourId, neighbour.armyCount + 1) attackingPossibilities.append(gameCopy) return attackingPossibilities
def __mySecuredCityWeightCalculator(self, isRedPlayer: bool, game: Game, myCityId: int, enemyCityId: int): hisBonus = game.bonusSoldiers(not isRedPlayer) return MY_STRONG_CITY_WEIGHT if (game.cityList[myCityId].armyCount >= game.cityList[enemyCityId].armyCount + hisBonus) else MY_WEAK_CITY_WEIGHT