def get_left_win_prob(cur_state: State): if not cur_state.team_left.is_alive(): return 0 elif not cur_state.team_right.is_alive(): return 1 else: state_left_win = cur_state.__copy__() state_right_win = cur_state.__copy__() rules_left_win = Rules(state_left_win, mode) rules_right_win = Rules(state_right_win, mode) left_glad = cur_state.team_left[0] right_glad = cur_state.team_right[0] prob_glad_win = rules_left_win \ .get_probability_glad_win(left_glad, right_glad) rules_left_win.fight(state_left_win.team_left[0], state_left_win.team_right[0], mode='test', winner=Party.LEFT) rules_right_win.fight(state_right_win.team_left[0], state_right_win.team_right[0], mode='test', winner=Party.RIGHT) return prob_glad_win[0] * get_left_win_prob(rules_left_win.state) \ + prob_glad_win[1] * get_left_win_prob(rules_right_win.state)
def prepare(self, mainChar: Character, otherChars: list[Character], state: State = None) -> bool: """ Prepares this Event to be triggered, assigning Characters to the Event State if they match. If any of the requirements aren't met, returns False, otherwise returns True. """ self.state = State(self.triggerCts, self.baseChance) if not state else state.sub( self.triggerCts, self.baseChance) if not self.state.doesCharExist(mainChar): return self.prepareBase(mainChar, otherChars) else: return self.prepareSub(otherChars)
def check(self, char: Character, state: State): target = state.getChar(self.targetShort) if self.relationship == RelationCheck.ALLY and char.isAllyOf(target): return True if self.relationship == RelationCheck.ENEMY and not char.isAllyOf(target): return True return False
def work(self): # initial state state = State() draw_state(state, State.board_size) print("") # start game while not state.has_finished(): counter_to_move = ask_to_choose_counter(state, state.get_turn(), self) new_state = ask_to_move(state, state.get_turn(), counter_to_move, self) print("") print("New State:\n") draw_state(new_state, State.board_size)
def __init__(self, left_player: Bot, right_player: Bot, mode: Mode = Mode.DEFAULT): self.left_player = left_player self.right_player = right_player self.state = State(left_player.team, right_player.team) if mode not in {Mode.DEFAULT, Mode.HILLMAN}: print('Wrong mode, install default mode') mode = Mode.DEFAULT self.rules = Rules(self.state, mode)
def __init__(self, name: str, texts: list[str], checkNamesToArgLists: dict[str, list[list[str]]], effectNamesToArgLists: dict[str, list[list[str]]], sub: list[Event]): self.name = name self.texts = texts self.checkSuites = [ CheckSuite(checkName, checkNamesToArgLists[checkName]) for checkName in checkNamesToArgLists ] self.effectSuites = [ EffectSuite(effectName, effectNamesToArgLists[effectName]) for effectName in effectNamesToArgLists ] self.sub = sub self.triggerCts: dict[Character, int] = {} self.state = State(self.triggerCts) self.baseChance = 100 if self.name.endswith("default"): self.baseChance = 0
def perform(self, char: Character, state: State): toAlly = state.getChar(self.charShort) if char.isAlone() and toAlly.isAlone(): alliance = [] char.joinAlliance(alliance) toAlly.joinAlliance(alliance) return f"allied with: {toAlly}" elif not char.isAlone(): alliance = char.getAlliance() toAlly.leaveAlliance() toAlly.joinAlliance(alliance) return f"alliance joined by: {toAlly}" elif not toAlly.isAlone(): alliance = toAlly.getAlliance() char.leaveAlliance() char.joinAlliance(alliance) return f"joined alliance of: {toAlly}"
def get_win_probability(left_powers: list, right_powers: list, mode: Mode): """ This function gets probability of win, if both players should choose first alive gladiators """ def get_left_win_prob(cur_state: State): if not cur_state.team_left.is_alive(): return 0 elif not cur_state.team_right.is_alive(): return 1 else: state_left_win = cur_state.__copy__() state_right_win = cur_state.__copy__() rules_left_win = Rules(state_left_win, mode) rules_right_win = Rules(state_right_win, mode) left_glad = cur_state.team_left[0] right_glad = cur_state.team_right[0] prob_glad_win = rules_left_win \ .get_probability_glad_win(left_glad, right_glad) rules_left_win.fight(state_left_win.team_left[0], state_left_win.team_right[0], mode='test', winner=Party.LEFT) rules_right_win.fight(state_right_win.team_left[0], state_right_win.team_right[0], mode='test', winner=Party.RIGHT) return prob_glad_win[0] * get_left_win_prob(rules_left_win.state) \ + prob_glad_win[1] * get_left_win_prob(rules_right_win.state) left_team = Team(powers=left_powers) right_team = Team(powers=right_powers) prob_left_win = get_left_win_prob(State(left_team, right_team)) return prob_left_win, 1 - prob_left_win
class Event: def __init__(self, name: str, texts: list[str], checkNamesToArgLists: dict[str, list[list[str]]], effectNamesToArgLists: dict[str, list[list[str]]], sub: list[Event]): self.name = name self.texts = texts self.checkSuites = [ CheckSuite(checkName, checkNamesToArgLists[checkName]) for checkName in checkNamesToArgLists ] self.effectSuites = [ EffectSuite(effectName, effectNamesToArgLists[effectName]) for effectName in effectNamesToArgLists ] self.sub = sub self.triggerCts: dict[Character, int] = {} self.state = State(self.triggerCts) self.baseChance = 100 if self.name.endswith("default"): self.baseChance = 0 def __repr__(self): return f"Event \"{self.name}\"" def exception(self, text): return Exception(f"In event {self.name}: ", text) def getName(self): return self.name def getChance(self): if not self.state: return self.baseChance return self.state.chance def load(self, valids: Valids, isSub: bool = False): try: print(self.name) if self.checkSuites: mainCheckSuite = self.checkSuites[0] mainCheckSuite.load(valids, isSub) for checkSuite in self.checkSuites[1:]: checkSuite.load(valids, isSub) checkSuite.addNearbyCheckIfNeeded(valids) for effectSuite in self.effectSuites: effectSuite.load(valids) for subEvent in self.sub: subEvent.load(valids, True) for text in self.texts: valids.validateText(text) except ValidationException as e: raise Exception( f"Encountered an exception when loading Event \"{self.name}\": {e}" ) def prepare(self, mainChar: Character, otherChars: list[Character], state: State = None) -> bool: """ Prepares this Event to be triggered, assigning Characters to the Event State if they match. If any of the requirements aren't met, returns False, otherwise returns True. """ self.state = State(self.triggerCts, self.baseChance) if not state else state.sub( self.triggerCts, self.baseChance) if not self.state.doesCharExist(mainChar): return self.prepareBase(mainChar, otherChars) else: return self.prepareSub(otherChars) def prepareBase(self, mainChar: Character, otherChars: list[Character]): """ Prepares a base-level event. """ # Main Character's requirements are always the first in the list of Suites mainCheckSuite = self.checkSuites[0] # Check the rest of the main's requirements if not mainCheckSuite.checkAll(mainChar, self.state): return False # If the main character matches, we put them into the State self.state.setChar(mainCheckSuite.getCharShort(), mainChar) # All other requirement suites match other characters from the given list of all other Characters for reqSuite in self.checkSuites[1:]: matchedChar = self.matchCharacter(reqSuite, otherChars) if not matchedChar: return False self.state.setChar(reqSuite.getCharShort(), matchedChar) return True def prepareSub(self, otherChars: list[Character]): """ Prepares a sub-event. """ # Sub-events can have empty requirements if not self.checkSuites: return True for checkSuite in self.checkSuites: # There might already be a character in the Event State short = checkSuite.getCharShort() matchedChar = self.state.getChar(short) # If that's not the case, we want to match a new Character like normal if not matchedChar: matchedChar = self.matchCharacter(checkSuite, otherChars) if not matchedChar: return False self.state.setChar(checkSuite.getCharShort(), matchedChar) continue # If that is the case, we want to check the preexisting Character against the new requirements if not checkSuite.checkAll(matchedChar, self.state): return False return True def matchCharacter(self, checkSuite: CheckSuite, otherChars: dict[str, Character]): # Collect a list of all matched Characters matchedChars: list[Character] = [] for char in otherChars.values(): # Can't match the same Character twice if self.state.doesCharExist(char): continue # Full Suite check, adding Character if it matches if checkSuite.checkAll(char, self.state): matchedChars.append(char) if not matchedChars: return False # Assign a random Character from the matched Characters to the State return choice(matchedChars) def incrementTriggers(self, char: Character): """ Increments the number of times this Event has been triggered by a certain Character. """ # Count the number of triggers that have happened to the main Character if not char in self.triggerCts: self.triggerCts[char] = 0 self.triggerCts[char] += 1 def trigger(self, result: Result): """ Performs the Event's effects on the Characters given to the State. """ mc = self.state.getChar() self.incrementTriggers(mc) result.addText(choice(self.texts), self.state) # Do each Suite's actions to the State's Characters for effectSuite in self.effectSuites: char = self.state.getChar(effectSuite.getCharShort()) for effectText in effectSuite.performAll(char, self.state): result.addEffect(char, effectText) return self.state, self.sub
def move(self, state: State): return choice(state.get_team(self.party))
def perform(self, char: Character, state: State): item = state.getItem(self.itemShort) char.copyAndGiveItem(item) return f"gave item: {item}"
def perform(self, char: Character, state: State): item = state.getItem(self.itemShort) char.takeItem(item) return f"consumed item: {item}"
def check(self, char: Character, state: State) -> bool: item = choice(self.items) state.setItem(self.itemShort, item) return True
def check(self, char: Character, state: State) -> bool: item = self.get(char) if not item: return False state.setItem(self.itemShort, item) return True
def check(self, char: Character, state: State) -> bool: if self.cType == LimitCheck.PERCHAR: return state.getTriggersFor(char) <= self.count else: return state.getTotalTriggers() <= self.count
def check(self, char: Character, state: State) -> bool: if self.state == DistanceCheck.NEARBY: target = state.getChar(self.targetShort) return char.isNearby(target) else: return True
def check(self, char: Character, state: State) -> bool: state.addChances(self.number) return True
def check(self, char: Character, state: State) -> bool: if not self.trove.hasItems(): return False state.setItem(self.newItemShort, self.trove.loot()) return True