#import "grid.h"

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

int main(void)
{
  id   aZone, anArray, aTurtle, aMember, index, newArray;
  int  i;
  
  initModule( grid );

  aZone = [Zone create: globalZone];

  anArray = [Array createBegin: aZone];
  [anArray setCount: 10];
  [anArray setDefaultMember: Member];
  anArray = [anArray createEnd];

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

  for ( i = 0; i < 5; i++ ) {
    aTurtle = [GridTurtle create: aZone];
    [aTurtle turn: -1];
    [aTurtle move: i * 10];
    [anArray atOffset: i put: aTurtle];
  }

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

  index = [anArray begin: aZone];
  while ( (aMember = [index next]) ) {
    if ( ! aMember ) {
      printf( "member is nil\n" );
    } else if ( [aMember respondsTo: M(print)] ) {
      [aMember print];
    } else {
      printf( "%s\n", [aMember getName] );
    }
  }

  newArray = [Array createBegin: aZone];
  [newArray setMemberBlock: (id *)[anArray getMemberBlock] + 2 setCount: 6];
  newArray = [newArray createEnd];

  index = [newArray begin: aZone];
  while ( (aMember = [index next]) ) {
    if ( ! aMember ) {
      printf( "member is nil\n" );
    } else if ( [aMember respondsTo: M(print)] ) {
      [aMember print];
    } else {
      printf( "%s\n", [aMember getName] );
    }
  }

  [newArray setMemberBlock: [anArray getMemberBlock] setCount: 6];

  index = [newArray begin: aZone];
  while ( (aMember = [index next]) ) {
    if ( ! aMember ) {
      printf( "member is nil\n" );
    } else if ( [aMember respondsTo: M(print)] ) {
      [aMember print];
    } else {
      printf( "%s\n", [aMember getName] );
    }
  }
  exit(0);
}
