Exemplo n.º 1
0
    def __init__(self):
        """O construtor."""

        self.jogadores = ('rei preto', 'rei branco')
        self.pecas = {'rei preto': 'p', 'rei branco': 'b', 'neutras': 'n'}
        self.size = BOARDSIZE
        tabuleiro_inicial = {
            (2, 1): 'n',
            (2, 2): 'n',
            (2, 4): 'n',
            (2, 5): 'n',
            (3, 1): 'p',
            (3, 2): 'n',
            (3, 4): 'n',
            (3, 5): 'b',
            (4, 1): 'n',
            (4, 2): 'n',
            (4, 4): 'n',
            (4, 5): 'n'
        }

        movimentos_iniciais = [((3, 1), (2, 1)), ((3, 1), (3, 2)),
                               ((3, 1), (4, 1))]

        self.initial = jogos_iia.GameState(to_move=self.jogadores[0],
                                           utility=0,
                                           board=Board(0, tabuleiro_inicial),
                                           moves=movimentos_iniciais)
Exemplo n.º 2
0
    def __init__(self):
        board = {(2, 1): 'n', (2, 2): 'n', (2, 4): 'n', (2, 5): 'n',
                 (3, 1): 'p', (3, 2): 'n', (3, 4): 'n', (3, 5): 'b',
                 (4, 1): 'n', (4, 2): 'n', (4, 4): 'n', (4, 5): 'n'}

        moves = [((3, 1), (2, 1)), ((3, 1), (3, 2)), ((3, 1), (4, 1))]
        self.initial = jogos_iia.GameState(self.jogadores[0], 0, (0, board), moves)
Exemplo n.º 3
0
 def result(self,state,move) :
     """
     Requires: 'move' é uma jogada válida no estado dado ('state')
     """
     accao,peca = move
     jogador = state.to_move
     adversario = JogoPeoes.outro_jogador(jogador)
     tabuleiro = deepcopy(state.board)
     tabuleiro[jogador].remove(peca)
     if accao == 'avança' :
         x = peca[0]+self.sentido[jogador]
         y = peca[1]
         tabuleiro[jogador].append((x,y))
     elif accao == 'come-esq' :
         x = peca[0]+self.sentido[jogador]
         y = peca[1]+self.sentido[jogador]
         tabuleiro[jogador].append((x,y))
         tabuleiro[adversario].remove((x,y))
     else : # come-dir
         x = peca[0]+self.sentido[jogador]
         y = peca[1]-self.sentido[jogador]
         tabuleiro[jogador].append((x,y))
         tabuleiro[adversario].remove((x,y))
     estado = jogos_iia.GameState(to_move = JogoPeoes.outro_jogador(jogador),
                        board = tabuleiro,
                        moves = self.movimentos_possiveis(tabuleiro,JogoPeoes.outro_jogador(jogador)),
                        utility = self.calcular_utilidade(tabuleiro,jogador))
     return estado
Exemplo n.º 4
0
 def result(self, state, move):
     """
 Requires: 'move' é uma jogada válida no estado dado ('state')
 """
     tabuleiro = deepcopy(state.board[1])
     jogador_atual = state.to_move
     proximo_jogador = self.outro_jogador(jogador_atual)
     num_jogadas = state.board[0]
     #print("move: {}".format(move))
     posicao_1 = self.get_posicao_atual(state.board[1], jogador_atual)
     posicao_2, posicao_3 = move
     x_1, y_1 = posicao_2
     x_2, y_2 = posicao_3
     if x_1 < x_2:
         if (x_1 + 1, y_1) in tabuleiro and tabuleiro[(x_1 + 1,
                                                       y_1)] == 'n':
             del tabuleiro[(x_1 + 1, y_1)]
             tabuleiro[(x_2 + 1, y_1)] = 'n'
         else:
             del tabuleiro[(x_1 - 1, y_1)]
             tabuleiro[(x_2 - 1, y_1)] = 'n'
     elif x_1 > x_2:
         if (x_1 - 1, y_1) in tabuleiro and tabuleiro[(x_1 - 1,
                                                       y_1)] == 'n':
             del tabuleiro[(x_1 - 1, y_1)]
             tabuleiro[(x_2 - 1, y_1)] = 'n'
         else:
             del tabuleiro[(x_1 + 1, y_1)]
             tabuleiro[(x_2 + 1, y_1)] = 'n'
     elif y_1 < y_2:
         if (x_1, y_1 + 1) in tabuleiro and tabuleiro[(x_1,
                                                       y_1 + 1)] == 'n':
             del tabuleiro[(x_1, y_1 + 1)]
             tabuleiro[(x_1, y_2 + 1)] = 'n'
         else:
             del tabuleiro[(x_1, y_1 - 1)]
             tabuleiro[(x_1, y_2 - 1)] = 'n'
     else:
         if (x_1, y_1 - 1) in tabuleiro and tabuleiro[(x_1,
                                                       y_1 - 1)] == 'n':
             del tabuleiro[(x_1, y_1 - 1)]
             tabuleiro[(x_1, y_2 - 1)] = 'n'
         else:
             del tabuleiro[(x_1, y_1 + 1)]
             tabuleiro[(x_1, y_2 + 1)] = 'n'
     if posicao_1 in tabuleiro and tabuleiro[posicao_1] == jogador_atual:
         del tabuleiro[posicao_1]
     tabuleiro[(x_2, y_2)] = jogador_atual
     novo_board = (num_jogadas + 1, tabuleiro)
     movimentos = self.movimentos_possiveis(tabuleiro, proximo_jogador)
     utilidade = self.calcular_utilidade(tabuleiro, jogador_atual,
                                         proximo_jogador, movimentos)
     estado = jogos_iia.GameState(to_move=proximo_jogador,
                                  board=novo_board,
                                  moves=movimentos,
                                  utility=utilidade)
     return estado
