#import "grid.h"

#define __USE_FIXED_PROTOTYPES__   // for gcc headers
#include <stdio.h>

int compareIntegers( id a, id b )
{
  if ( (int)a < (int)b ) return -1;
  return ( (int)a > (int)b );
}

int main(void)
{
  id aZone, aMap, aTurtle, index, zoneObjects, anObject;
  int i;

  initModule( grid );

  aZone = [Zone createBegin: globalZone];
  [aZone setObjectCollection: 1];
  aZone = [aZone createEnd];

  aMap  = [Map createBegin: aZone];
  [aMap setCompareFunction: compareIntegers];
  aMap = [aMap createEnd];

  printf( "count: %d\n", [aMap count] );

  index = [aMap begin: scratchZone];
  while ( (aTurtle = [index next]) ) [aTurtle print];
  if ( (aTurtle = [aMap at: (id)0]) ) [aTurtle print];

  for ( i = 0; i < 10; i++ ) {
    aTurtle = [GridTurtle create: aZone];
    [aTurtle turn: -1];
    [aTurtle move: i * 10];
    [aMap at: (id)i insert: aTurtle];
  }

  zoneObjects = [aZone getObjects];

  index = [zoneObjects begin: scratchZone];
  while ( (anObject = [index next]) ) xprint( anObject );

  index = [aMap begin: scratchZone];
  while ( (aTurtle = [index next]) ) [[index remove] drop];

  index = [zoneObjects begin: scratchZone];
  while ( (anObject = [index next]) ) xprint( anObject );

  return 0;
}
