示例#1
0
    def get_movimentos_simples(self, tabuleiro: tp.board, flags: list,
                               pos: tp.coord) -> tp.movements:
        res = movements_vazio()
        i, j = pos

        # Direções a verificar
        DIRECOES = (
            # Casas acima
            tp.direction(-2, -1),
            tp.direction(-2, 1),
            # Casas abaixo
            tp.direction(2, -1),
            tp.direction(2, 1),
            # Casas a esquerda
            tp.direction(-1, -2),
            tp.direction(1, -2),
            # Casas a direita
            tp.direction(-1, 2),
            tp.direction(1, 2),
        )

        for direcao in DIRECOES:
            nova_pos = pos + direcao
            if nova_pos.valida():
                m, n = nova_pos
                res[m][n] = self._criar_movimento(tabuleiro,
                                                  tp.action(pos, nova_pos))

        return res
示例#2
0
    def event(self, event: pg.event.EventType) -> None:
        match event:
            case pg.event.EventType(type=pg.MOUSEBUTTONDOWN, button=1):
                # click esquerdo
                click_antigo = self._click

                self._click = tp.coord(
                    int(event.pos[1] // self._qsize[1]),
                    int(event.pos[0] // self._qsize[0]),
                )

                movimentado = False
                if self.movimento and click_antigo:
                    movimento_oconteceu = self.tabuleiro.movimenta_peca(
                        tp.action(click_antigo, self._click)
                    )
                    if movimento_oconteceu:
                        self.movimento = None
                        movimentado = True

                if not movimentado:
                    self.atualiza_movimentos(self._click)
                else:
                    # TODO isso não deveria ser resposabilidade de xadrez
                    self.tabuleiro.vez = not self.tabuleiro.vez

                self._atualizacao = True
            case pg.event.EventType(type=pg.KEYDOWN, key=pg.K_ESCAPE):
                self._escape = True
示例#3
0
    def get_movimentos_simples(
        self, tabuleiro: tp.board, flags: list, pos: tp.coord
    ) -> tp.movements:
        res = movements_vazio()
        linha_promocao = 0 if self.cor else 7

        i, j = pos
        i += -1 if self.cor else 1
        if tp.coord.valida_componente(i) and tabuleiro[i][j] is None:
            if i == linha_promocao:
                res[i][j] = Promocao(tp.action(pos, tp.coord(i, j)), self.cor)
            else:
                res[i][j] = Avanco(self.cor, pos)

            ii = i - 1 if self.cor else i + 1
            avanco_duplo = (
                not self.movimentou
                and tp.coord.valida_componente(ii)
                and tabuleiro[ii][j] is None
            )
            if avanco_duplo:
                if i == linha_promocao:
                    res[ii][j] = Promocao(
                        tp.action(pos, tp.coord(ii, j)), self.cor
                    )
                else:
                    res[ii][j] = AvancoDuplo(
                        self.cor,
                        tp.pathaction(pos, tp.coord(i, j), tp.coord(ii, j)),
                    )

        i, j = pos
        i += -1 if self.cor else 1
        if tp.coord(i, j - 1).valida():
            res[i][j - 1] = self._criar_captura(
                tabuleiro, flags, tp.action(pos, tp.coord(i, j - 1))
            )
        if tp.coord(i, j + 1).valida():
            res[i][j + 1] = self._criar_captura(
                tabuleiro, flags, tp.action(pos, tp.coord(i, j + 1))
            )

        return res
示例#4
0
def calcula_direcao(
    res: tp.movements,
    tabuleiro: tp.board,
    pos: tp.coord,
    direcoes: tuple[tp.direction, ...],
    cor: bool,
    rei: bool = False,
) -> None:
    for direcao in direcoes:
        p = pos + direcao
        while p.valida():
            i, j = p
            peca = tabuleiro[i][j]
            if peca is None:
                res[i][j] = Movimento(tp.action(pos, tp.coord(i, j)), rei)
            else:
                if cor != peca.cor:
                    res[i][j] = Movimento(tp.action(pos, tp.coord(i, j)), rei)
                break  # Se a casa não está vazia, não tem porquê olhar adiante
            p += direcao
示例#5
0
 def get_movimentos(self, tabuleiro: tp.board, flags: list,
                    pos_rei: tp.coord, pos: tp.coord) -> tp.movements:
     """
     :param flags: flags do tabuleiro
     :param pos: posição da peça, cujos movimentos estão sendo calculados
     :return: matriz movements com todos os movimentos legais
     """
     res = movements_vazio()
     movimentos = self.get_movimentos_simples(tabuleiro, flags, pos)
     for i, linha in enumerate(movimentos):
         for j, mov in enumerate(linha):
             teste = testar_movimento(tabuleiro, flags, pos_rei,
                                      tp.action(pos, tp.coord(i, j)))
             res[i][j] = mov if teste else None
     return res
示例#6
0
    def _criar_captura(
        self, tabuleiro: tp.board, flags: list, acao: tp.action
    ) -> Movimento | None:
        i, j = acao.nova_pos
        promocao = 0 if self.cor else 7

        capturada = tabuleiro[i][j]
        if capturada is not None and capturada.cor != self.cor:
            if i == promocao:
                return Promocao(acao, self.cor)
            else:
                return Movimento(acao)
        else:
            enpassant = self._get_enpassant(flags, acao.nova_pos)
            if enpassant is not None:
                meio, final = enpassant
                return EnPassant(tp.action(acao.pos, final), meio)
            else:
                return None
示例#7
0
    def get_movimentos_simples(self, tabuleiro: tp.board, flags: list,
                               pos: tp.coord) -> tp.movements:
        # TODO Cuidado com xeque
        res = movements_vazio()
        i, j = pos

        # Posições a verificar
        DIRECOES = (
            # Casas acima do rei
            tp.direction(-1, -1),
            tp.direction(-1, 0),
            tp.direction(-1, 1),
            # Casas do meio
            tp.direction(0, -1),
            tp.direction(0, 1),
            # Casas abaixo do rei
            tp.direction(1, -1),
            tp.direction(1, 0),
            tp.direction(1, 1),
        )

        for direcao in DIRECOES:
            nova_pos = pos + direcao
            if nova_pos.valida():
                m, n = nova_pos
                res[m][n] = self._criar_movimento(tabuleiro,
                                                  tp.action(pos, nova_pos))

        # Verifica se é possível fazer o Roque
        if not self.movimentou:
            torre = tabuleiro[i][0]
            eh_realmente_torre = (torre is not None
                                  and isinstance(torre, Torre)
                                  and torre.movimentou)
            if eh_realmente_torre:
                # TODO verifica se deixa o rei em xeque ou passa em casas em xeque

                pecas_entre = False
                for jj in range(1, j):
                    pecas_entre = pecas_entre or tabuleiro[i][jj] is not None

                tab = board_copia(tabuleiro)
                tab[i][3] = Rei(self.cor)
                tab[i][4] = None
                xeque = testar_xeque(tab, flags, tp.coord(i, 3))

                if not xeque:
                    tab = board_copia(tabuleiro)
                    tab[i][2] = Rei(self.cor)
                    tab[i][4] = None
                    xeque = testar_xeque(tab, flags, tp.coord(i, 2))

                if not pecas_entre and not xeque:
                    res[i][j - 2] = Roque(
                        tp.action(tp.coord(i, j), tp.coord(i, j - 2)),
                        tp.action(tp.coord(i, 0), tp.coord(i, j - 1)),
                    )

            torre = tabuleiro[i][7]
            eh_realmente_torre = (torre is not None
                                  and isinstance(torre, Torre)
                                  and torre.movimentou)
            if eh_realmente_torre:
                pecas_entre = False
                for jj in range(j + 1, 7):
                    pecas_entre = pecas_entre or tabuleiro[i][jj] is not None

                if not pecas_entre:
                    res[i][j + 2] = Roque(
                        tp.action(tp.coord(i, j), tp.coord(i, j + 2)),
                        tp.action(tp.coord(i, 7), tp.coord(i, j + 1)),
                    )

        return res
示例#8
0
 def executar(self, tabuleiro: tp.board, flags: list):
     i, j = self.pos
     i += -1 if self.cor else 1
     mover_peca(tabuleiro, tp.action(self.pos, tp.coord(i, j)))