def declareAttack(self, ownedCountries, allCountries): currentState = self.getCurrentState(ownedCountries) #on s'interesse seulement au nombre de troop dans les pays qui peuvent #s'attaquer currentStateQ = tuple([self.getArmyDiff(zone[0].getNbTroops(), zone[1].getNbTroops()) for zone in currentState]) possibleActions = range(0, 2**len(currentState)) #Pour chaque frontiere, on peut attaquer ou ne rien faire, il y a donc 2**n choix possibles #on peut encode un action dans un nombre binaire, ou chaque bit represente une frontiere #si le bit est a 1, on attaque, sinon on ne fait rien currentAction = None attackActionList = [] if possibleActions: currentAction = glie(self, currentStateQ, 1.1, 100, possibleActions) actions = [x == '1' for x in '{0:b}'.format(currentAction)] attackIndex = 0 for attack in actions: if attack: attackActionList.append(AttackAction(currentState[attackIndex][0], currentState[attackIndex][1], 3)) attackIndex += 1 self.lastState = currentStateQ self.lastAction = currentAction self.lastScore = self.getScore(ownedCountries, allCountries) if len(attackActionList) == 0: self.onAttackResult(-0.5, ownedCountries) return attackActionList
def chooseStartingCountry(self, remainingCountry, ownedCountries, allCountries): rem = ([r.getName() for r in remainingCountry]) rem.sort() own = [o.getName() for o in ownedCountries] own.sort() currentState = (tuple(rem), tuple(own)) if(self.lastState and self.lastAction): self.appendLastIteration(currentState) currentAction = glie(self, currentState, 1.1, 10, currentState[0]) self.lastState = currentState self.lastAction = currentAction return next(country for country in remainingCountry if country.getName() is currentAction)
def moveTroop(self, turnAttackResults, ownedCountries, allCountries): currentState = self.getCurrentState(ownedCountries) possibleActions = self.getPossibleActions(ownedCountries.values()) currentAction = None if possibleActions: currentAction = glie(self, currentState, 1.1, 100, possibleActions) self.lastState = currentState self.lastAction = currentAction self.lastScore = self.getScore(ownedCountries) if currentAction: startCountry = ownedCountries[currentAction[0]] endCountry = ownedCountries[currentAction[1]] return MoveAction(startCountry, endCountry, max(1, startCountry.getNbTroops()/2))
def placeTroops(self, nbTroopsToPlace, ownedCountries, allCountries): currentState = self.getCurrentState(ownedCountries) #action possible: ajouter 1 armée sur un pay possibleActions = range(0, len(ownedCountries)) currentAction = glie(self, currentState, 1.1, 100, possibleActions) self.lastState = currentState self.lastAction = currentAction self.lastScore = self.getScore(ownedCountries) chosenCountry = currentState[currentAction][0] placeTroopAction = [] placeTroopAction.append(PlaceTroopsAction(chosenCountry, nbTroopsToPlace)) return placeTroopAction
def placeStartingTroop(self, nbTroopsToPlace, ownedCountries, allCountries): currentState = self.getCurrentState(ownedCountries) if(self.lastState and self.lastAction): self.appendLastIteration(currentState) #action possible: ajouter 1 armée sur un pay possibleActions = range(0, len(ownedCountries)) currentAction = glie(self, currentState, 1.1, 100, possibleActions) self.lastState = currentState self.lastAction = currentAction chosenCountry = currentState[currentAction][0] placeTroopAction = [] placeTroopAction.append(PlaceTroopsAction(chosenCountry, 1)) return placeTroopAction