#include "gridhunt.h"

// Number of Games Played
#define ROUNDS 500
#define GRAPH
// some Global variables
Monster Mob;
int games=0, moves=0;

/* An Array for all Teams */
Player *team_ptrs[MAX_TEAM];
/* An Array of all 'action-functions' */
void (*team_funcs[MAX_TEAM])();

/* This function assigns the pointers to the team_ptrs & team_funcs */
void assign_pointers()
{
  team_ptrs[0]=&team1;
  team_ptrs[1]=&team2;
  team_ptrs[2]=&team3;
  team_ptrs[3]=&team4;
  team_ptrs[4]=&team5;
  team_ptrs[5]=&team6;

  team_funcs[0]=hunt1;
  team_funcs[1]=hunt2;
  team_funcs[2]=hunt3;
  team_funcs[3]=hunt4;
  team_funcs[4]=hunt5;
  team_funcs[5]=hunt6;
}
  
/* Update returns the TRUE if a Team has found the monster,
 * Otherwise FALSE
 * Compares the coordinates of the Monster with the Coors of the
 * players.
 */

int update()
{
  Coor monster;
  Coor player;
  int i,ret=FALSE;

  monster=Mobptr->my_pos();

  for(i=0;i<MAX_TEAM;i++)
    {
      player=team_ptrs[i]->my_pos();
      if(player==monster)
	{
	  ret=TRUE;
	  team_ptrs[i]->wins++;
#ifdef TEXTOUT
	  cout << "Team "<<team_ptrs[i]->get_name()<<
	    " has found the monster.\n";
#endif
	}
      team_ptrs[i]->tick();
    }
  return ret;
}

int main_loop()
{
  int up,i;
  up=update();

  games++;
  moves=0;
#ifdef TEXTOUT
  cout << "Game #"<<games<<":\n";
#endif

  while(up==FALSE)
    {
      Mobptr->move();
      for(i=0;i<MAX_TEAM;i++)
	{
	  team_funcs[i]();
	}
      up=update();
#ifdef GRAPH
	  plotgraph();
#endif
      moves++;
    }
  return moves;
}

void monster_init()
{
  Mobptr=&Mob;
  Mobptr->set_name("Sea_Hag");
  Mobptr->teleport();
}

void player_init()
{
  int i;
  for(i=0;i<MAX_TEAM;i++)
    {
      team_ptrs[i]->reset();
    }
}

int main()
{
  int j,avg=0;

  srandom(time(NULL));
  assign_pointers();
  for(j=0;j<ROUNDS;j++)
    {
      monster_init();      
      player_init();
      avg+=main_loop();
    } 

  cout << "\n\nStatistics of the games :\n";
  
  for(j=0;j<MAX_TEAM;j++)
    {
      team_ptrs[j]->print_stats(TRUE);
    } 
  cout << "\nDone in average " << avg/ROUNDS << " Moves.\n";
}













