Find Better Way to Create Paths in JavaScript for

2019-07-23 12:43发布

问题:

I have been working on algorithm for a week at last I have successfully made an algorithm to create paths that captures opponent pieces, and then find a Maximum captures Path on 8x8 board.

I have created an array of the following move captures.

var moves = [[37 , 69] , [69 , 101] , [ 101 , 99] , [69 ,71], [ 69 , 67] , [67 , 99] , [99 , 101] ,  [71 ,103] ]

Note: Can't capture downwards respective to black.

Here I don't know the end point so that's why I can use Search Algorithms such as BFS, DFS, e.t.c.

So I decided to first find all the possible captures which are added in moves array hard coded for now.

Then created algorithm which create paths but the complexity of algorithm is much higher but I need the same work with better algorithm.

Here is my Algorithm

var getPaths = GetPaths(moves);
function GetPaths(moves) {
    var paths = [];
    for (var x = 0 ; x < moves.length ; x++) {
        var path = [];
        for (var y = x + 1 ; y < moves.length ; y++) {
            if (moves[x][1] == moves[y][0]) {
                if (!(path.includes(moves[x]))) {
                    path.push(moves[x]);
                }
                var data = [moves[y][0], moves[y][1]];
                path.push(data);
            }
        }
        if (path.length > 0) paths.push(path);



    }

    var newpaths = [];

    var nextRow = paths[0];
    var len = paths.length;


    for (var h = 1 ; h < nextRow.length; h++) {

        for (var j = 1 ; j < len ; j++) {
            var newpath = [];
            if (isInArray(nextRow[h], paths[j][0])) {

                newpath.push(nextRow[0]);
                var nextfound = false;
                for (var k = j + 1 ; k < paths.length ; k++) {
                    if (isInArray(paths[j][paths[j].length - 1], paths[k][0])) {
                        newpath.push(paths[j][0]);
                        if (paths[k][0][0] - paths[k][0][1] != -(paths[k][1][0] - paths[k][1][1])) {
                            newpath.push(paths[k]);
                        } else {
                            newpath.push(paths[k][0]);
                        }

                        nextfound = true;
                    }

                }
                if (!nextfound) {
                    newpath.push(paths[j]);
                }

            }
            if (newpath.length > 0) {
                newpaths.push(newpath);
            }
        }

    }

    return newpaths;
}

var maxPath = FindMaximumCapture(getPaths);
function FindMaximumCapture(totalPaths) {
    var max = totalPaths[0].toString().length;

    var index = 0;
    for (var count = 1 ; count < totalPaths.length ; count++) {
        if (max < totalPaths[count].toString().length) {
            max = totalPaths[count].toString().length;
            index = count;
        }
    }
    return totalPaths[index];
}
function isInArray(value, array) {
    return array.indexOf(value[0]) > -1 && array.indexOf(value[1]) > -1;
}

Here is the link of code in JSFiddle