# Path Finding Tiles Grid

**URL:** <https://discourse.processing.org/t/path-finding-tiles-grid/5395>\
**Category:** Coding Questions\
**Created:** [November 10, 2018, 12:04am UTC](https://discourse.processing.org/t/path-finding-tiles-grid/5395 "2018-11-10T00:04:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [November 10, 2018, 12:04am UTC](https://discourse.processing.org/t/path-finding-tiles-grid/5395/1 "2018-11-10T00:04:21Z")

</div>

I’m making a new path finding enemy, that checks the tiles around him to see if he can walk on them. Sadly he doesn’t move or moves onto the wrong square. Can anyone tell me why my code not working?  
here the main page, sry but i got a table and image so it won’t run for anyone

```auto
PImage map;
Table PathTable;
TableRow PT;
int s = 25;
ArrayList<Tiles> tiles = new ArrayList<Tiles>();
ArrayList<Enemy> enemy = new ArrayList<Enemy>();
int ty;
int j,ii;
int turn = 1;
void setup(){
  size(600,600);
  PathTable = loadTable("path.csv");
  PT = PathTable.getRow(0);
  for (int i = 0; i < 576; i++){
    ii = i;
    while (ii > 23){
      j += 1;
      ii -= 24;
    }
    ty = PT.getInt(i);
    tiles.add(new Tiles(ty,ii*s,j*s));
    j = 0;
  }
  enemy.add(new Enemy(0));
  /*PT.setInt(282,1);
  saveTable(PathTable,"path.csv");*/
  map = loadImage("m1.png");
}// setup

void draw(){
  //background(0);
  stroke(255,0,0);
  image(map,0,0);
  /*for (int i = 0; i < s; i++){
    line(i * s, 0, i * s, height);
    line(0, i * s, width, i * s);
  }*/
  for(int i = 0; i < tiles.size(); i++){
    Tiles t = tiles.get(i);
    t.show();
  }
  for (Enemy e: enemy){
    e.show();
    e.move();
  }
}

```

here the code for the enemy

```auto
class Enemy{
  boolean moved = false;
  boolean safe = false;
  int x, y, type;
  Enemy(int type){
    this.type = type;
    if (type == 0){
      x = 0;
      y = 3 * 25;
    }
  }
  void show(){
    fill(255,0,255);
    rect(x,y,s,s);
  }
  
  void move(){// here is the problem
    if (turn == 1 && moved == false){
      int way = 3;
      for (int i = 0; i < tiles.size(); i++){
        Tiles t = tiles.get(i);
        if (t.type == 1){
          if (way == 1 && t.y - 25 == y && t.x == x){safe = true;}
          if (way == 2 && t.y + 25 == y && t.x == x){safe = true;}
          if (way == 3 && t.y - 25 == x && t.y == y){safe = true;}
          if (way == 4 && t.y + 25 == x && t.x == y){safe = true;}
        }
      }
      if (safe == true){
        if (way == 1){
          y += 25;
          moved = true;
        }
        if (way == 2){
          y -= 25;
          moved = true;
        }
        if (way == 3){
          x += 25;
          moved = true;
        }
        if (way == 4){
          x -= 25;
          moved = true;
        }
      }
    }
  }// move
}

```

here the code for the tiles

```auto
class Tiles{
  int type;
  int x,y;
  Tiles(int type, int x, int y){
    this.type = type;
    this.x = x;
    this.y = y;
  }
  
  void show(){
    if (type == 1){fill(210,180,140);}
    if (type == 2){fill(0,205,0);}
    if (type == 3){fill(0,0,255);}
    rect(x,y,25,25);
    stroke(0);
    text(type,10,550);
  }
}

```

ok, so the enemy won’t move or moves incorrect. I made a path with titles to try it out. Can anyone tell me why he won’t move to the dirt tile in front of him. I sent it to 3 because that is the one he needs to move.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 10, 2018, 8:39am UTC](https://discourse.processing.org/t/path-finding-tiles-grid/5395/2 "2018-11-10T08:39:23Z")

</div>

The Problem probably has to do with the t.y-25==x in if (way == 3 && and so on). And according to your move function, it will only move once and then halt. You Need to reset the moved and safe variables. And i don‘t know what Type the tiles are you want it on, But it will only ever Walk of the Type is 1, and only once.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [January 6, 2019, 8:43pm UTC](https://discourse.processing.org/t/path-finding-tiles-grid/5395/3 "2019-01-06T20:43:59Z")

</div>

He moves one spot checks then move again stop checks

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [January 7, 2019, 10:49am UTC](https://discourse.processing.org/t/path-finding-tiles-grid/5395/4 "2019-01-07T10:49:14Z")

</div>

The question is 2 1/2 months old, so I assume there were some changes made to the code, so please update the code, but if not, could you state more precisely where your problem lies?
