dimanche 19 novembre 2017

JavaScript: Cleaning up an Array Path

I have code that makes an array path using JavaScript. I cannot post all of the code because it is WAY too long; however, I can show you snippets of the code so you can see how I am making the paths. Here is the snippet of code.

    var MazeComplete = false;
    var RV1 = null;
    var RV2 = null;
    while(MazeComplete === false){
        //Get Maze Blocks.
        RV1 = Math.random()*(MazeArray.length)|0;
        RV2 = Math.random()*(MazeArray.length)|0;

        //No dead Or Used Values
        if(MazeArray[RV1][RV2] == 0){
            MazeArray[RV1][RV2] = 3;
        }else if((MazeArray[RV1][RV2] == MazeArray[0][0] && MazeArray[0][0] == 0) || 
                 (MazeArray[RV1][RV2] == MazeArray[0][9] && MazeArray[0][9] == 0) ||
                 (MazeArray[RV1][RV2] == MazeArray[9][0] && MazeArray[9][0] == 0) ||
                 (MazeArray[RV1][RV2] == MazeArray[9][9] && MazeArray[9][9] == 0)){
            MazeArray[RV1][RV2] = 0;
        }else{
            continue;
        }
        //[0][0]
        if(MazeArray[RV1][RV2] == MazeArray[0][0] && (MazeArray[0][0] == 0 && (MazeArray[1][0] == 2 || MazeArray[0][1] == 2))){
            //Setting Highs.
            MazeArray[0][0] = 2;
        }
        //[0][1]
        else if(MazeArray[RV1][RV2] == MazeArray[0][1] && (MazeArray[0][0] == 2 || MazeArray[0][2] == 2 || MazeArray[1][1] == 2)){
            MazeArray[0][1] = 2;
        }
        //[0][2]
        else if(MazeArray[RV1][RV2] == MazeArray[0][2] && (MazeArray[0][1] == 2 || MazeArray[0][3] == 2 || MazeArray[1][2] == 2)){
            MazeArray[0][2] = 2;
        }
        //[0][3]
        else if(MazeArray[RV1][RV2] == MazeArray[0][3] && (MazeArray[0][2] == 2 || MazeArray[0][4] == 2 || MazeArray[1][3] == 2)){
            MazeArray[0][3] = 2;
        }
        //[0][4]
        else if(MazeArray[RV1][RV2] == MazeArray[0][4] && (MazeArray[0][3] == 2 || MazeArray[0][5] == 2 || MazeArray[1][4] == 2)){
            MazeArray[0][4] = 2;
        }
        //[0][5]
        else if(MazeArray[RV1][RV2] == MazeArray[0][5] && (MazeArray[0][4] == 2 || MazeArray[0][6] == 2 || MazeArray[1][5] == 2)){
            MazeArray[0][5] = 2;
        }
        ...
        else{
            MazeArray[RV1][RV2] = 0;
        }

        //Maze Ending.
        if((MazeArray[0][0] == 2 || MazeArray[0][9] == 2) && 
           (MazeArray[9][0] == 2 || MazeArray[9][1] == 2 || MazeArray[9][2] == 2 || MazeArray[9][3] == 2 || MazeArray[9][4] == 2 || MazeArray[9][5] == 2 || MazeArray[9][6] == 2 || MazeArray[9][7] == 2 || MazeArray[9][8] == 2 || MazeArray[9][9] == 2)){
            MazeComplete = true;
        }else if((MazeArray[9][0] == 2 || MazeArray[9][9] == 2) && 
           (MazeArray[0][0] == 2 || MazeArray[0][1] == 2 || MazeArray[0][2] == 2 || MazeArray[0][3] == 2 || MazeArray[0][4] == 2 || MazeArray[0][5] == 2 || MazeArray[0][6] == 2 || MazeArray[0][7] == 2 || MazeArray[0][8] == 2 || MazeArray[0][9] == 2)){
            MazeComplete = true;
        }else{
            continue;
        }
    }