Exemplo n.º 5
0
 def __init__(self) :
     self.jogadores = ('brancas','pretas')
     self.sentido = {'brancas':-1,'pretas':1}        
     
     self.linhas = 3 # número de linhas
     self.cols = 3   # número de colunas
     self.objectivo = {'brancas':1,'pretas':self.linhas}
     tabuleiro_inicial = {'brancas':[(3,1),(3,2)],'pretas':[(1,2),(1,3)]}
     movs_possiveis = self.movimentos_possiveis(tabuleiro_inicial,self.jogadores[0])
     self.initial = jogos_iia.GameState(
         to_move = self.jogadores[0],
         utility = 0,
         board = tabuleiro_inicial,
         moves = movs_possiveis)
Exemplo n.º 6
0
 def __init__(self):
     self.jogadores = ('p', 'b')
     tabuleiro_inicial = {
         (2, 1): 'n',
         (2, 2): 'n',
         (2, 4): 'n',
         (2, 5): 'n',
         (3, 1): 'p',
         (3, 2): 'n',
         (3, 4): 'n',
         (3, 5): 'b',
         (4, 1): 'n',
         (4, 2): 'n',
         (4, 4): 'n',
         (4, 5): 'n'
     }
     movs_possiveis = self.movimentos_possiveis(tabuleiro_inicial,
                                                self.jogadores[0])
     self.initial = jogos_iia.GameState(to_move=self.jogadores[0],
                                        utility=0,
                                        board=(0, tabuleiro_inicial),
                                        moves=movs_possiveis)
Exemplo n.º 7
0
    def result(self, state, move):
        """Obtencao do estado que se obtem ao executar uma dada jogada num dado estado."""
        player = state.to_move
        old_tabuleiro = state.board[1]
        new_tabuleiro = {}
        ((x1, y1), (x2, y2)) = move
        peca_jogador = self.pecas[player]
        pos_jogador = find_player(old_tabuleiro, peca_jogador)
        pos_neut_old = (0, 0)

        (Vx, Vy) = (x2 - x1, y2 - y1)

        if Vy == 0:  # andou na horizontal
            if Vx > 0:  # andou para a direita
                if (x1 + 1, y1) in old_tabuleiro and old_tabuleiro[(
                        x1 + 1, y1)] == other_player(player):
                    new_tabuleiro[(x1 + 1, y1)] = peca_jogador

                elif (x1 + 1,
                      y1) in old_tabuleiro and old_tabuleiro[(x1 + 1,
                                                              y1)] == 'n':
                    pos_neut_old = (x1 + 1, y1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2 + 1, y2)] = 'n'
                else:
                    pos_neut_old = (x1 - 1, y1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2 - 1, y2)] = 'n'

            else:  # andou para a esquerda
                if (x1 - 1, y1) in old_tabuleiro and old_tabuleiro[(
                        x1 - 1, y1)] == other_player(player):
                    new_tabuleiro[(x1 - 1, y1)] = peca_jogador

                elif (x1 - 1,
                      y1) in old_tabuleiro and old_tabuleiro[(x1 - 1,
                                                              y1)] == 'n':
                    pos_neut_old = (x1 - 1, y1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2 - 1, y2)] = 'n'
                else:
                    pos_neut_old = (x1 + 1, y1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2 + 1, y2)] = 'n'

        else:  # andou na vertical
            if Vy > 0:  # andou para cima
                if (x1, y1 + 1) in old_tabuleiro and old_tabuleiro[(
                        x1, y1 + 1)] == other_player(player):
                    new_tabuleiro[(x1, y1 + 1)] = peca_jogador

                elif (x1, y1 + 1) in old_tabuleiro and old_tabuleiro[(
                        x1, y1 + 1)] == 'n':
                    pos_neut_old = (x1, y1 + 1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2, y2 + 1)] = 'n'
                else:
                    pos_neut_old = (x1, y1 - 1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2, y2 - 1)] = 'n'

            else:  # andou para baixo
                if (x1, y1 - 1) in old_tabuleiro and old_tabuleiro[(
                        x1, y1 - 1)] == other_player(player):
                    new_tabuleiro[(x1, y1 - 1)] = peca_jogador

                elif (x1, y1 - 1) in old_tabuleiro and old_tabuleiro[(
                        x1, y1 - 1)] == 'n':
                    pos_neut_old = (x1, y1 - 1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2, y2 - 1)] = 'n'
                else:
                    pos_neut_old = (x1, y1 + 1)
                    new_tabuleiro[(x2, y2)] = peca_jogador
                    new_tabuleiro[(x2, y2 + 1)] = 'n'

        board_key_list = old_tabuleiro.keys()
        for pos in board_key_list:
            if pos != pos_neut_old and pos != pos_jogador:
                new_tabuleiro[pos] = old_tabuleiro[pos]

        to_be_moves = self.possible_moves(new_tabuleiro, other_player(player))
        next_player = other_player(player)

        result = jogos_iia.GameState(to_move=next_player,
                                     utility=self.calcular_utilidade(
                                         new_tabuleiro, to_be_moves,
                                         next_player),
                                     board=(state.board[0] + 1, new_tabuleiro),
                                     moves=to_be_moves)

        return result
