Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1.         private void findPath(int startX, int startY, int endX, int endY)
  2.         {
  3.             pathmatrix = new int[numberOfTilesInHeight,numberOfTilesInWidth];
  4.  
  5.             for (int i = 0; i < numberOfTilesInHeight; i++)
  6.                 for (int j = 0; j < numberOfTilesInWidth; j++)
  7.                 {
  8.                     if (backMatrix[i, j] != Settings.Default.idWall)
  9.                         pathmatrix[i, j] = -2;
  10.                     else
  11.                         pathmatrix[i, j] = -1;
  12.                 }
  13.  
  14.             pathmatrix[startX, startY] = 0;
  15.  
  16.             bool changed = true;
  17.             int step = 0;
  18.             while ((changed) && (pathmatrix[endX, endY] == -2))
  19.             {
  20.                 changed = false;
  21.                 step++;
  22.                 for (int i = 0; i < numberOfTilesInHeight; i++)
  23.                 {
  24.                     for (int j = 0; j < numberOfTilesInWidth; j++)
  25.                     {
  26.                         if (pathmatrix[i, j] == step - 1)
  27.                         {
  28.                             if ((i - 1 >= 0) && (pathmatrix[i - 1, j] == -2) && (pathmatrix[i - 1, j] != -1))
  29.                             {
  30.                                 pathmatrix[i - 1, j] = step;
  31.                                 changed = true;
  32.                             }
  33.  
  34.                             if ((i + 1 < numberOfTilesInHeight) && (pathmatrix[i + 1, j] == -2) &&
  35.                                 (pathmatrix[i + 1, j] != -1))
  36.                             {
  37.                                 pathmatrix[i + 1, j] = step;
  38.                                 changed = true;
  39.                             }
  40.  
  41.                             if ((j - 1 >= 0) && (pathmatrix[i, j - 1] == -2) && (pathmatrix[i, j - 1] != -1))
  42.                             {
  43.                                 pathmatrix[i, j - 1] = step;
  44.                                 changed = true;
  45.                             }
  46.  
  47.                             if ((j + 1 < numberOfTilesInWidth) && (pathmatrix[i, j + 1] == -2) &&
  48.                                 (pathmatrix[i, j + 1] != -1))
  49.                             {
  50.                                 pathmatrix[i, j + 1] = step;
  51.                                 changed = true;
  52.                             }
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.         private void makeStep(int x, int y, int pathid)
  60.         {
  61.             if (pathmatrix[x, y] == 0) return;
  62.  
  63.             int curStep = pathmatrix[x, y];
  64.  
  65.             if ((x - 1 >= 0) && (pathmatrix[x - 1, y] == curStep - 1))
  66.             {
  67.                 frontMatrix[x, y] = pathid;
  68.                 x -= 1;
  69.                 curStep++;
  70.                 makeStep(x, y, pathid);
  71.                 path.Add(new Point(x, y));
  72.                 return;
  73.             }
  74.  
  75.             if ((y - 1 >= 0) && (pathmatrix[x, y - 1] == curStep - 1))
  76.             {
  77.                 frontMatrix[x, y] = pathid;
  78.                 y -= 1;
  79.                 curStep++;
  80.                 makeStep(x, y, pathid);
  81.                 path.Add(new Point(x, y));
  82.                 return;
  83.             }
  84.  
  85.             if ((x + 1 < numberOfTilesInHeight) && (pathmatrix[x + 1, y] == curStep - 1))
  86.             {
  87.                 frontMatrix[x, y] = pathid;
  88.                 x += 1;
  89.                 curStep++;
  90.                 makeStep(x, y, pathid);
  91.                 path.Add(new Point(x, y));
  92.                 return;
  93.             }
  94.  
  95.             if ((y + 1 < numberOfTilesInWidth) && (pathmatrix[x, y + 1] == curStep - 1))
  96.             {
  97.                 frontMatrix[x, y] = pathid;
  98.                 y += 1;
  99.                 curStep++;
  100.                 makeStep(x, y, pathid);
  101.                 path.Add(new Point(x, y));
  102.                 return;
  103.             }
  104.         }
Add Comment
Please, Sign In to add comment