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 __onConflictingPairs(self, isRedPlayer: bool, game: Game, calculate) -> int: result: int = 0 myCities: [int] = game.citiesOf(isRedPlayer) for myCityId in myCities: for adjCityId in game.map.graph[myCityId]: if game.cityList[adjCityId].isRedArmy != isRedPlayer: enemyCityId: int = adjCityId result += calculate(isRedPlayer, game, myCityId, enemyCityId) 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