Exemplo n.º 8
0
    def result(self, state, move):
        jogada_1 = move[0]
        tabuleiro = state.board[1]
        jogador = self.conv_peca(state.to_move)

        tabuleiro_novo = dict(tabuleiro)

        # 1a jogada a fazer!
        pos_jogador = self.procura_jogador(tabuleiro_novo, jogador)

        del tabuleiro_novo[pos_jogador]
        tabuleiro_novo[jogada_1] = jogador
        # 1a jogada feita!

        # 2a jogada a fazer!
        jogada_2 = move[1]
        result = tuple(map(operator.sub, jogada_2, jogada_1))

        x = jogada_1[0]
        y = jogada_1[1]

        if jogada_2 in tabuleiro_novo and tabuleiro_novo[jogada_2] == self.outro_jogador_conv(jogador):
            del tabuleiro_novo[jogada_1]
            tabuleiro_novo[jogada_2] = jogador
            to_move_new = self.outro_jogador(state.to_move)
            utilidade_new = self.do_utility(tabuleiro_novo, to_move_new, [])
            jogadas = state.board[0] + 1
            new_state = jogos_iia.GameState(to_move_new, utilidade_new, (jogadas, tabuleiro_novo), [])
            return new_state

        moveu = 'x' if result[1] == 0 else 'y'
        del tabuleiro_novo[(x, y)]
        if (moveu == 'x'):
            if result[0] > 0:
                if (x + 1, y) in tabuleiro_novo:
                    del tabuleiro_novo[(x + 1, y)]
                    tabuleiro_novo[(jogada_2[0] + 1, y)] = 'n'
                else:
                    del tabuleiro_novo[(x - 1, y)]
                    tabuleiro_novo[(jogada_2[0] - 1, y)] = 'n'
            else:
                if (x - 1, y) in tabuleiro_novo:
                    del tabuleiro_novo[(x - 1, y)]
                    tabuleiro_novo[(jogada_2[0] - 1, y)] = 'n'
                else:
                    del tabuleiro_novo[(x + 1, y)]
                    tabuleiro_novo[(jogada_2[0] + 1, y)] = 'n'
        else:
            if result[1] > 0:
                if (x, y + 1) in tabuleiro_novo:
                    del tabuleiro_novo[(x, y + 1)]
                    tabuleiro_novo[(x, jogada_2[1] + 1)] = 'n'
                else:
                    del tabuleiro_novo[(x, y - 1)]
                    tabuleiro_novo[(x, jogada_2[1] - 1)] = 'n'
            else:
                if (x, y - 1) in tabuleiro_novo:
                    del tabuleiro_novo[(x, y - 1)]
                    tabuleiro_novo[(x, jogada_2[1] - 1)] = 'n'
                else:
                    del tabuleiro_novo[(x, y + 1)]
                    tabuleiro_novo[(x, jogada_2[1] + 1)] = 'n'

        tabuleiro_novo[jogada_2] = jogador
        # 2a jogada feita!
        to_move_new = self.outro_jogador(state.to_move)
        moves_new = self.do_actions(tabuleiro_novo, to_move_new)
        utilidade_new = self.do_utility(tabuleiro_novo, to_move_new, moves_new)
        jogadas = state.board[0] + 1
        new_state = jogos_iia.GameState(to_move_new, utilidade_new, (jogadas, tabuleiro_novo), moves_new)

        return new_state