def inicio(self, route): GestorRoutes.inicio(self, route) ending = self.route.get_ending() if "|" in ending: self.is_guided = True self.t4 = None self.fen, label, pv = ending.split("|") self.liPV = pv.split(" ") self.posPV = 0 else: self.is_guided = False self.t4 = LibChess.T4(self.configuracion) self.fen = ending + " - - 0 1" self.is_rival_thinking = False cp = Position.Position() cp.read_fen(self.fen) is_white = cp.is_white self.game.set_position(cp) self.game.pending_opening = False self.warnings = 0 self.max_warnings = 5 self.human_is_playing = False self.state = ST_PLAYING self.is_human_side_white = is_white self.is_engine_side_white = not is_white self.main_window.ponActivarTutor(False) self.quitaAyudas(True) self.ayudas_iniciales = 0 li_options = [TB_CLOSE, TB_HELP] self.main_window.pon_toolbar(li_options) self.main_window.activaJuego(True, False, siAyudas=False) self.main_window.quitaAyudas(True) self.set_dispatcher(self.mueve_humano) self.setposition(self.game.last_position) self.mostrarIndicador(True) self.ponPiezasAbajo(is_white) self.ponWarnings() self.pgnRefresh(True) QTUtil.refresh_gui() self.dgt_setposition() if self.is_guided: self.ponRotulo1("<b>%s</b>" % label) self.siguiente_jugada()
def scanner_learn(self): cp = Position.Position() cp.read_fen(self.tablero.fen_active()) tolerance = self.sb_scanner_tolerance.valor() tolerance_learn = min(self.sb_scanner_tolerance_learns.valor(), tolerance) self.n_scan_last_added = len(self.li_scan_pch) dic_deduced_extended = self.scanner_deduce_base(True) for pos, pz_real in cp.squares.items(): if pz_real: resp = dic_deduced_extended.get(pos) if resp is None: pz_deduced = None dt = 99 else: pz_deduced, reg_scan, dt = resp if (not pz_deduced) or (pz_real != pz_deduced) or dt > tolerance_learn: color_celda = self.dic_pos_color[pos] hs = self.dicscan_pos_hash[pos] key = (pz_real, color_celda, hs) self.li_scan_pch.append(key) if self.chb_rem_ghost_deductions.valor(): for pos_a1h8, (pz_deduced, reg_scan, dt) in dic_deduced_extended.items(): if cp.get_pz(pos_a1h8) is None: # ghost pz, color, hs = reg_scan for pos_li, (xpz, xcolor, xhs) in enumerate(self.li_scan_pch): if pz == xpz and color == xcolor and hs == xhs: del self.li_scan_pch[pos_li] break self.scanner_show_learned()
def siguiente_posicion(self): num_lineas_posicion = len(self.li_lineas_posicion) while True: random_pos = random.randint(0, num_lineas_posicion - 1) if not (random_pos in self.st_randoms): self.st_randoms.add(random_pos) break self.fen, self.result, self.pgn_result, self.pgn, self.difficult = ( self.li_lineas_posicion[random_pos].strip().split("|")) self.difficult = int(self.difficult) self.cp = Position.Position() self.cp.read_fen(self.fen) self.is_white = " w " in self.fen self.tablero.bloqueaRotacion(False) self.tablero.set_dispatcher(self.mueve_humano) self.tablero.setposition(self.cp) self.tablero.ponerPiezasAbajo(self.is_white) self.tablero.activaColor(self.is_white) self.tablero.ponIndicador(self.is_white) self.intentos = 0 self.max_intentos = (self.difficult + 1) // 2 + 4 self.iniTime = time.time()
def almListaJugadas(self, fen): li = self.book.lista(self.path, fen) position = Position.Position() position.read_fen(fen) total = 0 maxim = 0 for entry in li: w = entry.weight total += w if w > maxim: maxim = w listaJugadas = [] for entry in li: alm = Util.Record() pv = alm.pv = entry.pv() w = entry.weight alm.from_sq, alm.to_sq, alm.promotion = pv[:2], pv[2:4], pv[4:] alm.pgn = position.pgn_translated(alm.from_sq, alm.to_sq, alm.promotion) alm.pgnRaw = position.pgn(alm.from_sq, alm.to_sq, alm.promotion) alm.fen = fen alm.porc = "%0.02f%%" % (w * 100.0 / total, ) if total else "" alm.weight = w listaJugadas.append(alm) return listaJugadas
def posicionEditar(self): cp = Position.Position() cp.read_fen(self.fen) resp = Voyager.voyager_position(self, cp) if resp is not None: self.fen = resp self.muestraPosicion()
def __init__(self, line): self.line = line # Se usa en GestorEntPos self.position = Position.Position() self.label = "" self.game_obj = None self.game_original = None self.read(line)
def leeLibros(self, liLibros, fen, masTitulo, siPV): if not fen: return [] position = Position.Position() position.read_fen(fen) p = Polyglot() ico = "w" if position.is_white else "b" icoL = "l" liResp = [] for libro in liLibros: liResp.append((None, libro.name + masTitulo, icoL)) li = p.lista(libro.path, fen) if li: total = 0 for entry in li: total += entry.weight for entry in li: pv = entry.pv() w = entry.weight pc = w * 100.0 / total if total else "?" pgn = position.pgn_translated(pv[:2], pv[2:4], pv[4:]) liResp.append((pv if siPV else None, "%-5s -%7.02f%% -%7d" % (pgn, pc, w), ico)) else: liResp.append((None, _("No result"), "c")) return liResp
def set_fen(self, fen): if not fen or fen == FEN_INITIAL: return self.set_position(None) cp = Position.Position() cp.read_fen(fen) self.set_position(cp)
def check(self): self.bt_check.hide() cp = Position.Position() cp.read_fen(self.mate15.fen) first_move = self.mate15.move cp.mover(first_move[:2], first_move[2:4], first_move[4:]) fen = cp.fen() si_error = False for pos, (move, resp) in enumerate(self.mate15.resp.items()): cp.read_fen(fen) cp.mover(move[:2], move[2:4], move[4:]) wm = self.li_lb_wm[pos][1] mv_done = wm.movimiento() if mv_done == resp: wm.correcta() else: wm.error() si_error = True if si_error: self.bt_check.show() else: tiempo = time.time() - self.time_base self.lb_result.ponTexto("%s: %.1f\"" % (_("Time"), tiempo)) self.lb_result.show() self.mate15.append_try(tiempo) self.db_mate15.save(self.mate15) self.show_tb(self.terminar, self.seguir)
def posicionEditar(self): cp = Position.Position() cp.read_fen(self.fen) resp = Voyager.voyager_position(self, cp, wownerowner=self.procesador.main_window) if resp is not None: self.fen = resp.fen() self.muestraPosicion()
def setPosicion(self, game, njg, numTab=None): if game is None: return move = game.move(njg) self.game = game self.njg = njg next = self.cb_nextmove.valor() if move: if njg == 0: pv = game.pv_hasta(njg) if next else "" else: pv = game.pv_hasta(njg if next else njg - 1) position = move.position if next else move.position_before else: position = Position.Position().set_pos_initial() pv = "" for ntab, (tipo, tab) in enumerate(self.li_tabs): if ntab == 0: p = Game.Game() p.read_pv(pv) tab.setData(p.pgn_html(siFigurines=self.configuracion.x_pgn_withfigurines), position) else: data = pv if tipo == "dbase" else position if numTab is not None: if ntab != numTab: continue if ntab > 1: tab.setData(data) tab.start()
def __init__(self, moveOwner, fen, dbCache): self.moveOwner = moveOwner self.dbCache = dbCache if not moveOwner: self.nivel = 0 cp = Position.Position() cp.read_fen(fen) self.partidaBase = Game.Game(cp) else: self.nivel = self.moveOwner.listaMovesPadre.nivel + 1 self.partidaBase = self.moveOwner.game.copia() self.fenm2 = self.partidaBase.last_position.fenm2() dicCache = self.dbCache[self.fenm2] FasterCode.set_fen(self.fenm2 + " 0 1") liMov = [xpv[1:] for xpv in FasterCode.get_moves()] liMov.sort() liMoves = [] for pv in liMov: um = UnMove(self, pv, dicCache) liMoves.append(um) self.liMoves = liMoves self.liMovesInicial = liMoves[:] self.liAnalisis = dicCache.get("ANALISIS", []) if dicCache else [] # self.analisisActivo # self.dicAnalisis self.ponAnalisisActivo( dicCache.get("ANALISIS_ACTIVO", None) if dicCache else None)
def MoverInicio(self): if self.usoNormal: self.posHistoria = -1 position = Position.Position().set_pos_initial() else: # self.colocatePartida(-1) self.pos_move = -1 position = self.game.first_position self.tablero.setposition(position)
def factorDistancia(self): nummoves = 0 distancia = 0.0 for line in self.lines: d, n = line.distancia_moves() nummoves += n distancia += d base_dist = Position.distancia("a1", "a4") return (distancia / nummoves) / base_dist
def orden_fen(self, fen): posicionInicial = Position.Position() posicionInicial.read_fen(fen) self.siW = posicionInicial.is_white self.escribe("stop") game = Game.Game(fen=fen) self.engine.ac_inicio(game)
def __init__(self, owner, liFens, engine, segundos, fns): super(WDailyTest, self).__init__(owner, _("Your daily test"), Iconos.DailyTest(), "nivel") self.procesador = owner.procesador self.configuracion = self.procesador.configuracion if engine.startswith("*"): engine = engine[1:] confMotor = self.configuracion.buscaTutor(engine) self.xtutor = self.procesador.creaGestorMotor(confMotor, segundos * 1000, None) self.xtutor.maximizaMultiPV() self.historico = owner.historico # Tablero config_board = self.configuracion.config_board("NIVEL", 48) self.liFens = liFens self.nFens = len(self.liFens) self.juego = 0 self.liPuntos = [] self.liPV = [] self.li_tiempos = [] self.fns = fns self.tablero = Tablero.Tablero(self, config_board) self.tablero.crea() self.tablero.set_dispatcher(self.mueve_humano) # Rotulos informacion self.lbColor = Controles.LB(self, "").ponWrap().anchoMinimo(200) self.lbJuego = Controles.LB(self, "").alinCentrado() # Tool bar li_acciones = ( # ( _( "Start" ), Iconos.Empezar(), "empezar" ), (_("Analysis"), Iconos.Tutor(), "analizar"), (_("Cancel"), Iconos.Cancelar(), "cancelar"), (_("Continue"), Iconos.Pelicula_Seguir(), "seguir"), (_("Resign"), Iconos.Abandonar(), "abandonar"), ) self.tb = Controles.TB(self, li_acciones) lyT = Colocacion.V().control(self.tablero).relleno() lyV = Colocacion.V().control(self.lbJuego).relleno().control(self.lbColor).relleno(2) lyTV = Colocacion.H().otro(lyT).otro(lyV) ly = Colocacion.V().control(self.tb).otro(lyTV) self.setLayout(ly) self.position = Position.Position() self.restore_video() self.siguiente_jugada()
def insert(self, fen, mt): cp = Position.Position() cp.read_fen(fen) key = cp.pzs_key() fen_m2 = cp.fenm2() self.read_key(key, "initial") if fen_m2 not in self.current_dicfen: self.current_dicfen[fen_m2] = {"MATE": mt, "XFEN": cp.rotulo(), "ORIGIN": "manual"} self.db_data[key] = self.current_dicfen self.current_listfen.append(fen_m2) return key, fen_m2
def compruebaInput(self): if not self.engine: return self.veces += 1 if self.veces == 3: self.veces = 0 if self.siPlay: mrm = self.engine.ac_estado() rm = mrm.rmBest() if rm and rm.depth > self.depth: if mrm.li_rm: cp = Position.Position() cp.read_fen(self.fen) self.liData = [] def tr(tp, mas=""): self.liData.append((tp[0], "%.01f%%" % tp[1], "%s%s" % (mas, tp[2]))) tp = AnalisisIndexes.tp_gamestage(cp, mrm) self.liData.append((tp[0], "%d" % tp[1], tp[2])) pts = mrm.li_rm[0].centipawns_abs() mas = "" if pts: w, b = _("White"), _("Black") siW = "w" in self.fen if pts > 0: mas = w if siW else b elif pts < 0: mas = b if siW else w mas += "-" tr(AnalisisIndexes.tp_winprobability(cp, mrm), mas) tr(AnalisisIndexes.tp_complexity(cp, mrm)) tr(AnalisisIndexes.tp_efficientmobility(cp, mrm)) tr(AnalisisIndexes.tp_narrowness(cp, mrm)) tr(AnalisisIndexes.tp_piecesactivity(cp, mrm)) tr(AnalisisIndexes.tp_exchangetendency(cp, mrm)) tp = AnalisisIndexes.tp_positionalpressure(cp, mrm) self.liData.append((tp[0], "%d" % int(tp[1]), "")) tr(AnalisisIndexes.tp_materialasymmetry(cp, mrm)) self.grid.refresh() self.grid.resizeRowsToContents() QTUtil.refresh_gui() self.cpu.compruebaInput()
def __init__(self, owner, db_coordinates, is_white): QTVarios.WDialogo.__init__(self, owner, _("Coordinates"), Iconos.Blocks(), "runcoordinatesbasic") self.configuracion = Code.configuracion self.is_white = is_white self.db_coordinates = db_coordinates self.coordinates = None self.current_score = 0 self.working = False self.time_ini = None conf_board = self.configuracion.config_board( "RUNCOORDINATESBASIC", self.configuracion.size_base()) self.board = Tablero.TableroEstaticoMensaje(self, conf_board, None, 0.6) self.board.crea() self.board.bloqueaRotacion(True) self.cp_initial = Position.Position() self.cp_initial.set_pos_initial() self.board.ponerPiezasAbajo(self.is_white) self.board.setposition(self.cp_initial) font = Controles.TipoLetra( puntos=self.configuracion.x_sizefont_infolabels) lb_score_k = Controles.LB(self, _("Score") + ":").ponFuente(font) self.lb_score = Controles.LB(self).ponFuente(font) li_acciones = ((_("Close"), Iconos.MainMenu(), self.terminar), None, (_("Begin"), Iconos.Empezar(), self.begin), (_("Continue"), Iconos.Pelicula_Seguir(), self.seguir)) self.tb = QTVarios.LCTB(self, li_acciones) self.show_tb(self.terminar, self.begin) ly_info = Colocacion.G() ly_info.controld(lb_score_k, 0, 0).controlc(self.lb_score, 0, 1) ly_right = Colocacion.V().control( self.tb).relleno().otro(ly_info).relleno() w = QtWidgets.QWidget(self) w.setLayout(ly_right) w.setMinimumWidth(240) ly_center = Colocacion.H().control(self.board).control(w).margen(3) self.setLayout(ly_center) self.restore_video() self.adjustSize()
def posicionPegar(self): texto = QTUtil.traePortapapeles() if texto: cp = Position.Position() try: cp.read_fen(texto.strip()) self.fen = cp.fen() if self.fen == FEN_INITIAL: self.fen = "" self.muestraPosicion() except: pass
def orden_fen(self, fen): posicionInicial = Position.Position() posicionInicial.read_fen(fen) self.siW = posicionInicial.is_white self.tablero.setposition(posicionInicial) self.tablero.activaColor(self.siW) self.escribe("stop") game = Game.Game(fen=fen) self.engine.ac_inicio(game)
def reinicio(self, dbop): self.dbop = dbop self.game_type = GT_OPENING_LINES self.training = self.dbop.training() self.li_trainPositions = self.training["LITRAINPOSITIONS"] self.trposition = self.li_trainPositions[0] self.tm = 0 for game_info in self.li_trainPositions: for tr in game_info["TRIES"]: self.tm += tr["TIME"] self.liMensBasic = [ "%s: %d" % (_("Moves"), len(self.li_trainPositions)) ] self.siAyuda = False self.with_automatic_jump = True cp = Position.Position() cp.read_fen(self.trposition["FENM2"] + " 0 1") self.game = Game.Game(ini_posicion=cp) self.ayudas = 9999 # Para que analice sin problemas self.is_human_side_white = self.training["COLOR"] == "WHITE" self.is_engine_side_white = not self.is_human_side_white self.main_window.pon_toolbar((TB_CLOSE, TB_HELP, TB_CONFIG)) self.main_window.activaJuego(True, False, siAyudas=False) self.set_dispatcher(self.mueve_humano) self.setposition(cp) self.mostrarIndicador(True) self.quitaAyudas() self.ponPiezasAbajo(self.is_human_side_white) self.pgnRefresh(True) self.ponCapInfoPorDefecto() self.state = ST_PLAYING self.dgt_setposition() self.quitaInformacion() self.errores = 0 self.ini_time = time.time() self.muestraInformacion() self.siguiente_jugada()
def import_lista(self, tipo, li_fens_mt): cp = Position.Position() self.db_data.set_faster_mode() for fen, mt in li_fens_mt: cp.read_fen(fen) key = cp.pzs_key() fen_m2 = cp.fenm2() dic = self.db_data.get(key, {}) if fen_m2 in dic: dic[fen_m2]["ORIGIN"] = tipo else: dic[fen_m2] = {"MATE": mt, "XFEN": cp.rotulo(), "ORIGIN": tipo} self.db_data[key] = dic self.db_data.set_normal_mode()
def seguir(self): fenIni, fenFin, liMV, info = self.owner.dameOtro() self.liMV = liMV self.fenIni = fenIni self.fenFin = fenFin # Tableros cpIni = Position.Position() cpIni.read_fen(fenIni) is_white = cpIni.is_white self.tableroIni.ponerPiezasAbajo(is_white) self.tableroIni.setposition(cpIni) cpFin = Position.Position() cpFin.read_fen(fenFin) self.tableroFin.ponerPiezasAbajo(is_white) # esta bien self.tableroFin.setposition(cpFin) # Rotulo informacion self.lbInformacion.ponTexto(self.textoLBInformacion(info, cpIni)) # Rotulo vtime self.lbTiempo.ponTexto("") for wm in self.liwm: wm.limpia() self.time_base = time.time() self.btComprobar.show() self.btSeguir.hide() self.btCancelar.show() self.btTerminar.hide() self.liwm[0].activa()
def add_examples(self, key): cp = Position.Position() lista = self.db_examples[key] if lista: num_imported = 0 d = self.db_data.get(key, {}) for mt, fen_m2 in lista: if fen_m2 not in d: cp.read_fen(fen_m2) d[fen_m2] = {"MATE": mt, "XFEN": cp.rotulo(), "ORIGIN": "example"} num_imported += 1 self.db_data[key] = d return num_imported else: return 0
def pon_fen_hist(self, fen): if not self.history or self.history[-1] != fen: self.history.append(fen) self.position = Position.Position() self.position.read_fen(fen) self.fen = fen if self.siPlay: self.siW = self.position.is_white self.tablero.setposition(self.position) self.tablero.activaColor(self.siW) self.li_moves = self.book.almListaJugadas(fen) self.grid_moves.gotop() self.grid_moves.refresh() self.ponFlecha(0)
def recalcFenM2(self): lilipv = [FasterCode.xpv_pv(xpv).split(" ") for xpv in self.li_xpv] cp = Position.Position() dicFENm2 = {} for lipv in lilipv: FasterCode.set_init_fen() for pv in lipv: fen = FasterCode.get_fen() cp.read_fen(fen) fenm2 = cp.fenm2() if not (fenm2 in dicFENm2): dicFENm2[fenm2] = set() dicFENm2[fenm2].add(pv) FasterCode.make_move(pv) return dicFENm2
def preparaTest(self): self.cpInicial = Position.Position() self.cpInicial.read_fen("8/8/8/8/8/8/8/8 w - - 0 1") squares = self.cpInicial.squares self.baseUnica = self.test > 3 self.is_white = random.randint(1, 2) == 1 if self.test in (1, 4, 5): celdas_ocupadas = [] elif self.test == 2: # 4 peones celdas_ocupadas = [18, 21, 25, 27, 28, 30, 42, 45, 49, 51, 52, 54] for a1 in ("c3", "c6", "f3", "f6"): squares[a1] = "p" if self.is_white else "P" elif self.test == 3: # levitt ch = celdas_ocupadas = [27] for li in FasterCode.dict_q[27]: for x in li: ch.append(x) squares["d4"] = "q" if self.is_white else "Q" self.camino = [] p, f, s = 0, 7, 1 for x in range(8): li = list(range(p, f + s, s)) for t in range(7, -1, -1): if li[t] in celdas_ocupadas: del li[t] self.camino.extend(li) if s == 1: s = -1 p += 15 f += 1 else: s = +1 p += 1 f += 15 if self.test == 5: # empieza en e4 for n, x in enumerate(self.camino): if x == 28: del self.camino[n] self.camino.insert(0, 28) break self.current_position = 0 self.celdas_ocupadas = celdas_ocupadas
def restore(self, btxt_save): dic = Util.zip2var(btxt_save) self.first_position = Position.Position() self.first_position.read_fen(dic["first_position"]) self.first_comment = dic["first_comment"] self.result = dic["result"] self.termination = dic["termination"] self.li_moves = [] cp = self.first_position.copia() for save_jg in dic["li_moves"]: move = Move.Move(self, position_before=cp) move.restore(save_jg) cp = move.position.copia() self.li_moves.append(move) self.assign_opening() self.si3repetidas() self.li_tags = dic.get("li_tags", [])
def orden_fen(self, fen): if self.siThreats: fen = FasterCode.fen_other(fen) posicionInicial = Position.Position() posicionInicial.read_fen(fen) self.siW = posicionInicial.is_white if self.siThreats: self.siW = not self.siW self.tablero.setposition(posicionInicial) self.tablero.activaColor(self.siW) self.escribe("stop") game = Game.Game(fen=fen) self.engine.ac_inicio(game)