def getAllFen(self): stFENm2 = set() lilipv = [LCEngine.xpv2pv(xpv).split(" ") for xpv in self.li_xpv] for lipv in lilipv: LCEngine.setFenInicial() for pv in lipv: fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) stFENm2.add(fenM2) LCEngine.makeMove(pv) return stFENm2
def wd_move(self, fen, move): LCEngine.setFen(fen) liMoves = LCEngine.getMoves() liMoves = map(lambda x: x[1:], liMoves) if move in liMoves: LCEngine.movePV(move[:2], move[2:4], move[4:]) xfen = LCEngine.getFen() wdl, dtz = self.wdl_dtz(xfen) else: wdl, dtz = 2, 0 return -wdl, -dtz
def checkFen(self, fen): LCEngine.setFen(fen) liMoves = LCEngine.getMoves() dic = {} for xpv in liMoves: pv = xpv[1:] LCEngine.setFen(fen) LCEngine.movePV(pv[:2], pv[2:4], pv[4:]) xfen = LCEngine.getFen() wdl, dtz = self.wdl_dtz(xfen) if wdl is not None and dtz is not None: dic[pv] = -wdl, -dtz return dic
def recalcFenM2(self): lilipv = [LCEngine.xpv2pv(xpv).split(" ") for xpv in self.li_xpv] dicFENm2 = {} for lipv in lilipv: LCEngine.setFenInicial() for pv in lipv: fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) if fenM2 not in dicFENm2: dicFENm2[fenM2] = set() dicFENm2[fenM2].add(pv) LCEngine.makeMove(pv) return dicFENm2
def totree(self): parent = ItemTree(None, None, None, None) dic = AperturasStd.ap.dicFenM2 for xpv in self.li_xpv: lipv = LCEngine.xpv2pv(xpv).split(" ") lipgn = LCEngine.xpv2pgn(xpv).replace("\n", " ").strip().split(" ") linom = [] LCEngine.setFenInicial() for pv in lipv: LCEngine.makeMove(pv) fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) linom.append(dic[fenM2].trNombre if fenM2 in dic else "") parent.addLista(lipv, lipgn, linom) return parent
def baseXPV(self, xpv): lipv = LCEngine.xpv2pv(xpv).split(" ") last_ap = None LCEngine.setFenInicial() mx = self.max_ply + 3 for n, pv in enumerate(lipv): if n > mx: break LCEngine.makeMove(pv) fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) if fenM2 in self.dicFenM2: last_ap = self.dicFenM2[fenM2] return last_ap
def lee_subnivel(self, fila): alm_base = self.li_moves[fila] if alm_base.nivel >= 17: return LCEngine.setFen(alm_base.fen) if LCEngine.movePV(alm_base.desde, alm_base.hasta, alm_base.coronacion): fen = LCEngine.getFen() for alm in self.book.almListaJugadas(fen): nv = alm.nivel = alm_base.nivel + 1 alm.dato = [""] * 20 alm.dato[nv] = alm.pgn alm.dato[nv+1] = alm.porc alm.dato[nv+2] = "%d" % alm.weight fila += 1 self.li_moves.insert(fila, alm)
def mover(self, desdeA1H8, hastaA1H8, coronacion=""): self.setLCE() mv = LCEngine.moveExPV(desdeA1H8, hastaA1H8, coronacion) if not mv: return False, "Error" self.liExtras = [] enrK = mv.isCastleK() enrQ = mv.isCastleQ() enPa = mv.isEnPassant() if coronacion: if self.siBlancas: coronacion = coronacion.upper() else: coronacion = coronacion.lower() self.liExtras.append(("c", hastaA1H8, coronacion)) elif enrK: if self.siBlancas: self.liExtras.append(("m", "h1", "f1")) else: self.liExtras.append(("m", "h8", "f8")) elif enrQ: if self.siBlancas: self.liExtras.append(("m", "a1", "d1")) else: self.liExtras.append(("m", "a8", "d8")) elif enPa: capt = self.alPaso.replace("6", "5").replace("3", "4") self.liExtras.append(("b", capt)) self.leeFen(LCEngine.getFen()) # despues de liExtras, por si enpassant return True, self.liExtras
def read(self, fen, txt): ntxt = len(txt) pos = 0 mv = Move() mv.pgn = "" while pos < ntxt: c = txt[pos] if c in "123456789": pos += 1 while pos < ntxt and txt[pos] in "1234567890.": pos += 1 elif c in "abcdfghKQRBNOo": desde = pos hasta = pos pos += 1 while pos < ntxt and txt[ pos] in "abcdefghKQRBN12345678xX-Oo=p+": hasta += 1 pos += 1 mv = Move() x = mv.pgn = txt[desde:hasta + 1] if "-" in x: if "o" in x: x = x.replace("o", "O") elif x[0] != "O": x = x.replace("-", "") mv.pgn = x self.liMoves.append(mv) elif c == "e": desde = pos hasta = pos pos += 1 while pos < ntxt and txt[pos] in "abcdefghKQRBN12345678xX-=p.": hasta += 1 pos += 1 mv = Move() x = mv.pgn = txt[desde:hasta + 1] if "-" in x: x = x.replace("-", "") mv.pgn = x if x.endswith("e.p."): x = x[:-4] mv.pgn = x if x and not (x in ("ep", "e.p.", "e.p", "ep.")): self.liMoves.append(mv) elif c == "$": pos += 1 desde = pos hasta = pos while pos < ntxt and txt[pos].isdigit(): hasta += 1 pos += 1 mv.criticas.append(txt[desde:hasta]) elif c in "?!": desde = pos hasta = pos pos += 1 while pos < ntxt and txt[pos] in "!?": hasta += 1 pos += 1 mv.criticas.append(txt[desde:hasta + 1]) elif c == "(": pos += 1 desde = pos hasta = pos par = 1 coment = 0 while pos < ntxt: c = txt[pos] if coment: if c == "{": coment += 1 elif c == "}": coment -= 1 else: if c == "(": par += 1 elif c == ")": par -= 1 if par == 0: break elif c == "{": coment = 1 hasta += 1 pos += 1 mv.variantes.append(txt[desde:hasta].replace( "\r\n", " ").replace("\r", " ").replace("\n", " ").strip()) elif c == "{": pos += 1 desde = pos hasta = pos par = 1 while pos < ntxt: c = txt[pos] if c == "{": par += 1 elif c == "}": par -= 1 if par == 0: break hasta += 1 pos += 1 comment = txt[desde:hasta].replace("\r\n", " ").replace( "\r", " ").replace("\n", " ").strip() if not mv.pgn: self.firstComment = comment else: mv.comentarios.append(comment) elif c == ";": pos += 1 while pos < ntxt and txt[pos] != "\n": pos += 1 elif c == "#": mv.siMate = True pos += 1 elif c == "*": mv.siDesconocido = True pos += 1 elif c == "0": desde = pos hasta = pos pos += 1 while pos < ntxt and txt[pos] in "0Oo-": hasta += 1 pos += 1 mv = Move() x = mv.pgn = txt[desde:hasta + 1].replace("0", "O").upper() if len(x) in (3, 5): self.liMoves.append(mv) else: pos += 1 LCEngine.setFen(fen) fenPrev = fen for mv in self.liMoves: mv.fenPrev = fenPrev pv = LCEngine.lc_pgn2pv(mv.pgn) if len(pv) < 4: return False mv.pv = pv mv.desde = pv[:2] mv.hasta = pv[2:4] mv.coronacion = pv[4:] if not LCEngine.movePV(mv.desde, mv.hasta, mv.coronacion): return False fenPrev = LCEngine.getFen() mv.fen = fenPrev # Se hace separado para que no influya for mv in self.liMoves: if mv.variantes: livar = [] for variante in mv.variantes: moves = Moves() if moves.read(mv.fenPrev, variante): livar.append(moves) mv.variantes = livar return True
def preparaTraining(self, reg): maxmoves = reg["MAXMOVES"] siBlancas = reg["COLOR"] == "WHITE" siRandom = reg["RANDOM"] lilipv = [LCEngine.xpv2pv(xpv).split(" ") for xpv in self.li_xpv] if maxmoves: for pos, lipv in enumerate(lilipv): if len(lipv) > maxmoves: lilipv[pos] = lipv[:maxmoves] # Ultimo el usuario for pos, lipv in enumerate(lilipv): if len(lipv) % 2 == (0 if siBlancas else 1): lilipv[pos] = lipv[:-1] # Duplicados stBorrar = set() lista = [(pos, "".join(lipv)) for pos, lipv in enumerate(lilipv)] lista.sort(key=lambda elem: elem[1]) for pos, (posli, pv) in enumerate(lista): for pos1 in range(pos + 1, len(lista)): if lista[pos1][1].startswith(pv): stBorrar.add(posli) break lilipv = [ lipv for pos, lipv in enumerate(lilipv) if pos not in stBorrar ] if siRandom: random.shuffle(lilipv) ligamesST = [] ligamesSQ = [] dicFENm2 = {} for lipv in lilipv: game = {} game["LIPV"] = lipv game["NOERROR"] = 0 game["TRIES"] = [] ligamesST.append(game) game = dict(game) ligamesSQ.append(game) LCEngine.setFenInicial() for pv in lipv: fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) if fenM2 not in dicFENm2: dicFENm2[fenM2] = set() dicFENm2[fenM2].add(pv) LCEngine.makeMove(pv) reg["LIGAMES_STATIC"] = ligamesST if reg["RANDOM"]: random.shuffle(ligamesSQ) reg["LIGAMES_SEQUENTIAL"] = ligamesSQ reg["DICFENM2"] = dicFENm2 bcolor = " w " if siBlancas else " b " liTrainPositions = [] for fenM2 in dicFENm2: if bcolor in fenM2: data = {} data["FENM2"] = fenM2 data["MOVES"] = dicFENm2[fenM2] data["NOERROR"] = 0 data["TRIES"] = [] liTrainPositions.append(data) random.shuffle(liTrainPositions) reg["LITRAINPOSITIONS"] = liTrainPositions
def preparaTraining(self, reg, procesador): maxmoves = reg["MAXMOVES"] siBlancas = reg["COLOR"] == "WHITE" siRandom = reg["RANDOM"] siRepetir = False lilipv = [LCEngine.xpv2pv(xpv).split(" ") for xpv in self.li_xpv] if maxmoves: for pos, lipv in enumerate(lilipv): if len(lipv) > maxmoves: lilipv[pos] = lipv[:maxmoves] # Ultimo el usuario for pos, lipv in enumerate(lilipv): if len(lipv) % 2 == (0 if siBlancas else 1): lilipv[pos] = lipv[:-1] # Quitamos las repetidas lilipvfinal = [] nt = len(lilipv) for x in range(nt - 1): pvmirar = "".join(lilipv[x]) esta = False for y in range(x + 1, nt): pvotro = "".join(lilipv[y]) if pvotro.startswith(pvmirar): esta = True break if not esta: lilipvfinal.append(lilipv[x]) lilipv = lilipvfinal ligamesST = [] ligamesSQ = [] dicFENm2 = {} for lipv in lilipv: game = {} game["LIPV"] = lipv game["NOERROR"] = 0 game["TRIES"] = [] ligamesST.append(game) game = dict(game) ligamesSQ.append(game) LCEngine.setFenInicial() for pv in lipv: fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) if fenM2 not in dicFENm2: dicFENm2[fenM2] = set() dicFENm2[fenM2].add(pv) LCEngine.makeMove(pv) if not siRepetir: stBorrar = set() xanalyzer = procesador.XAnalyzer() for stpv, fenM2 in dicFENm2.iteritems(): if len(stpv) > 1: siW = " w " in fenM2 if siW and siBlancas: dic = self.getfenvalue(fenM2) if "ANALISIS" not in dic: dic["ANALISIS"] = xanalyzer.analiza(fen) self.setfenvalue(fenM2, dic) mrm = dic["ANALISIS"] pvsel = stpv[0] # el primero que encuentre por defecto for rm in mrm.liMultiPV(): pv0 = rm.movimiento() if pv0 in stpv: pvsel = pv0 stpv.remove(pvsel) break dicFENm2[fenM2] = {pvsel} for pv in stpv: stBorrar.add("%s|%s" % (fenM2, pv)) liBorrar = [] for n, game in enumerate(ligamesSQ): LCEngine.setFenInicial() for pv in game["LIPV"]: fen = LCEngine.getFen() fenM2 = LCEngine.fen2fenM2(fen) key = "%s|%s" % (fenM2, pv) if key in stBorrar: liBorrar.append(n) break LCEngine.makeMove(pv) liBorrar.sort(reverse=True) for n in liBorrar: del ligamesSQ[n] del ligamesST[n] if siRandom: random.shuffle(ligamesSQ) random.shuffle(ligamesST) reg["LIGAMES_STATIC"] = ligamesST reg["LIGAMES_SEQUENTIAL"] = ligamesSQ reg["DICFENM2"] = dicFENm2 bcolor = " w " if siBlancas else " b " liTrainPositions = [] for fenM2 in dicFENm2: if bcolor in fenM2: data = {} data["FENM2"] = fenM2 data["MOVES"] = dicFENm2[fenM2] data["NOERROR"] = 0 data["TRIES"] = [] liTrainPositions.append(data) random.shuffle(liTrainPositions) reg["LITRAINPOSITIONS"] = liTrainPositions