#include<iostream>
#include<vector>
//#include"Agent.h"
#include"Group.h"
//#include<StdAfx.h>

using namespace std;                       //to use vector


Group::Group()
{
    group_choice=0;
	cheat_choice = 0;
}

void Group::addAgent(int agentIndex)
{
	member.push_back(agentIndex);
}


void Group::makeDecision(Agent **agentPter)
{
	int max;//the max socre of the agent in one group
	int demaciya;
	//int adv_decision;
	long sum=0,sum_fortune=0;
	//int cheat_decision;
	//int  decision;
	max=agentPter[member[0]]->getCapital();
	for (int i=0;i<member.size();i++)
	{   
		if(agentPter[member[i]]->getCapital() > max)
		{
			max=agentPter[member[i]]->getCapital();
			demaciya = i;
		}
		sum += agentPter[member[i]]->getDecision() * agentPter[member[i]]->getCapital();
		sum_fortune += agentPter[member[i]]->getCapital();
	}
	adv_choice = agentPter[member[demaciya]]->getDecision();
	if (adv_choice == 0)
	{
		 cheat_choice = 1;
	}
	else
		cheat_choice = 0;
	avg_capital == sum_fortune/member.size();
	//revise avg_captial
	if ((double)sum/(double)sum_fortune < 0.5)
		group_choice=0;
	else
		group_choice=1;
		
	//return (cheat_choice);
}


void Group::ModGroup (Agent **agentPter,int numAgents,int myIndex)
{
	int j=0;
	int individual_num=0;
	int group_num,group_min,group_min_index;     // group_num是个全局变量 groupIndex全局变量
	
	/*********** find the poorest agent **********/
	group_min= agentPter[member[0]]->getCapital();
	group_min_index=0;

	for (int i=0;i<member.size();i++)
	{   
		if (agentPter[member[i]]->getCapital() < group_min)
			 {
				group_min = agentPter[member[i]]->getCapital();
		        group_min_index=i;
		     }
	}


	/***** find an alone agent to get in group *****/
	
	while (j=0)
	{
		for (int i=0;i<numAgents;i++)
		{
			if (agentPter[i]->getGroupIndex() == -1)
			{
				member[group_min_index]=i;
				agentPter[i]->setGroupIndex(myIndex);
				j++;
			}
		}
	}
}

int Group::get_suggestion ()    
{ 
  return group_choice;
}
 

