Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: SilverTiger (posted 2014-02-17 20:05:14, viewed 73 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121  
/** finds the Path from (startX, startY) to (endX,endY)
 * @param startX starting X-Coordinate
 * @param startY starting Y-Coordinate
 * @param endX target X-Coordinate
 * @param endY target Y-Coordinate
 * @param walkableTiles Map representation of walkable tiles
 * @return path array with x & y coordinates
 */
private int[][] findPath(int startX, int startY, int endX, int endY, boolean[][] walkable {
    /* first we get the the width and length of the map */
    int length = walkable.length;
    int width = walkable[0].length;

    /* now we initialize an int array and fill it with -1 */
    int[][] distance = new int[length][width];
    for(int i=0; i < distance.length; i++) {
        for(int j=0; j < distance[i].length; j++) {
            distance[i][j] = -1;
        }
    }

    /* our target point is (endX,endY) so there is distance = 0 */
    distance[endX][endY] = 0;
    /* we need to store the steps from the end point to the next Tiles in a variable */
    int steps = 1;

    /* now we'll mark every tile how much steps there are from the current Point to the end point until we hit the start point*/
    while(distance[startX][startY] = -1) {
        for(int i=0; i < distance.length; i++) {
            for(int j=0; j < distance[i].length; j++) {
                /* check if the current Tile is have the distance of the last marked Tile */
                if(distance[i][j] == steps - 1) {
                    /* check if the Tile right from the current Tile is valid */
                    if(i < width - 1) {
                        /* check if that Tile is not marked and if it is walkable*/
                        if(distance[i+1][j] == -1 && walkable[i+1][j]) {
                            /* not marked & walkable, so we can mark it */
                            distance[i+1][j] = steps;
                        }
                    }

                    /* check if the Tile left from the current Tile is valid */
                    if(i > 0) {
                        /* check if that Tile is not marked and if it is walkable*/
                        if(distance[i-1][j] == -1 && walkable[i-1][j]) {
                            /* not marked & walkable, so we can mark it */
                            distance[i-1][j] = steps;
                        }
                    }

                    /* check if the Tile underneath the current Tile is valid */
                    if(j < length - 1) {
                        /* check if that Tile is not marked and if it is walkable*/
                        if(distance[i][j+1] == -1 && walkable[i][j+1]) {
                            /* not marked & walkable, so we can mark it */
                            distance[i][j+1] = steps;
                        }
                    }

                    /* check if the Tile above the current Tile is valid */
                    if(j > 0) {
                        /* check if that Tile is not marked and if it is walkable*/
                        if(distance[i][j-1] == -1 && walkable[i][j-1]) {
                            /* not marked & walkable, so we can mark it */
                            distance[i][j-1] = steps;
                        }
                    }
                }
            }
        }
        /* increment steps after each map check */
        steps++;
    }

    /* now we have checked every Tile, so we can start making a path */
    int[][] path = new int[steps][2];
    /* we start with the start point */
    path[0][0] = startX;
    path[0][1] = startY;

    int i = 0;
    /* now we go on until we hit the end point */
    while(path[i][0] != endX && path[i][1] != endY) {
        /* get current tile coordinates */
        int x = path[i][0];
        int y = path[i][1];
        /* we have everything from the current tile so increment i */
        i++;

        /* check if the field right from the current field is one step away */
        if(steps[x+1][y] == steps[x][y] - 1) {
            /* that tile is one step away, so take it into the path */
            path[i][0] = x+1;
            path[i][1] = y;
        }

        /* check if the field left from the current field is one step away */
        else if(steps[x-1][y] == steps[x][y] - 1) {
            /* that tile is one step away, so take it into the path */
            path[i][0] = x-1;
            path[i][1] = y;
        }

        /* check if the field underneath the current field is one step away */
        else if(steps[x][y+1] == steps[x][y] - 1) {
            /* that tile is one step away, so take it into the path */
            path[i][0] = x;
            path[i][1] = y+1;
        }

        /* check if the field above the current field is one step away */
        else if(steps[x][y-1] == steps[x][y] - 1) {
            /* that tile is one step away, so take it into the path */
            path[i][0] = x;
            path[i][1] = y-1;
        }
    }
    /* now we should have the finished path array */
    // note that path[i][0] has the X-Coordinate and path[i][1] has the Y-Coordinate!
    return path;
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (22 views)
2014-04-15 03:46:01

UprightPath (38 views)
2014-04-14 17:39:50

UprightPath (20 views)
2014-04-14 17:35:47

Porlus (36 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!