コード例 #1
0
ファイル: ai.py プロジェクト: JackJoestar/Checkers-Project-AI
def minimax(depth, color, board):
    if depth == DIFFICULTY:
        moves = weighBoard(board, depth)
        if color:
            black_moves = moves[1]
            # Min
            # Returns move best for black
            mini = None
            for move in black_moves:
                if mini is None:
                    mini = move
                elif mini.weight > move.weight:
                    mini = move
            return mini
        else:
            white_moves = moves[0]
            # Max
            # Returns move best for white
            maxi = None
            for move in white_moves:
                if maxi is None:
                    maxi = move
                elif maxi.weight < move.weight:
                    maxi = move
            return maxi

    best_move = None
    if color:
        # Min
        # Evaluates future impact of moves and ranks them accordingly
        black_moves = findMoves(board, True) + findJumps(board, True)
        if best_move is not None:
            return best_move

        for move in black_moves:
            copy = model.copyBoard(board)
            val = minimax(depth + 1, False, move.apply(copy))
            if best_move is None or val.weight < best_move.weight or move.type == "Jump":
                best_move = val

    else:
        # Evaluates future impact of moves and ranks them accordingly
        white_moves = findMoves(board, False) + findJumps(board, False)
        for move in white_moves:
            val = minimax(depth + 1, True,
                          move.apply(model.copyBoard(board)))
            if best_move is None or val.weight > best_move.weight:
                best_move = val

    return best_move
コード例 #2
0
ファイル: ai.py プロジェクト: JackJoestar/Checkers-Project-AI
def enemyJump(board, move, color):
    enemy_jumps = findJumps(move.apply(model.copyBoard(board)), not color)
    for jump in enemy_jumps:
        for victim in jump.jumped:
            if victim.checker.id == move.checker.id:
                return True
    return False
コード例 #3
0
ファイル: ai.py プロジェクト: JackJoestar/Checkers-Project-AI
def doesMoveEndProtect(board, move, color):
    jumps = findJumps(move.apply(model.copyBoard(board)), not color)
    for jump in jumps:
        for otherpiece in jump.otherpieces:
            if otherpiece.x/62.5 == move.checker.x and otherpiece.y/62.5 == move.checker.y:
                return True
        if jump.piece.x/62.5 == move.checker.x and jump.piece.y/62.5 == move.checker.y:
            return True

    return False
コード例 #4
0
ファイル: ai.py プロジェクト: JackJoestar/Checkers-Project-AI
def findJumps(board, color, old=None, depth=0):
    jumps = []
    for piece in board.flat:
        if piece.checker is None or piece.checker.black != color:
            continue
        options = []
        dirs = []
        if piece.checker.king:
            pieces = findNeighbor(board, piece.x / 62.5, piece.y / 62.5)
        elif color:
            pieces = findNeighbor(board, piece.x / 62.5,
                                  piece.y / 62.5, down=True)
        elif not color:
            pieces = findNeighbor(board, piece.x / 62.5,
                                  piece.y / 62.5, up=True)
        for new_piece in pieces:
            dir = []
            if new_piece.checker is None or new_piece.checker.black == color:
                continue
            if color is True or piece.checker.king:
                dir.append(checkNeighbor(new_piece.x / 62.5, new_piece.y /
                                         62.5, piece.x / 62.5, piece.y / 62.5, down=True))
            if color is False or piece.checker.king:
                dir.append(checkNeighbor(new_piece.x / 62.5, new_piece.y /
                                         62.5, piece.x / 62.5, piece.y / 62.5, up=True))
            for direction in dir:
                if direction[0] != -1:
                    options.append(new_piece)
                    dirs.append(direction[0])
        x = 0
        for option in options:
            new_piece = None
            if option.x / 62.5 == 0 or option.x / 62.5 == 7 or option.y / 62.5 == 0 or option.y / 62.5 == 7:
                x += 1
                continue
            if dirs[x] == 0:
                new_piece = board[int(option.x / 62.5) + 1,
                                  int(option.y / 62.5) + 1]
                x += 1
            elif dirs[x] == 1:
                new_piece = board[int(option.x / 62.5 - 1),
                                  int(option.y / 62.5 + 1)]
                x += 1
            elif dirs[x] == 2:
                new_piece = board[int(option.x / 62.5 + 1),
                                  int(option.y / 62.5 - 1)]
                x += 1
            elif dirs[x] == 3:
                new_piece = board[int(option.x / 62.5 - 1),
                                  int(option.y / 62.5 - 1)]
                x += 1
            if new_piece.checker is None:
                move = Move(piece.checker, new_piece, "Jump")
                move.jumped.append(option)
                new_board = model.copyBoard(board)
                move.apply(new_board)

                if depth < 2:
                    new_jumps = findJumps(new_board, color, option, depth + 1)
                    extra_jump = False
                    for jump in new_jumps:
                        if jump.checker.id == piece.checker.id:
                            extra_jump = True
                            jump.jumped.append(option)
                            jump.otherpieces.append(new_piece)
                            if old is not None:
                                jump.jumped.append(old)
                            jump.checker = piece.checker
                            jumps.append(jump)
                    if not extra_jump:
                        jumps.append(move)
    return jumps