Beispiel #1
0
    def read(self, line):
        li = line.split("|")
        self.position.read_fen(li[0])
        nli = len(li)
        if nli > 1:
            self.label = li[1]

            if nli > 2:
                solucion = li[2]
                ok, game_obj = Game.pgn_game('[FEN "%s"]\n%s' %
                                             (self.position.fen(), solucion))
                if ok:
                    self.game_obj = game_obj

                    if nli > 3:
                        txt = li[3].replace("]", "]\n").replace(" [", "[")
                        ok, game_original = Game.pgn_game(txt)
                        if ok:
                            ok = False
                            for n in range(len(game_original) - 1, -1, -1):
                                move = game_original.move(n)
                                if move.position == self.position:
                                    ok = True
                                    if n + 1 != len(game_original):
                                        game_original.li_moves = game_original.li_moves[:
                                                                                        n
                                                                                        +
                                                                                        1]
                                    break
                        if ok:
                            self.game_original = game_original
                            self.game_original.set_unknown()
Beispiel #2
0
    def leePGNRecno(self, recno, sp):
        raw = self.leeAllRecno(recno)
        litags = []
        result = "*"
        for field in self.li_fields:
            if not (field in ("XPV", "_DATA_", "PLYCOUNT")):
                v = raw[field]
                if v:
                    litags.append((drots.get(field, field), v if type(v) == str else str(v)))
                    if field == "RESULT":
                        result = v if type(v) == str else str(v)

        litags.append(("PlyCount", str(raw["PLYCOUNT"])))
        xpgn = raw["_DATA_"]
        if xpgn:
            if xpgn.startswith(BODY_SAVE):
                ok, p = Game.pgn_game(xpgn[len(BODY_SAVE):].strip())
            else:
                p = Game.Game()
                p.restore(xpgn)
            p.set_tags(litags)
            return p.pgn(), p.resultado()

        pgn = xpv_pgn(raw["XPV"])
        if sp == "\r\n":
            pgn = pgn.replace("\n", "\r\n")
        tags = []
        for t, v in litags:
            tags.append('[%s "%s"]' % (t, v))
        pgn = sp.join(tags) + sp + sp + pgn
        return pgn, result
Beispiel #3
0
    def leePartidaRaw(self, raw):
        p = Game.Game()
        xpgn = raw["_DATA_"]
        ok = False
        if xpgn:
            if xpgn.startswith(BODY_SAVE):
                ok, p = Game.pgn_game(xpgn[len(BODY_SAVE):].strip())
            else:
                try:
                    p.restore(xpgn)
                    ok = True
                except:
                    ok = False

        if not ok:
            fen, pv = self.read_xpv(raw["XPV"])
            if fen:
                p.set_fen(fen)
            p.read_pv(pv)

        litags = []
        for field in self.li_fields:
            if not (field in ("XPV", "_DATA_", "PLYCOUNT")):
                v = raw[field]
                if v:
                    litags.append((drots.get(field, field), v if type(v) == str else str(v)))
        litags.append(("PlyCount", str(raw["PLYCOUNT"])))

        p.set_tags(litags)
        p.assign_opening()
        return p
Beispiel #4
0
def pgn_lcsb(estado, pgn, jugada_inicial=None):
    ok, game = Game.pgn_game(pgn)
    if jugada_inicial:
        move = game.move(jugada_inicial)
        si_blancas_abajo = move.position_before.is_white
    else:
        si_blancas_abajo = True

    return dict(ESTADO=estado, GAME=game.save(), WHITEBOTTOM=si_blancas_abajo)
Beispiel #5
0
    def work_read_position(self):
        if self.w_reinforcement_working:
            pos = self.reinforcement.get_working_position()
        else:
            pos = self.current_position()
        txt = self.liFNS[pos]
        li = txt.split("|")

        fen = li[0]
        if fen.endswith(" 0"):
            fen = fen[:-1] + "1"
        rotulo = li[1]
        solucion = li[2]
        ok, game_obj = Game.pgn_game('[FEN "%s"]\n%s' % (fen, solucion))

        game_base = None
        if len(li) > 3:
            txt = li[3].replace("]", "]\n").replace(" [", "[")
            ok, game_base = Game.pgn_game(txt)
            if ok:
                cp = Position.Position()
                cp.read_fen(fen)
                ok = False
                for n in range(len(game_base) - 1, -1, -1):
                    move = game_base.move(n)
                    if move.position == cp:
                        ok = True
                        if n + 1 != len(game_base):
                            game_base.li_moves = game_base.li_moves[:n + 1]
                        break
            if not ok:
                game_base = None

        if game_base:
            game_base.set_unknown()

        self.w_label = rotulo

        return game_obj, game_base
