Example #1
0
void initializeMaze(int height, int width, int nAvatars)
{
    MazeNode *temp = (MazeNode*)calloc(width*height, sizeof(MazeNode));
    
    // Amazing is a 2D array of MazeNodes
    Amazing = (MazeNode**)calloc(width, sizeof(MazeNode));
    for (int i = 0; i < width; i++)
        Amazing[i] = temp + i*height;

    // Populate Amazing with unvisited MazeNodes with blank walls
    for(int i = 0; i < width; i++)
        for(int j = 0; j < height; j++)
        {
            Amazing[i][j].visited = malloc(sizeof(struct MazeNode) + (nAvatars));
            for(int k = 0; k < nAvatars; k++)
            {
                Amazing[i][j].visited[k] = 0;
            }
            Amazing[i][j].north = 0;
            Amazing[i][j].east = 0;
            Amazing[i][j].south = 0;
            Amazing[i][j].west = 0;
            Amazing[i][j].whoLast = -1;
            Amazing[i][j].lastDir = -1;
        }
    // Set the walls of each MazeNode in Amazing
    createPerimeter(height, width);
}
/*
 *createZone function
 *
 *p : a pointer to an array of strings represinting a zone
 *
 *this function will take an array of strings represinting a zone
 *and return a struct zone
 */
struct zone *createZone(char **p)
{
  struct zone *z;
  char *temp;
  char **params;
  int now;
  int n = 0;
  int i;
  int end_of_perimeter;
  int s_start;
  int s_end;

  z = malloc(sizeof(struct zone));
  assert(z != 0);
  now = numberOfWords(p[n]);
  assert(now == 2);
  params = divideLine(p[n], now, MAX_STRING_LENGTH);
  assert(params != 0);
  z -> zone_id = atoi(params[1]);
  freeCharArray(params, now);
  n++;
  
  now = numberOfWords(p[n]);
  assert(now == 2);
  params = divideLine(p[n], now, MAX_STRING_LENGTH);
  assert(params != 0);
  z -> number_of_parking_spots = atoi(params[1]);
  freeCharArray(params, now);
  n++;

  temp = "zone_name";
  now = numberOfWords(p[n]);
  params = divideLine(p[n], now, MAX_STRING_LENGTH);
  assert(params != 0);
  if(compTwoStrings(params[0], temp)){
    assert(now == 2);
    z -> zone_name = malloc(MAX_STRING_LENGTH * sizeof(char));
    stringCopy(params[1], z -> zone_name);
    n++;
  }
  else
    z -> zone_name = 0;
  freeCharArray(params, now);

  z -> zone_perimeter = createPerimeter(p + n);
  end_of_perimeter = n;
  temp = "end_perimeter";
  now = numberOfWords(p[end_of_perimeter]);
  params = divideLine(p[end_of_perimeter], now, MAX_STRING_LENGTH);
  assert(params != 0);
  while(!compTwoStrings(temp, params[0])){
    freeCharArray(params, now);
    end_of_perimeter++;
    now = numberOfWords(p[end_of_perimeter]);
    params = divideLine(p[end_of_perimeter], now, MAX_STRING_LENGTH);
    assert(params != 0);
  }
  freeCharArray(params, now);
  n = ++end_of_perimeter;

  z -> parking_spots_list = 
    malloc(z -> number_of_parking_spots * sizeof(struct zone));
  s_start = n;
  s_end = n + 1;
  temp = "end_spot";
  for(i = 0; i < z -> number_of_parking_spots; i++){
    z -> parking_spots_list[i] = createSpot((p + s_start));
    now = numberOfWords(p[s_end]);
    params = divideLine(p[s_end], now, MAX_STRING_LENGTH);
    assert(params != 0);
    while(!compTwoStrings(temp, params[0])){
      freeCharArray(params, now);
      s_end++;
      now = numberOfWords(p[s_end]);
      params = divideLine(p[s_end], now, MAX_STRING_LENGTH);
      assert(params != 0);
    }
    freeCharArray(params, now);
    s_end++;
    s_start = s_end;
  }
  return z;
}