Exemplo n.º 1
0
    def putOnBoard(self, match, piece):
        available_pos = match.board.unusedPositions()
        fallback_i = random.randint(0, len(available_pos) - 1)
        better_pos = available_pos[fallback_i]
        alpha, beta = 0, Minimax.EVAL_WIN

        if len(available_pos) >= Minimax.MINIMAX_1_UNTIL and \
                self.max_depth > 1:
            return Minimax(1).putOnBoard(match, piece)
        if len(available_pos) >= Minimax.MINIMAX_2_UNTIL and \
                self.max_depth > 2:
            return Minimax(2).putOnBoard(match, piece)
        if len(available_pos) >= Minimax.MINIMAX_3_UNTIL and \
                self.max_depth > 3:
            return Minimax(3).putOnBoard(match, piece)

        for pos in available_pos:
            eval = self.min_evaluation(
                match.board, piece, pos,
                alpha, beta, 1
            )

            if eval > alpha:
                alpha = eval
                better_pos = pos

        ui.showPlayer(match.active_player)
        ui.showSelectedPosition(better_pos)

        return better_pos
Exemplo n.º 2
0
    def putOnBoard(self, match, piece):
        available_pos = match.board.unusedPositions()
        i = random.randint(0, len(available_pos) - 1)

        ui.showPlayer(match.active_player)
        ui.showSelectedPosition(available_pos[i])

        return available_pos[i]
Exemplo n.º 3
0
    def putOnBoard(self, match, piece):
        available_pos = match.board.unusedPositions()

        if len(available_pos) >= 15:
            return Random().putOnBoard(match, piece)

        board_values = get_board_values(match.board.board)
        better_color = maximize_property(
            match.board.board,
            {"propriety": "color", "value": piece.color},
            board_values
        )
        better_height = maximize_property(
            match.board.board,
            {"propriety": "height", "value": piece.height},
            board_values
        )
        better_shape = maximize_property(
            match.board.board,
            {"propriety": "shape", "value": piece.shape},
            board_values
        )
        better_state = maximize_property(
            match.board.board,
            {"propriety": "state", "value": piece.state},
            board_values
        )

        val_max = max((
            better_color["value"],
            better_height["value"],
            better_shape["value"],
            better_state["value"]
        ))

        final_pos = None
        for best in (better_color, better_height,
                     better_shape, better_state):
            if best["value"] == val_max:
                final_pos = best["position"]

        if final_pos is None:
            i = random.randint(0, len(available_pos) - 1)
            final_pos = available_pos[i]

        ui.showPlayer(match.active_player)
        ui.showSelectedPosition(final_pos)

        return final_pos
Exemplo n.º 4
0
    def selectPiece(self, match):
        available_pieces = match.board.unusedPieces()
        if len(available_pieces) < 1:
            # trick for tournament, when board is full
            return Piece({
                "color": "blue",
                "height": "small",
                "shape": "round",
                "state": "solid"
            })

        i = random.randint(0, len(available_pieces) - 1)

        ui.showPlayer(match.active_player)
        ui.showSelectedPiece(available_pieces[i])

        return available_pieces[i]
Exemplo n.º 5
0
    def selectPiece(self, match):
        available_pieces = match.board.unusedPieces()
        if len(available_pieces) < 1:
            # trick for tournament, when board is full
            return Piece({
                "color": "blue", "height": "small",
                "shape": "round", "state": "solid"
            })

        available_pos = match.board.unusedPositions()
        fallback_i = random.randint(0, len(available_pieces) - 1)
        chosen_piece = available_pieces[fallback_i]
        alpha, beta = 0, Minimax.EVAL_WIN

        if len(available_pieces) >= Minimax.MINIMAX_1_UNTIL and \
                self.max_depth > 1:
            return Minimax(1).selectPiece(match)
        if len(available_pieces) >= Minimax.MINIMAX_2_UNTIL and \
                self.max_depth > 2:
            return Minimax(2).selectPiece(match)
        if len(available_pieces) >= Minimax.MINIMAX_3_UNTIL and \
                self.max_depth > 3:
            return Minimax(3).selectPiece(match)

        for piece in available_pieces:
            alpha = 0

            for pos in available_pos:
                eval = self.min_evaluation(
                    match.board, piece, pos,
                    alpha, beta, 1
                )

                alpha = max(alpha, eval)

            if alpha < beta:
                beta = alpha
                chosen_piece = piece

        ui.showPlayer(match.active_player)
        ui.showSelectedPiece(chosen_piece)

        return chosen_piece
Exemplo n.º 6
0
 def putOnBoard(self, match, piece):
     ui.showPlayer(match.active_player)
     return ui.askPosition(match.board.unusedPositions())
Exemplo n.º 7
0
 def selectPiece(self, match):
     ui.showPlayer(match.active_player)
     return ui.askSelectPiece(match.board.unusedPieces())