Beispiel #6
0
    def read_line(self, line):
        li = line.strip().split("|")
        self.fen, self.label, pgn_moves = li[0], li[1], li[2]

        self.pgn = '[FEN "%s"]\n\n%s' % (self.fen, pgn_moves)
        ok, p = Game.pgn_game(self.pgn)
        self.pv = p.pv()
        self.limoves = self.pv.split(" ")

        nmoves = len(self.limoves)
        self.num_moves = int(len(self.limoves) / 2)
        if nmoves % 2 == 1:
            self.num_moves += 1
        return self
Beispiel #7
0
 def paste(self, texto):
     try:
         if "." in texto or '"' in texto:
             ok, game = Game.pgn_game(texto)
             if not ok:
                 return
         elif "/" in texto:
             game = Game.Game(fen=texto)
         else:
             return
         self.bloqueApertura = None
         self.xfichero = None
         self.xpgn = None
         self.xjugadaInicial = None
         dic = self.creaDic()
         dic["GAME"] = game.save()
         dic["WHITEBOTTOM"] = game.last_position.is_white
         self.reiniciar(dic)
     except:
         pass
Beispiel #8
0
    def current_pgn(self):
        pgn = ""
        for key, value in self.li_labels:
            key = key.strip()
            value = value.strip()
            if key and value:
                pgn += '[%s "%s"]\n' % (key, value)
        pgn += "\n%s\n" % self.em_body.texto().strip()
        if "\r\n" in pgn:
            pgn = pgn.replace("\r\n", "\n")
        if Code.isWindows:
            pgn = pgn.replace("\n", "\r\n")

        if self.chb_remove_c_v.isChecked():
            ok, p = Game.pgn_game(pgn)

            pgn = ""
            for k, v in p.li_tags:
                pgn += '[%s "%s"]\n' % (k, v)
            pgn += "\n\n"
            pgn += p.pgnBase()

        return pgn
Beispiel #9
0
    def configurarGS(self):
        sep = (None, None, None)

        liMasOpciones = [
            ("rotacion", _("Auto-rotate board"), Iconos.JS_Rotacion()),
            sep,
            ("leerpgn", _("Read PGN"), Iconos.PGN_Importar()),
            sep,
            ("pastepgn", _("Paste PGN"), Iconos.Pegar16()),
            sep,
        ]
        if not self.siCompleta:
            liMasOpciones.extend([
                ("position", _("Edit start position"), Iconos.Datos()),
                sep,
                ("pasteposicion", _("Paste FEN position"), Iconos.Pegar16()),
                sep,
                ("voyager", _("Voyager 2"), Iconos.Voyager1()),
            ])

        resp = self.configurar(liMasOpciones,
                               siCambioTutor=True,
                               siSonidos=True)

        if resp == "rotacion":
            self.auto_rotate = not self.auto_rotate
            is_white = self.game.last_position.is_white
            if self.auto_rotate:
                if is_white != self.tablero.is_white_bottom:
                    self.tablero.rotaTablero()

        elif resp == "position":
            ini_position = self.game.first_position
            new_position = Voyager.voyager_position(self.main_window,
                                                    ini_position)
            if new_position and new_position != ini_position:
                self.game.set_position(new_position)
                self.inicio(self.game, self.siCompleta)

        elif resp == "pasteposicion":
            texto = QTUtil.traePortapapeles()
            if texto:
                cp = Position.Position()
                try:
                    cp.read_fen(str(texto))
                    self.fen = cp.fen()
                    self.posicApertura = None
                    self.reiniciar()
                except:
                    pass

        elif resp == "leerpgn":
            game = self.procesador.select_1_pgn(self.main_window)
            if game:
                if self.siCompleta and not game.siFenInicial():
                    return
                p = Game.Game()
                p.leeOtra(game)
                p.assign_opening()
                self.reinicio = p.save()
                self.reiniciar()

        elif resp == "pastepgn":
            texto = QTUtil.traePortapapeles()
            if texto:
                ok, game = Game.pgn_game(texto)
                if not ok:
                    QTUtil2.message_error(
                        self.main_window,
                        _("The text from the clipboard does not contain a chess game in PGN format"
                          ))
                    return
                if self.siCompleta and not game.siFenInicial():
                    return
                self.reinicio = game.save()
                self.reiniciar()

        elif resp == "voyager":
            ptxt = Voyager.voyagerPartida(self.main_window, self.game)
            if ptxt:
                dic = self.creaDic()
                dic["GAME"] = ptxt.save()
                dic["FEN"] = None if ptxt.siFenInicial(
                ) else ptxt.first_position.fen()
                dic["WHITEBOTTOM"] = self.tablero.is_white_bottom
                self.reiniciar(dic)
