def juegaHumano(self, is_white): self.reloj_start(True) self.timekeeper.start() self.human_is_playing = True if self.premove: from_sq, to_sq = self.premove promotion = "q" if self.game.last_position.siPeonCoronando( from_sq, to_sq) else None siBien, error, move = Move.dameJugada(self.game, self.game.last_position, self.premove[0], self.premove[1], promotion) if siBien: self.mueve_humano(from_sq, to_sq, promotion) return self.premove = None self.pon_toolbar() self.analizaInicio() self.activaColor(is_white)
def piensa_rival(self): self.human_is_playing = False self.is_rival_thinking = True self.disable_all() if self.is_playing_gameobj(): if self.game_obj and self.pos_obj == len(self.game_obj): self.is_rival_thinking = False self.lineaTerminadaOpciones() return move = self.game_obj.move(self.pos_obj) self.pos_obj += 1 from_sq, to_sq, promotion = move.from_sq, move.to_sq, move.promotion else: self.pensando(True) self.rm_rival = self.xrival.juega() self.pensando(False) from_sq, to_sq, promotion = (self.rm_rival.from_sq, self.rm_rival.to_sq, self.rm_rival.promotion) self.is_rival_thinking = False siBien, mens, move = Move.dameJugada(self.game, self.game.last_position, from_sq, to_sq, promotion) self.is_analyzed_by_tutor = False is_obj = self.is_playing_gameobj() if self.is_tutor_enabled: if not is_obj: self.analizaTutor( ) # Que analice antes de activar humano, para que no tenga que esperar self.is_analyzed_by_tutor = True self.move_the_pieces(move.liMovs, True) self.add_move(move, False) if is_obj: self.lineaTerminadaOpciones() self.siguiente_jugada()
def sigueMaquina(self): ended, go_next = self.test_final() if ended: if go_next: self.play_next() return lista = self.t4.best_moves_game(self.game) if len(lista) == 0: return move = random.choice(lista) from_sq, to_sq, promotion = move[:2], move[2:4], move[4:] siBien, mens, move = Move.dameJugada(self.game, self.game.last_position, from_sq, to_sq, promotion) self.game.add_move(move) for movim in move.liMovs: if movim[0] == "b": self.tablero.borraPieza(movim[1]) elif movim[0] == "m": self.tablero.muevePieza(movim[1], movim[2]) elif movim[0] == "c": self.tablero.cambiaPieza(movim[1], movim[2]) self.timer = time.time() self.sigueHumano()
def mueve_rival(self, rm_rival): self.reloj_stop(False) self.pensando(False) time_s = self.timekeeper.stop() self.setSummary("TIMERIVAL", time_s) if self.state in (ST_ENDGAME, ST_PAUSE): return self.state == ST_ENDGAME if self.nAjustarFuerza == ADJUST_SELECTED_BY_PLAYER: rm_rival = self.ajustaPlayer(rm_rival) self.lirm_engine.append(rm_rival) if not (self.resign_limit < -1500): # then not ask for draw if not self.valoraRMrival(): self.muestra_resultado() return True siBien, self.error, move = Move.dameJugada(self.game, self.game.last_position, rm_rival.from_sq, rm_rival.to_sq, rm_rival.promotion) self.rm_rival = rm_rival if siBien: fen_ultimo = self.fenUltimo() move.set_time_ms(int(time_s * 1000)) self.add_move(move, False) self.move_the_pieces(move.liMovs, True) if self.siTiempo: move.cacheTime = self.vtime[self.is_engine_side_white].save() self.cache[fen_ultimo] = move self.siguiente_jugada() return True else: return False
def juegaRival(self): self.pensando(True) self.disable_all() from_sq = to_sq = promotion = "" siEncontrada = False if self.opening: siEncontrada, from_sq, to_sq, promotion = self.opening.run_engine( self.fenUltimo()) if not siEncontrada: self.opening = None if siEncontrada: self.rm_rival = EngineResponse.EngineResponse( "Apertura", self.is_engine_side_white) self.rm_rival.from_sq = from_sq self.rm_rival.to_sq = to_sq self.rm_rival.promotion = promotion else: self.rm_rival = self.xrival.juegaTiempo(self.tmRival, self.tmRival, 0) self.pensando(False) siBien, self.error, move = Move.dameJugada(self.game, self.game.last_position, self.rm_rival.from_sq, self.rm_rival.to_sq, self.rm_rival.promotion) if siBien: self.add_move(move, False) self.move_the_pieces(move.liMovs, True) return True else: return False
def mueve_humano(self, from_sq, to_sq, promotion=None): self.tablero.disable_all() movimiento = from_sq + to_sq position = self.game.move( self.posCurrent ).position if self.posCurrent >= 0 else self.game.last_position # Peon coronando if not promotion and position.siPeonCoronando(from_sq, to_sq): promotion = self.tablero.peonCoronando(position.is_white) if promotion is None: self.sigueHumano() return False if promotion: movimiento += promotion siBien, mens, move = Move.dameJugada(self.game, position, from_sq, to_sq, promotion) if siBien: self.nuevaJugada(move) else: self.actualizaPosicion()
def mueve_humano(self, from_sq, to_sq, promotion=None): jgUsu = self.checkmueve_humano(from_sq, to_sq, promotion) if not jgUsu: return False movimiento = jgUsu.movimiento() position = self.game.last_position isValid = self.motorGM.isValidMove(movimiento) analisis = None if not isValid: self.tablero.setposition(position) self.tablero.activaColor(self.is_human_side_white) li_moves = self.motorGM.dameJugadasTXT(position, True) desdeGM, hastaGM, coronacionGM = PantallaGM.eligeJugada(self, li_moves, True) siAnalizaJuez = self.siJuez if siAnalizaJuez: if self.book: fen = self.fenUltimo() siH = self.book.check_human(fen, from_sq, to_sq) siGM = self.book.check_human(fen, desdeGM, hastaGM) if siGM and siH: siAnalizaJuez = False else: self.book = False else: siAnalizaJuez = ( self.siJuez 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, coronacionGM = from_sq, to_sq, promotion siBien, mens, jgGM = Move.dameJugada(self.game, position, desdeGM, hastaGM, coronacionGM) movGM = jgGM.pgn_translated() movUsu = jgUsu.pgn_translated() if siAnalizaJuez: um = QTUtil2.analizando(self.main_window) mrm = self.analizaMinimo(self.vtime * 100) 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, coronacionGM) pos_gm = mrm.agregaRM(rmGM) self.analizaInicio() um.final() analisis = mrm, pos_gm dpts = rmUsu.centipawns_abs() - rmGM.centipawns_abs() if self.mostrar is None or ((self.mostrar is True) and not isValid): w = PantallaJuicio.WJuicio( self, self.xtutor, self.nombreGM, position, mrm, rmGM, rmUsu, analisis, siCompetitivo=not self.showevals, ) w.exec_() rm, pos_gm = w.analysis[0].buscaRM(jgGM.movimiento()) analisis = w.analysis[0], pos_gm dpts = w.difPuntos() self.puntos += dpts comentario0 = "<b>%s</b> : %s = %s<br>" % (self.configuracion.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>" % (_("Points 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 = analisis self.add_move(jgGM, True) self.error = "" self.siguiente_jugada() return True
def comprobar(self): self.stop_clock() self.pon_toolbar([self.cancelar]) for wm in self.liwm: wm.deshabilita() um = QTUtil2.analizando(self) self.liAnalisis = [] cp = Position.Position() cp.read_fen(self.fen) siError = False totalPuntos = 0 factor = 1 previo = 100 for wm in self.liwm: from_sq, to_sq, promotion = wm.resultado() if from_sq: cpNue = cp.copia() siBien, mensaje = cpNue.mover(from_sq, to_sq, promotion) wm.siCorrecto(siBien) if not siBien: wm.ponError(_("Invalid move")) siError = True break move = Move.Move(None) move.ponDatos(cp, cpNue, from_sq, to_sq, promotion) mrm, pos = self.xtutor.analyse_move(move, self.xtutor.motorTiempoJugada) move.analysis = mrm, pos self.liAnalisis.append(move) rm = mrm.li_rm[pos] rj = mrm.li_rm[0] dif = rj.centipawns_abs() - rm.centipawns_abs() if dif >= 100: puntos = 0 else: puntos = 100 - dif wm.ponPuntos(puntos) cp = cpNue totalPuntos += int(puntos * factor * previo / 100) previo = puntos * previo / 100 factor *= 2 else: break um.final() self.btConsultar.show() if not siError: self.lbTiempo.ponTexto("<h2>%s: %d %s</h2>" % (_("Result"), totalPuntos, _("pts"))) self.historico.append( Util.today(), totalPuntos, self.xtutor.clave, int(self.xtutor.motorTiempoJugada / 1000), self.min_min, self.min_max, self.linea, self.ref, ) self.pon_toolbar([self.terminar])
def pgn_game(pgn): game = Game() last_posicion = game.first_position jg_activa = None if type(pgn) is bytes: pgn = pgn.decode("utf-8", errors="ignore") li = FasterCode.xparse_pgn(pgn) if li is None: return False, game si_fen = False dic_nags = { "!": NAG_1, "?": NAG_2, "!!": NAG_3, "‼": NAG_3, "??": NAG_4, "⁇": NAG_4, "!?": NAG_5, "⁉": NAG_5, "?!": NAG_6, "⁈": NAG_6, } FasterCode.set_init_fen() for elem in li: key = elem[0] if elem else "" if key == "[": kv = elem[1:].strip() pos = kv.find(" ") if pos > 0: lb = kv[:pos] vl = kv[pos + 1:].strip() lbup = lb.upper() if lbup == "FEN": FasterCode.set_fen(vl) if vl != FEN_INITIAL: game.set_fen(vl) last_posicion = game.first_position game.add_tag(lb, vl) si_fen = True elif lbup == "RESULT": game.result = vl game.add_tag(lb, vl) else: game.add_tag(lb, vl) elif key == "M": a1h8 = elem[1:] posicion_base = last_posicion FasterCode.make_move(a1h8) last_posicion = Position.Position() last_posicion.read_fen(FasterCode.get_fen()) jg_activa = Move.Move(game, posicion_base, last_posicion, a1h8[:2], a1h8[2:4], a1h8[4:]) game.li_moves.append(jg_activa) elif key in "!?": if jg_activa: jg_activa.add_nag(dic_nags.get(elem, None)) elif key == "$": if jg_activa: nag = elem[1:] if nag.isdigit(): nag = int(nag) if 0 < nag < 256: jg_activa.add_nag(nag) elif key in "{;": comment = elem[1:-1].strip() if comment: if jg_activa: if jg_activa.comment: jg_activa.comment += "\n" jg_activa.comment += comment else: game.set_first_comment(comment) elif key == "(": variation = elem[1:-1].strip() if variation: if jg_activa: fen = FasterCode.get_fen() jg_activa.variations.add_pgn_variation(variation) FasterCode.set_fen(fen) elif key == "R": if jg_activa: r1 = elem[1] if r1 == "1": game.result = RESULT_WIN_WHITE elif r1 == "2": game.result = RESULT_DRAW elif r1 == "3": game.result = RESULT_WIN_BLACK elif r1 == "0": game.result = RESULT_UNKNOWN if si_fen: game.pending_opening = False if jg_activa: game.comprueba() return True, game
def mueve_humano(self, from_sq, to_sq, promotion=None): if not self.human_is_playing: return self.check_premove(from_sq, to_sq) move = self.checkmueve_humano(from_sq, to_sq, promotion, not self.is_tutor_enabled) if not move: return False movimiento = move.movimiento() siAnalisis = False is_selected = False fen_base = self.fenUltimo() if self.bookR and self.bookMandatory: listaJugadas = self.bookR.miraListaJugadas(fen_base) if listaJugadas: li = [] for apdesde, aphasta, apcoronacion, nada, nada1 in listaJugadas: mx = apdesde + aphasta + apcoronacion if mx.strip().lower() == movimiento: is_selected = True break li.append((apdesde, aphasta, False)) if not is_selected: self.tablero.ponFlechasTmp(li) self.sigueHumano() return False if not is_selected and self.aperturaObl: if self.aperturaObl.check_human(fen_base, from_sq, to_sq): is_selected = True else: apdesde, aphasta = self.aperturaObl.from_to_active(fen_base) if apdesde is None: self.aperturaObl = None else: self.tablero.ponFlechasTmp(((apdesde, aphasta, False), )) self.sigueHumano() return False if not is_selected and self.aperturaStd: if self.aperturaStd.check_human(fen_base, from_sq, to_sq): is_selected = True else: if not self.aperturaStd.is_active(fen_base): self.aperturaStd = None is_mate = move.is_mate self.analizaFinal(is_mate) # tiene que acabar siempre if not is_mate and not is_selected and self.is_tutor_enabled: if not self.tutor_book.si_esta(fen_base, movimiento): rmUser, n = self.mrmTutor.buscaRM(movimiento) if not rmUser: self.main_window.pensando_tutor(True) rmUser = self.xtutor.valora(self.game.last_position, from_sq, to_sq, move.promotion) self.main_window.pensando_tutor(False) if not rmUser: self.sigueHumanoAnalisis() return False self.mrmTutor.agregaRM(rmUser) siAnalisis = True pointsBest, pointsUser = self.mrmTutor.difPointsBest( movimiento) self.setSummary("POINTSBEST", pointsBest) self.setSummary("POINTSUSER", pointsUser) difpts = self.configuracion.x_tutor_difpoints difporc = self.configuracion.x_tutor_difporc if self.mrmTutor.mejorRMQue(rmUser, difpts, difporc): if not move.is_mate: if self.chance: num = self.mrmTutor.numMejorMovQue(movimiento) if num: rmTutor = self.mrmTutor.rmBest() menu = QTVarios.LCMenu(self.main_window) menu.opcion("None", _("There are %d best moves") % num, Iconos.Motor()) menu.separador() menu.opcion( "tutor", "&1. %s (%s)" % (_("Show tutor"), rmTutor.abrTextoBase()), Iconos.Tutor(), ) menu.separador() menu.opcion("try", "&2. %s" % _("Try again"), Iconos.Atras()) menu.separador() menu.opcion( "user", "&3. %s (%s)" % (_("Select my move"), rmUser.abrTextoBase()), Iconos.Player(), ) self.main_window.cursorFueraTablero() resp = menu.lanza() if resp == "try": self.sigueHumanoAnalisis() return False elif resp == "user": siTutor = False elif resp != "tutor": self.sigueHumanoAnalisis() return False tutor = Tutor.Tutor(self, self, move, from_sq, to_sq, False) if self.aperturaStd: liApPosibles = self.listaAperturasStd.list_possible_openings( self.game) else: liApPosibles = None if tutor.elegir(self.ayudas > 0, liApPosibles=liApPosibles): if self.ayudas > 0: # doble entrada a tutor. self.reponPieza(from_sq) self.ayudas -= 1 self.tutor_con_flechas = self.nArrowsTt > 0 and self.ayudas > 0 from_sq = tutor.from_sq to_sq = tutor.to_sq promotion = tutor.promotion siBien, mens, jgTutor = Move.dameJugada( self.game, self.game.last_position, from_sq, to_sq, promotion) if siBien: move = jgTutor self.setSummary("SELECTTUTOR", True) if self.configuracion.x_save_tutor_variations: tutor.ponVariantes(move, 1 + len(self.game) / 2) del tutor self.setSummary("TIMEUSER", self.timekeeper.stop()) self.reloj_stop(True) self.move_the_pieces(move.liMovs) if siAnalisis: rm, nPos = self.mrmTutor.buscaRM(move.movimiento()) if rm: move.analysis = self.mrmTutor, nPos self.add_move(move, True) self.error = "" self.siguiente_jugada() return True
def mueve_humano(self, from_sq, to_sq, promotion=None): move = self.checkmueve_humano(from_sq, to_sq, promotion) if not move: return False movUsu = move.movimiento().lower() self.dbwashing.add_time(self.timekeeper.stop()) jgObj = self.partidaObj.move(self.posJugadaObj) movObj = jgObj.movimiento().lower() if movUsu != movObj: lic = [] if jgObj.analysis: mrmObj, posObj = jgObj.analysis rmObj = mrmObj.li_rm[posObj] lic.append("%s: %s (%s)" % (_("Played previously"), jgObj.pgn_translated(), rmObj.abrTextoBase())) ptsObj = rmObj.centipawns_abs() rmUsu, posUsu = mrmObj.buscaRM(movUsu) if posUsu >= 0: lic.append("%s: %s (%s)" % (_("Played now"), move.pgn_translated(), rmUsu.abrTextoBase())) ptsUsu = rmUsu.centipawns_abs() if ptsUsu < ptsObj - 10: lic[-1] += ' <span style="color:red"><b>%s</b></span>' % _( "Bad move") self.errores += 1 self.dbwashing.add_hint() else: lic.append("%s: %s (?) %s" % (_("Played now"), move.pgn_translated(), _("Bad move"))) self.errores += 1 self.dbwashing.add_hint() else: # Debe ser una move de libro para aceptarla fen = self.fenUltimo() siBookUsu = self.book.check_human(fen, from_sq, to_sq) bmove = _("book move") lic.append( "%s: %s (%s)" % (_("Played previously"), jgObj.pgn_translated(), bmove)) if siBookUsu: lic.append("%s: %s (%s)" % (_("Played now"), move.pgn_translated(), bmove)) else: lic.append("%s: %s (?) %s" % (_("Played now"), move.pgn_translated(), _("Bad move"))) self.errores += 1 self.dbwashing.add_hint() comment = "<br>".join(lic) w = PantallaJuicio.MensajeF(self.main_window, comment) w.mostrar() self.setposition(move.position_before) # Creamos un move sin analisis siBien, self.error, move = Move.dameJugada(self.game, self.game.last_position, jgObj.from_sq, jgObj.to_sq, jgObj.promotion) self.move_the_pieces(move.liMovs) self.add_move(move, True) self.posJugadaObj += 1 if len(self.game) == self.partidaObj.num_moves(): self.finPartida() else: self.error = "" self.siguiente_jugada() return True