Ejemplo n.º 1
0
int main() {
  Point path[] = {{0, 0}, {10, 10}, {5, 5}, {15, 15}, {19, 19}};
  int32_t pathSize = sizeof(path)/sizeof(Point);
  int32_t mapWidth = 20;
  int32_t mapHeight = 20;
  Map map = newMap(mapWidth, mapHeight);
  Guard guard = newGuard("wiwi", (Point) {0, 0});
  guard.joinMap(&guard, &map);

  int i, j = 0;
  int32_t rectWallCount = randNum(3, 5);
  printf("rectWallCount: %d\n", rectWallCount);
  RectWall *rectWalls = malloc(sizeof(RectWall) * rectWallCount);
  for (i = 0; i < rectWallCount; i++) { // each rectWalls
    rectWalls[i].area[0].x = randNum(i, mapWidth - 1);
    rectWalls[i].area[0].y = randNum(i+1, mapHeight - 1);
    rectWalls[i].area[1].x = rectWalls[i].area[0].x + 1;
    rectWalls[i].area[1].y = rectWalls[i].area[0].y;
    rectWalls[i].area[2].x = rectWalls[i].area[0].x + 1;
    rectWalls[i].area[2].y = rectWalls[i].area[0].y + 1;
    rectWalls[i].area[3].x = rectWalls[i].area[0].x;
    rectWalls[i].area[3].y = rectWalls[i].area[0].y + 1;
  }

  i = 0;
  while ((rectWallsInMap(rectWalls, rectWallCount, &map) == false ||
        overlapRectWalls(rectWalls, rectWallCount) ||
        overlapRectWallsAndPath(rectWalls, rectWallCount, &path[0], pathSize)) &&
      i < rectWallCount) {
    // TODO Should find wrong specific rectWall and rand it.
    rectWalls[i].area[0].x = randNum(i+randNum(0,i), mapWidth - 1);
    rectWalls[i].area[0].y = randNum(i, mapHeight - 1);
    rectWalls[i].area[1].x = rectWalls[i].area[0].x + 1;
    rectWalls[i].area[1].y = rectWalls[i].area[0].y;
    rectWalls[i].area[2].x = rectWalls[i].area[0].x + 1;
    rectWalls[i].area[2].y = rectWalls[i].area[0].y + 1;
    rectWalls[i].area[3].x = rectWalls[i].area[0].x;
    rectWalls[i].area[3].y = rectWalls[i].area[0].y + 1;
    i++;
    if (i == rectWallCount) {
      i = 0;
    }
  }

  for (i = 0; i < rectWallCount; i++) { // each rectWalls
    for (j = 0; j < 4; j++) { // each area
      if (map.gridth(&map, rectWalls[i].area[j]) != NULL) {
        printf("rect wall: %d, %d\n",
            map.gridth(&map, rectWalls[i].area[j])->position.x,
            map.gridth(&map, rectWalls[i].area[j])->position.y);
        map.gridth(&map, rectWalls[i].area[j])->isBeWalkable = false;
      }
    }
  }
  free(rectWalls);

  printf("%s start moving\n",guard.name);
  map.gridth(&map, path[2])->isBeWalkable = false;
  guard.move(&guard, path[1]);
  map.gridth(&map, path[2])->isBeWalkable = true;
  guard.move(&guard, path[2]);
  map.gridth(&map, path[1])->isBeWalkable = false;
  guard.move(&guard, path[3]);
  map.gridth(&map, path[1])->isBeWalkable = true;
  guard.move(&guard, path[4]);
  return 0;
}