Beispiel #10
0
    def configurarGS(self):
        mt = _("Engine").lower()
        mt = _X(_("Disable %1"), mt) if self.play_against_engine else _X(
            _("Enable %1"), mt)

        sep = (None, None, None)

        liMasOpciones = [
            ("rotacion", _("Auto-rotate board"), Iconos.JS_Rotacion()),
            sep,
            ("opening", _("Opening"), Iconos.Apertura()),
            sep,
            ("position", _("Edit start position"), Iconos.Datos()),
            sep,
            ("pasteposicion", _("Paste FEN position"), Iconos.Pegar16()),
            sep,
            ("leerpgn", _("Read PGN"), Iconos.PGN_Importar()),
            sep,
            ("pastepgn", _("Paste PGN"), Iconos.Pegar16()),
            sep,
            ("engine", mt, Iconos.Motores()),
            sep,
            ("voyager", _("Voyager 2"), Iconos.Voyager1()),
        ]
        resp = self.configurar(liMasOpciones,
                               siCambioTutor=True,
                               siSonidos=True)

        if resp == "rotacion":
            self.auto_rotate = not self.auto_rotate
            is_white = self.game.last_position.is_white
            if self.auto_rotate:
                if is_white != self.tablero.is_white_bottom:
                    self.tablero.rotaTablero()
        elif resp == "opening":
            me = self.unMomento()
            w = PantallaOpenings.WAperturas(self.main_window,
                                            self.configuracion,
                                            self.bloqueApertura)
            me.final()
            if w.exec_():
                self.bloqueApertura = w.resultado()
                self.xfichero = None
                self.xpgn = None
                self.xjugadaInicial = None
                self.reiniciar()

        elif resp == "position":
            self.startPosition()

        elif resp == "pasteposicion":
            texto = QTUtil.traePortapapeles()
            if texto:
                cp = Position.Position()
                try:
                    cp.read_fen(str(texto))
                    self.xfichero = None
                    self.xpgn = None
                    self.xjugadaInicial = None
                    self.new_game()
                    self.game.set_position(first_position=cp)
                    self.bloqueApertura = None
                    self.reiniciar()
                except:
                    pass

        elif resp == "leerpgn":
            self.leerpgn()

        elif resp == "pastepgn":
            texto = QTUtil.traePortapapeles()
            if texto:
                ok, game = Game.pgn_game(texto)
                if not ok:
                    QTUtil2.message_error(
                        self.main_window,
                        _("The text from the clipboard does not contain a chess game in PGN format"
                          ))
                    return
                self.xfichero = None
                self.xpgn = None
                self.xjugadaInicial = None
                self.bloqueApertura = None
                dic = self.creaDic()
                dic["GAME"] = game.save()
                dic["WHITEBOTTOM"] = game.last_position.is_white
                self.reiniciar(dic)

        elif resp == "engine":
            self.ponRotulo1("")
            if self.play_against_engine:
                if self.xrival:
                    self.xrival.terminar()
                    self.xrival = None
                self.play_against_engine = False
            else:
                self.cambioRival()

        elif resp == "voyager":
            ptxt = Voyager.voyagerPartida(self.main_window, self.game)
            if ptxt:
                self.xfichero = None
                self.xpgn = None
                self.xjugadaInicial = None
                dic = self.creaDic()
                dic["GAME"] = ptxt.save()
                dic["WHITEBOTTOM"] = self.tablero.is_white_bottom
                self.reiniciar(dic)
Beispiel #11
0
    def editar(self, fila):

        if fila is None:
            name = ""
            eco = ""
            pgn = ""
            estandar = True
            titulo = _("New opening")

        else:
            reg = self.lista[fila]

            name = reg["NOMBRE"]
            eco = reg["ECO"]
            pgn = reg["PGN"]
            estandar = reg["ESTANDAR"]

            titulo = name

        # Datos
        liGen = [(None, None)]
        liGen.append((_("Name") + ":", name))
        config = FormLayout.Editbox("ECO", ancho=30, rx="[A-Z, a-z][0-9][0-9]")
        liGen.append((config, eco))
        liGen.append((_("Add to standard list") + ":", estandar))

        # Editamos
        resultado = FormLayout.fedit(liGen,
                                     title=titulo,
                                     parent=self,
                                     anchoMinimo=460,
                                     icon=Iconos.Apertura())
        if resultado is None:
            return

        accion, liResp = resultado
        name = liResp[0].strip()
        if not name:
            return
        eco = liResp[1].upper()
        estandar = liResp[2]

        fen = FEN_INITIAL

        self.procesador.procesador = self.procesador  # ya que editaVariante espera un gestor

        if pgn:
            ok, game = Game.pgn_game(pgn)
            if not ok:
                game = Game.Game()
        else:
            game = Game.Game()

        resp = Variantes.editaVariante(self.procesador,
                                       game,
                                       titulo=name,
                                       is_white_bottom=True)

        if resp:
            game = resp

            reg = {}
            reg["NOMBRE"] = name
            reg["ECO"] = eco
            reg["PGN"] = game.pgnBaseRAW()
            reg["A1H8"] = game.pv()
            reg["ESTANDAR"] = estandar

            if fila is None:
                self.lista.append(reg)
                self.grid.refresh()
                self.grabar()
            else:
                self.lista[fila] = reg
            self.grid.refresh()
            self.grabar()