I will, also, explain how the code functions. It is a 10x10 Array path generator. I up 10 arrays into one big array, I can have 10 rows of 10. It starts from one corner of the array and flows to the opposite side. of the array. It only hits the opposite side once. The array generator is going to act as the "Back-Code" to my html elements. If an output value (i.e. [0][5]) is 2, then it is a high value and is part of the path; likewise, if it is 0, it is not part of the path.

Now I will show you the first three outputs that my code produced. Keep in mind that they were COMPLETELY random. I did not alt them in ANY way. Here are the outputs.

Output 1

[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 0, 0, 0]
[2, 0, 0, 0, 0, 2, 2, 0, 0, 0]
[2, 0, 2, 2, 2, 2, 0, 0, 0, 0]
[2, 0, 0, 2, 2, 2, 2, 2, 2, 0]
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0]
[2, 0, 2, 2, 2, 2, 0, 2, 2, 0]
[2, 2, 2, 2, 2, 2, 2, 0, 0, 0]
[2, 2, 2, 2, 2, 2, 2, 0, 0, 0]
[2, 2, 2, 2, 2, 2, 2, 2, 0, 0]

It starts at [9][0] and ends at [0][5].

Output 2

[0, 0, 0, 0, 0, 0, 0, 2, 0, 0]
[0, 0, 0, 0, 0, 2, 0, 2, 2, 0]
[0, 0, 0, 0, 0, 2, 2, 2, 2, 2]
[0, 0, 0, 0, 0, 0, 2, 2, 2, 2]
[0, 0, 0, 0, 0, 0, 2, 2, 2, 0]
[0, 0, 0, 0, 2, 0, 2, 2, 2, 2]
[0, 2, 0, 2, 2, 2, 2, 2, 2, 2]
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[0, 0, 2, 2, 2, 2, 2, 2, 2, 2]
[0, 2, 2, 2, 2, 2, 2, 2, 2, 2]

It starts at [9][9] and ends at [0][7].

Output 3

[2, 2, 2, 2, 2, 2, 2, 2, 2, 0]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 2, 2, 0, 2, 2]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[2, 2, 2, 2, 2, 0, 2, 0, 2, 2]
[0, 2, 2, 0, 2, 2, 0, 0, 0, 2]
[0, 0, 2, 2, 2, 0, 0, 0, 0, 2]
[0, 0, 0, 0, 2, 2, 2, 0, 2, 2]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0]

It starts at [0][0] and ends at [9][4].

The problem with the outputs is it doesn't have any algorithm to make better paths. I only know how to code a random path from one corner to the bottom. I want to know how to clean up the path so it looks more like a "maze."

For instance, I would like a path that looks cleaner and could be easier to distinguish as a path. I will edit the third output to make it cleaner, so I can show you what kind of output I would like. Here is the adjusted third output:

[2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 2, 2, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 2, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 2, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 2, 0, 0, 0, 0, 0]

(Adjusted from Output 3).

The only way I see this being possible is if I go through each value in the array and putting a restriction on what values can and can't be hit, but I can't do that in a sensible manner (a manner that doesn't require me to write out thousands of line of code). I tried doing it earlier using a tactic I call "diagonal cancellation", but it didn't work because, depending on which corner you start from, a value could be illogical to be it from on corner but logical to hit from another.

An example is [0][1]. If you are starting from [0][0], you can get a maze goes to [0][1]; therefore [9][0] would be an illogical value to allow because it will make the maze unclean. So you would declare it a "dead value" by putting a 1 in its place.

[2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

This seems like a good step for making a clean path, but what if you start from [9][0]? Then, you couldn't use [1][0] because it was declared dead bythe first value. So, you would, instead, have to make rules for each value from each starting corner, which will take a LONG time and effort. I don't have the patience to do this nor do I have enough time or knowledge.

How can I manipulate Output 1, 2, or 3 to get a more "cleaner" path (Note: the output doesn't have to be as clean as the adjusted one that I did, but it has to be somewhat clean)?

And, if you want my full code to further help you understand I how programmed the paths, I will post the link to it. I realize people refrain from clicking links for obvious reasons, but I assure you that it is my code, and only my code.

Link to the full code: http://ift.tt/2zXnXiU




Aucun commentaire:

Enregistrer un commentaire