def legalMoves(self): map_ = self.map["map"] player = "xPlayer" if self.getStat("xPlayer", "ACT") >= 2 else "oPlayer" other = self.getStat("oPlayer" if player == "xPlayer" else "xPlayer", "token") token = self.getStat(player, "token") options = [] forced = None backup = None for x in range(map_.x): y = udebs_config.BOTTOM(map_, x) if y is not None: loc = (x, y) if udebs_config.win(map_, other, loc) >= self.win_cond: if forced is not None: yield 1 return forced = loc if y > 0: if udebs_config.win(map_, other, (x, y - 1)) >= self.win_cond: # We cannot play here unless it is our only option backup = 1 if forced == loc: yield 1 return continue # finally these are our only good options if forced is None: options.append(loc) if forced: yield player, forced, "drop" elif len(options) > 0: const = (map_.x - 1) / 2 huristic = lambda x: abs(const - x[0]) for loc in sorted(options, key=huristic): yield player, loc, "drop" else: yield backup if backup else 0
def result(self, alpha=None, beta=None): map_ = self.getMap().copy() map_.playerx = self.getStat("xPlayer", "ACT") >= 2 map_.scored = len(map_) - self.time map_.const = (map_.x - 1) / 2 result = None if self.value is not None: result = -(map_.scored + 1) // 2 else: # We have to check if we are one turn away from victory token = "x" if map_.playerx else "o" for x in range(map_.x): y = udebs_config.BOTTOM(map_, x) if y is not None: if udebs_config.win(map_, token, (x, y)) >= self.win_cond: result = (map_.scored + 1) // 2 break if result: if alpha and result < alpha: return alpha elif beta and result > beta: return beta return result if not beta: beta = (map_.scored - 1) // 2 if not alpha: alpha = -beta return self.negamax(alpha, beta, map_)
def legalMoves(self, map_): yield map_.scored // 2 token, other = ("x", "o") if map_.playerx else ("o", "x") options = [] forced = False for x in range(map_.x): for y in range(map_.y - 1, -1, -1): if map_[x, y] == "empty": break else: continue loc = (x, y) if udebs_config.win(map_, other, loc) >= self.win_cond: if forced: return options = [] forced = loc if not forced or forced == loc: if y > 0 and udebs_config.win(map_, other, (x, y - 1)) >= self.win_cond: # We cannot play here unless it is our only option if forced: return else: options.append(loc) # Lower bound optimization magic if len(options) > 0 and not forced: if map_.scored >= 2: yield (map_.scored - 2) // 2 # end magic if len(options) > 1: huristic = lambda x: (-self.win_huristic(map_, token, x), abs(map_.const - x[0])) options = sorted(options, key=huristic) for loc in options: yield token, loc
def legalMoves(self, map_): token, other = ("x", "o") if map_.playerx else ("o", "x") options = [] forced = None backup = None for x in range(map_.x): for y in range(map_.y -1, -1, -1): if map_[x,y] == "empty": break else: continue loc = (x, y) if udebs_config.win(map_, other, loc) >= self.win_cond: if forced is not None: yield 1 return forced = loc if y > 0: if udebs_config.win(map_, other, (x, y - 1)) >= self.win_cond: # We cannot play here unless it is our only option backup = 1 if forced == loc: yield 1 return continue # finally these are our only good options if forced is None: options.append(loc) if forced: yield token, forced elif len(options) > 0: huristic = lambda x: abs(map_.const - x[0]) for loc in sorted(options, key=huristic): yield token, loc else: yield backup if backup else 0
def result(self, alpha=-1, beta=1): if self.value is not None: return -abs(self.value) map_ = self.getMap() player = "xPlayer" if self.getStat("xPlayer", "ACT") >= 2 else "oPlayer" token = "x" if player == "xPlayer" else "o" for x in range(map_.x): y = udebs_config.BOTTOM(map_, x) if y is not None: if udebs_config.win(map_, token, (x, y)) >= self.win_cond: return 1 return self.negamax(alpha, beta)
def result(self, alpha=-1, beta=1): if self.value is not None: return -abs(self.value) map_ = self.getMap().copy() map_.playerx = self.getStat("xPlayer", "ACT") >= 2 map_.const = (map_.x - 1) / 2 token = "x" if map_.playerx else "o" for x in range(map_.x): y = udebs_config.BOTTOM(map_, x) if y is not None: if udebs_config.win(map_, token, (x,y)) >= self.win_cond: return 1 return self.negamax(alpha, beta, map_)