Esempio n. 1
0
    def player_has_moved(self, from_sq, to_sq, promotion=""):
        jgUsu = self.check_human_move(from_sq, to_sq, promotion)
        if not jgUsu:
            return False

        movimiento = jgUsu.movimiento()
        position = self.game.last_position
        isValid = self.motorGM.is_valid_move(movimiento)
        analysis = None

        if not isValid:
            self.board.set_position(position)
            self.board.activate_side(self.human_side)
            li_moves = self.motorGM.get_moves_txt(position, True)
            desdeGM, hastaGM, promotionGM = WindowGM.select_move(
                self, li_moves, True)
            siAnalizaJuez = self.with_adjudicator
            if siAnalizaJuez:
                if self.book:
                    fen = self.last_fen()
                    siH = self.book.check_human(fen, from_sq, to_sq)
                    is_gm = self.book.check_human(fen, desdeGM, hastaGM)
                    if is_gm and siH:
                        siAnalizaJuez = False
                    else:
                        self.book = False
        else:
            siAnalizaJuez = self.with_adjudicator and self.mostrar is None  # None es ver siempre False no ver nunca True ver si diferentes
            if len(movimiento) == 5:
                promotion = movimiento[4].lower()
            desdeGM, hastaGM, promotionGM = from_sq, to_sq, promotion

        ok, mens, jgGM = Move.get_game_move(self.game, position, desdeGM,
                                            hastaGM, promotionGM)
        movGM = jgGM.pgn_translated()
        movUsu = jgUsu.pgn_translated()

        if siAnalizaJuez:
            um = QTUtil2.analizando(self.main_window)
            mrm = self.analizaMinimo(self.vtime * 100)

            import time

            t = time.time()

            rmUsu, nada = mrm.buscaRM(jgUsu.movimiento())
            if rmUsu is None:
                um = QTUtil2.analizando(self.main_window)
                self.analizaFinal()
                rmUsu = self.xtutor.valora(position, from_sq, to_sq, promotion)
                mrm.agregaRM(rmUsu)
                self.analizaInicio()
                um.final()

            rmGM, pos_gm = mrm.buscaRM(jgGM.movimiento())
            if rmGM is None:
                self.analizaFinal()
                rmGM = self.xtutor.valora(position, desdeGM, hastaGM,
                                          promotionGM)
                pos_gm = mrm.agregaRM(rmGM)
                self.analizaInicio()

            um.final()

            analysis = mrm, pos_gm
            dpts = rmUsu.centipawns_abs() - rmGM.centipawns_abs()

            if self.mostrar is None or ((self.mostrar is True)
                                        and not isValid):
                w = WindowJuicio.WJuicio(self,
                                         self.xtutor,
                                         self.nombreGM,
                                         position,
                                         mrm,
                                         rmGM,
                                         rmUsu,
                                         analysis,
                                         is_competitive=not self.show_evals)
                w.exec_()

                rm, pos_gm = w.analysis[0].buscaRM(jgGM.movimiento())
                analysis = w.analysis[0], pos_gm

                dpts = w.difPuntos()

            self.puntos += dpts

            comentario0 = "<b>%s</b> : %s = %s<br>" % (
                self.configuration.x_player, movUsu, rmUsu.texto())
            comentario0 += "<b>%s</b> : %s = %s<br>" % (self.nombreGM, movGM,
                                                        rmGM.texto())
            comentario1 = "<br><b>%s</b> = %+d<br>" % (_("Difference"), dpts)
            comentario2 = "<b>%s</b> = %+d<br>" % (_("Centipawns accumulated"),
                                                   self.puntos)
            self.textoPuntuacion = comentario2
            self.ponRotuloSecundario()

            if not isValid:
                jgGM.comment = (comentario0 + comentario1 +
                                comentario2).replace("<b>", "").replace(
                                    "</b>", "").replace("<br>", "\n")

        self.analizaFinal()

        self.move_the_pieces(jgGM.liMovs)

        jgGM.analysis = analysis
        self.add_move(jgGM, True)
        self.error = ""
        self.play_next_move()
        return True
Esempio n. 2
0
    def play_next_move(self):
        self.analizaFinal()
        self.disable_all()

        if self.state == ST_ENDGAME:
            return

        self.state = ST_PLAYING

        self.human_is_playing = False
        self.put_view()

        is_white = self.game.last_position.is_white

        if (len(self.game) > 0) and self.motorGM.is_finished():
            self.put_result()
            return

        self.set_side_indicator(is_white)
        self.refresh()

        siRival = is_white == self.is_engine_side_white

        if self.jugInicial > 1:
            si_jug_inicial = len(self.game) < (self.jugInicial - 1) * 2
        else:
            si_jug_inicial = False

        li_alternativas = self.motorGM.alternativas()
        nli_alternativas = len(li_alternativas)

        # Movimiento automatico
        if si_jug_inicial or self.on_opening or self.on_bypass_book:
            siBuscar = True
            if self.on_opening:
                li_pv = self.opening.a1h8.split(" ")
                nj = len(self.game)
                if len(li_pv) > nj:
                    move = li_pv[nj]
                    if move in li_alternativas:
                        siBuscar = False
                    else:
                        self.on_opening = False
                else:
                    self.on_opening = False

            if siBuscar:
                if self.on_bypass_book:
                    li_moves = self.bypass_book.get_list_moves(self.last_fen())
                    liN = []
                    for from_sq, to_sq, promotion, pgn, peso in li_moves:
                        move = from_sq + to_sq + promotion
                        if move in li_alternativas:
                            liN.append(move)
                    if liN:
                        siBuscar = False
                        nli_alternativas = len(liN)
                        if nli_alternativas > 1:
                            pos = random.randint(0, nli_alternativas - 1)
                            move = liN[pos]
                        else:
                            move = liN[0]
                    else:
                        self.on_bypass_book = None

            if siBuscar:
                if si_jug_inicial:
                    siBuscar = False
                    if nli_alternativas > 1:
                        pos = random.randint(0, nli_alternativas - 1)
                        move = li_alternativas[pos]
                    elif nli_alternativas == 1:
                        move = li_alternativas[0]

            if not siBuscar:
                self.play_rival(move)
                self.play_next_move()
                return

        if siRival:
            if nli_alternativas > 1:
                if self.select_rival_move:
                    li_moves = self.motorGM.get_moves_txt(
                        self.game.last_position, False)
                    from_sq, to_sq, promotion = WindowGM.select_move(
                        self, li_moves, False)
                    move = from_sq + to_sq + promotion
                else:
                    pos = random.randint(0, nli_alternativas - 1)
                    move = li_alternativas[pos]
            else:
                move = li_alternativas[0]

            self.play_rival(move)
            self.play_next_move()

        else:
            self.human_is_playing = True
            if self.with_adjudicator:
                self.thinking(True)
                self.analizaInicio()
                self.thinking(False)
            self.activate_side(is_white)