#pragma once
#include <windows.h>
#include "Form2.h"
#include "Form1.h"
#include "Town.h"
#include <iostream>
#include <fstream>
#include <string>
namespace BARGAME {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Threading;

	/// <summary>
	/// Summary for Form2
	/// </summary>
	public ref class Form2 : public System::Windows::Forms::Form
	{
	public:

		Form2(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

		Form2(double numagents,int barcap,bool ispercentcap,int avgage,int alpha,int grpsize ,String^ outputname)	//ΈΔΑΛ
			
		{
			town=new Town((int) numagents,barcap, ispercentcap,avgage,alpha,grpsize);
			ultimategraph=new double[8560];
			for(int i=0;i<8560;i++){
				ultimategraph[i]=0;
			}
			filename=outputname;
			datax=new double[1000];
			for(int i=0;i<1000;i++){
				datax[i]=i;
			}
			InitializeComponent();
		}

		String^ filename;
		double *datax;
		int numbars;
		Town *town;
		int sleepytime;
		int selectedindex1;
		int selectedindex2;
		int stop;
		double *ultimategraph;
		graphPtr t;
		int turn;
		Thread^ backendthread;
		delegate void SetTextDelegate(String^ text);

	private: System::Windows::Forms::Label^  label3;
	public: 
	private: System::Windows::Forms::Label^  label4;


	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label5;

	public: 


	public: 

		//Function that updates the delay per round.  Future version will also update graph types so the user no longer
		//needs to click the update button.

		//Function that calls the graphing functions to update the graph in real time.
	private: void runner(double *data){
				 switch(selectedindex1){
				 case 0:
					 break;
				 case 1:
					 winningbars(data,1000,0);
					 break;
				 case 2:
					 winners(data,1000,0);
					 break;
				 case 3:
					 avgstratscores(data,1000,0);
					 break;
				 case 4:
					 beststrat(data,1000,0);
					 break;
				 case 5:
					 bardist(data,numbars,0);
					 break;
				 case 6:
					 numdeaths(data,1000,0);
					 break;
				 case 7:
					 agedist(data,100,0);
					 break;
				 }

				 switch(selectedindex2){
				 case 0:
					 break;
				 case 1:
					 winningbars(data,1000,1);
					 break;
				 case 2:
					 winners(data,1000,1);
					 break;
				 case 3:
					 avgstratscores(data,1000,1);
					 break;
				 case 4:
					 beststrat(data,1000,1);
					 break;
				 case 5:
					 bardist(data,numbars,1);
					 break;
				 case 6:
					 numdeaths(data,1000,1);
					 break;
				 case 7:
					 agedist(data,100,1);
					 break;
				 }
			 }

			 //Creates the winning bars graph
	private: void winningbars(double data[],int size,int leftorright ){
				 // The data for the bar chart

				 // The labels for the bar chart
				 const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);


				 // Add a title to the chart
				 c->addTitle("AvgCapital of Groupers - AvgCapital of NonGroupers");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addLineLayer(DoubleArray(data, size), 0xcc0000);
				 c->addTrendLayer(DoubleArray(datax, size),DoubleArray(data, size),0x0000FF)->setLineWidth(3);

				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart

				 c->makeChart("winningbars.png");

				 if(leftorright==0 && selectedindex1==1){
					 pictureBox2->Load(L".\\winningbars.p" 
						 L"ng");
				 }
				 else 
				 //free up resources
				 delete c;

			 }

			 //Creates the winners graph
	private: void winners(double data[],int size,int leftorright ){
				 // The data for the bar chart

				 // The labels for the bar chart
				 const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);

				 // Add a title to the chart
				 c->addTitle("Number of Winners");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addLineLayer(DoubleArray(data+1000, size), 0xcc0000);
				 c->addTrendLayer(DoubleArray(datax, size),DoubleArray(data+1000, size),0x0000FF)->setLineWidth(3);

				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart
				 c->makeChart("winners.png");
				 if(leftorright==0  && selectedindex1==2){
					 pictureBox2->Load(L".\\winners.p" 
						 L"ng");
				 }
				 else 
				 //free up resources
				 delete c;
			 }

			 //Creates the average score of strategies graph
	private: void avgstratscores(double data[],int size,int leftorright ){
				 // The data for the bar chart

				 // The labels for the bar chart
				 const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);

				 // Add a title to the chart
				 c->addTitle("Average Score of All Strategies");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addLineLayer(DoubleArray(data+2000, size), 0xcc0000);

				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart
				 c->makeChart("avgstratscores.png");
				 if(leftorright==0  && selectedindex1==3){
					 pictureBox2->Load(L".\\avgstratscores.p" 
						 L"ng");
				 }
				 else 

				 //free up resources
				 delete c;
			 }

			 //Creates the best strategy's score graph
	private: void beststrat(double data[],int size,int leftorright){
				 // The data for the bar chart

				 // The labels for the bar chart
				 const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);

				 // Add a title to the chart
				 c->addTitle("Best Strategy's Score");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addLineLayer(DoubleArray(data+3000, size), 0xcc0000);

				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart
				 c->makeChart("beststrat.png");
				 if(leftorright==0 && selectedindex1==4){
					 pictureBox2->Load(L".\\beststrat.p" 
						 L"ng");
				 }
				 else 
				 //free up resources
				 delete c;

			 }

			 //Creates the bar distribution graph
	private: void bardist(double data[],int size,int leftorright){
				 // The data for the pie chart

				 // The labels for the pie chart
				 // The labels for the pie chart
				 //256 is max number of bars
				 char* labels[256]={
					 "Home",
					 "Bar 1",
					 "Bar 2",
					 "Bar 3",
					 "Bar 4",
					 "Bar 5",
					 "Bar 6",
					 "Bar 7",
					 "Bar 8",
					 "Bar 9",
					 "Bar 10",
					 "Bar 11",
					 "Bar 12",
					 "Bar 13",
					 "Bar 14",
					 "Bar 15",
					 "Bar 16",
					 "Bar 17",
					 "Bar 18",
					 "Bar 19",
					 "Bar 20",
					 "Bar 21",
					 "Bar 22",
					 "Bar 23",
					 "Bar 24",
					 "Bar 25",
					 "Bar 26",
					 "Bar 27",
					 "Bar 28",
					 "Bar 29",
					 "Bar 30",
					 "Bar 31",
					 "Bar 32",
					 "Bar 33",
					 "Bar 34",
					 "Bar 35",
					 "Bar 36",
					 "Bar 37",
					 "Bar 38",
					 "Bar 39",
					 "Bar 40",
					 "Bar 41",
					 "Bar 42",
					 "Bar 43",
					 "Bar 44",
					 "Bar 45",
					 "Bar 46",
					 "Bar 47",
					 "Bar 48",
					 "Bar 49",
					 "Bar 50",
					 "Bar 51",
					 "Bar 52",
					 "Bar 53",
					 "Bar 54",
					 "Bar 55",
					 "Bar 56",
					 "Bar 57",
					 "Bar 58",
					 "Bar 59",
					 "Bar 60",
					 "Bar 61",
					 "Bar 62",
					 "Bar 63",
					 "Bar 64",
					 "Bar 65",
					 "Bar 66",
					 "Bar 67",
					 "Bar 68",
					 "Bar 69",
					 "Bar 70",
					 "Bar 71",
					 "Bar 72",
					 "Bar 73",
					 "Bar 74",
					 "Bar 75",
					 "Bar 76",
					 "Bar 77",
					 "Bar 78",
					 "Bar 79",
					 "Bar 80",
					 "Bar 81",
					 "Bar 82",
					 "Bar 83",
					 "Bar 84",
					 "Bar 85",
					 "Bar 86",
					 "Bar 87",
					 "Bar 88",
					 "Bar 89",
					 "Bar 90",
					 "Bar 91",
					 "Bar 92",
					 "Bar 93",
					 "Bar 94",
					 "Bar 95",
					 "Bar 96",
					 "Bar 97",
					 "Bar 98",
					 "Bar 99",
					 "Bar 100",
					 "Bar 101",
					 "Bar 102",
					 "Bar 103",
					 "Bar 104",
					 "Bar 105",
					 "Bar 106",
					 "Bar 107",
					 "Bar 108",
					 "Bar 109",
					 "Bar 110",
					 "Bar 111",
					 "Bar 112",
					 "Bar 113",
					 "Bar 114",
					 "Bar 115",
					 "Bar 116",
					 "Bar 117",
					 "Bar 118",
					 "Bar 119",
					 "Bar 120",
					 "Bar 121",
					 "Bar 122",
					 "Bar 123",
					 "Bar 124",
					 "Bar 125",
					 "Bar 126",
					 "Bar 127",
					 "Bar 128",
					 "Bar 129",
					 "Bar 130",
					 "Bar 131",
					 "Bar 132",
					 "Bar 133",
					 "Bar 134",
					 "Bar 135",
					 "Bar 136",
					 "Bar 137",
					 "Bar 138",
					 "Bar 139",
					 "Bar 140",
					 "Bar 141",
					 "Bar 142",
					 "Bar 143",
					 "Bar 144",
					 "Bar 145",
					 "Bar 146",
					 "Bar 147",
					 "Bar 148",
					 "Bar 149",
					 "Bar 150",
					 "Bar 151",
					 "Bar 152",
					 "Bar 153",
					 "Bar 154",
					 "Bar 155",
					 "Bar 156",
					 "Bar 157",
					 "Bar 158",
					 "Bar 159",
					 "Bar 160",
					 "Bar 161",
					 "Bar 162",
					 "Bar 163",
					 "Bar 164",
					 "Bar 165",
					 "Bar 166",
					 "Bar 167",
					 "Bar 168",
					 "Bar 169",
					 "Bar 170",
					 "Bar 171",
					 "Bar 172",
					 "Bar 173",
					 "Bar 174",
					 "Bar 175",
					 "Bar 176",
					 "Bar 177",
					 "Bar 178",
					 "Bar 179",
					 "Bar 180",
					 "Bar 181",
					 "Bar 182",
					 "Bar 183",
					 "Bar 184",
					 "Bar 185",
					 "Bar 186",
					 "Bar 187",
					 "Bar 188",
					 "Bar 189",
					 "Bar 190",
					 "Bar 191",
					 "Bar 192",
					 "Bar 193",
					 "Bar 194",
					 "Bar 195",
					 "Bar 196",
					 "Bar 197",
					 "Bar 198",
					 "Bar 199",
					 "Bar 200",
					 "Bar 201",
					 "Bar 202",
					 "Bar 203",
					 "Bar 204",
					 "Bar 205",
					 "Bar 206",
					 "Bar 207",
					 "Bar 208",
					 "Bar 209",
					 "Bar 210",
					 "Bar 211",
					 "Bar 212",
					 "Bar 213",
					 "Bar 214",
					 "Bar 215",
					 "Bar 216",
					 "Bar 217",
					 "Bar 218",
					 "Bar 219",
					 "Bar 220",
					 "Bar 221",
					 "Bar 222",
					 "Bar 223",
					 "Bar 224",
					 "Bar 225",
					 "Bar 226",
					 "Bar 227",
					 "Bar 228",
					 "Bar 229",
					 "Bar 230",
					 "Bar 231",
					 "Bar 232",
					 "Bar 233",
					 "Bar 234",
					 "Bar 235",
					 "Bar 236",
					 "Bar 237",
					 "Bar 238",
					 "Bar 239",
					 "Bar 240",
					 "Bar 241",
					 "Bar 242",
					 "Bar 243",
					 "Bar 244",
					 "Bar 245",
					 "Bar 246",
					 "Bar 247",
					 "Bar 248",
					 "Bar 249",
					 "Bar 250",
					 "Bar 251",
					 "Bar 252",
					 "Bar 253",
					 "Bar 254",
					 "Bar 255"};

					 // Create a PieChart object of size 480 x 480 pixels
					 PieChart *c = new PieChart(480, 480, 0xddddff, -1, 1);

					 // Set the center of the pie at (240, 240) and the radius to 200 pixels
					 c->setPieSize(240, 240, 140);

					 // Add a title to the pie chart
					 c->addTitle("");

					 // Draw the pie in 3D
					 c->set3D();

					 // Set the pie data and the pie labels
					 c->setData(DoubleArray(data+6000, size), StringArray(labels, size));

					 // Explode the 1st sector (index = 0)
					 c->setExplode(0);

					 // Output the chart
					 c->makeChart("bardist.png");

					 // Output the chart
					 c->makeChart("bardist.png");
					 if(leftorright==0 && selectedindex1==5){
						 pictureBox2->Load(L".\\bardist.p" 
							 L"ng");
					 }
					 else 
					 //free up resources
					 delete c;
			 }

	private: void numdeaths(double data[],int size,int leftorright){
				 // The data for the bar chart

				 // The labels for the bar chart
				 //const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);

				 // Add a title to the chart
				 c->addTitle("Number of Deaths");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addScatterLayer(DoubleArray(datax, size),DoubleArray(data+4000, size),"# Deaths",Chart::DiamondSymbol,10,0xcc0000);
				 c->addTrendLayer(DoubleArray(datax, size),DoubleArray(data+4000, size),0xcc0000)->setLineWidth(3);
				// c->addLineLayer(DoubleArray(data+40000, 1000), 0xcc0000);
				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart
				 c->makeChart("numdeaths.png");
				 if(leftorright==0 && selectedindex1==6){
					 pictureBox2->Load(L".\\numdeaths.p" 
						 L"ng");
				 }
				 else 
				 //free up resources
				 delete c;

			 }

	private: void agedist(double data[],int size,int leftorright){
				 // The data for the bar chart

				 // The labels for the bar chart
				 const char *labels[] = {"Mon", "Tue", "Wed", "Thu", "Fri"};

				 // Create a XYChart object of size 480 x 480 pixels
				 XYChart *c = new XYChart(480, 480);

				 // Set the plotarea at (45, 30) and of size 400 x 400 pixels
				 c->setPlotArea(45, 30, 400, 400, -1, 0xffffff, 0xffffff);

				 // Add a title to the chart
				 c->addTitle("Age Distribution");

				 // Add a title to the y axis
				 //c->yAxis()->setTitle("MBytes");

				 // Add a title to the x axis
				 //c->xAxis()->setTitle("Work Week 25");

				 // Add a bar chart layer with green (0x00ff00) bars using the given data
				 c->addLineLayer(DoubleArray(data+5000, size), 0xcc0000);

				 // Set the labels on the x axis.
				 //c->xAxis()->setLabels(StringArray(labels, sizeof(labels)/sizeof(labels[0])));

				 // Output the chart
				 c->makeChart("agedist.png");
				 if(leftorright==0 && selectedindex1==7){
					 pictureBox2->Load(L".\\agedist.p" 
						 L"ng");
				 }
				 else 
				 //free up resources
				 delete c;

			 }

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form2()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button2;
	protected: 

	private: System::Windows::Forms::Label^  label2;


	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::HScrollBar^  hScrollBar1;


	private: System::Windows::Forms::HelpProvider^  helpProvider1;

	private: System::Windows::Forms::PictureBox^  pictureBox2;


	private: System::ComponentModel::IContainer^  components;


	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form2::typeid));
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->hScrollBar1 = (gcnew System::Windows::Forms::HScrollBar());
			this->helpProvider1 = (gcnew System::Windows::Forms::HelpProvider());
			this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->BeginInit();
			this->SuspendLayout();
			// 
			// button2
			// 
			this->button2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(224)), static_cast<System::Int32>(static_cast<System::Byte>(224)), 
				static_cast<System::Int32>(static_cast<System::Byte>(224)));
			this->button2->Font = (gcnew System::Drawing::Font(L"SimHei", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->button2->ForeColor = System::Drawing::Color::Black;
			this->button2->Location = System::Drawing::Point(14, 8);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(203, 39);
			this->button2->TabIndex = 18;
			this->button2->Text = L"Start simulation";
			this->button2->UseVisualStyleBackColor = false;
			this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->BackColor = System::Drawing::Color::Transparent;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label2->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label2->Location = System::Drawing::Point(170, 60);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(150, 22);
			this->label2->TabIndex = 16;
			this->label2->Text = L"Speed Per Round";
			// 
			// comboBox1
			// 
			this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox1->Font = (gcnew System::Drawing::Font(L"Georgia", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(8) {L"(N/A)", L"AvgCapital of Groupers - AvgCapital of NonGroupers", 
				L"Number of Winners", L"Average Score of All Strategies", L"Best Strategy\'s Score", L"", L"Number of Deaths", L"Age Distribution"});
			this->comboBox1->Location = System::Drawing::Point(14, 136);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(480, 23);
			this->comboBox1->TabIndex = 13;
			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form2::ComboBox1_SelectedIndexChanged);
			// 
			// hScrollBar1
			// 
			this->hScrollBar1->Location = System::Drawing::Point(10, 82);
			this->hScrollBar1->Name = L"hScrollBar1";
			this->hScrollBar1->Size = System::Drawing::Size(480, 32);
			this->hScrollBar1->TabIndex = 12;
			this->hScrollBar1->Value = 91;
			// 
			// helpProvider1
			// 
			this->helpProvider1->HelpNamespace = L"C:\\Users\\Andrew\\Documents\\Visual Studio 2010\\Projects\\BARGAME\\BARGAMEhelp.CHM";
			// 
			// pictureBox2
			// 
			this->pictureBox2->ImageLocation = L"";
			this->pictureBox2->Location = System::Drawing::Point(14, 161);
			this->pictureBox2->Name = L"pictureBox2";
			this->pictureBox2->Size = System::Drawing::Size(480, 466);
			this->pictureBox2->TabIndex = 20;
			this->pictureBox2->TabStop = false;
			this->pictureBox2->Click += gcnew System::EventHandler(this, &Form2::pictureBox2_Click);
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->BackColor = System::Drawing::Color::Transparent;
			this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 10.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label3->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label3->Location = System::Drawing::Point(449, 60);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(48, 20);
			this->label3->TabIndex = 22;
			this->label3->Text = L"Faster";
			this->label3->Click += gcnew System::EventHandler(this, &Form2::label3_Click);
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->BackColor = System::Drawing::Color::Transparent;
			this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 10.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label4->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label4->Location = System::Drawing::Point(10, 62);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(54, 20);
			this->label4->TabIndex = 23;
			this->label4->Text = L"Slower";
			// 
			// textBox1
			// 
			this->textBox1->Enabled = false;
			this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox1->Location = System::Drawing::Point(372, 12);
			this->textBox1->Name = L"textBox1";
			this->textBox1->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
			this->textBox1->Size = System::Drawing::Size(100, 26);
			this->textBox1->TabIndex = 25;
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->BackColor = System::Drawing::Color::Transparent;
			this->label5->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 18, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label5->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
			this->label5->Location = System::Drawing::Point(256, 12);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(118, 31);
			this->label5->TabIndex = 26;
			this->label5->Text = L"Round #:";
			// 
			// Form2
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::SystemColors::Control;
			this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"$this.BackgroundImage")));
			this->ClientSize = System::Drawing::Size(510, 639);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->pictureBox2);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->hScrollBar1);
			this->HelpButton = true;
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"Form2";
			this->helpProvider1->SetShowHelp(this, true);
			this->Text = L"MINORITYGAME";
			this->FormClosed += gcnew System::Windows::Forms::FormClosedEventHandler(this, &Form2::Form2_FormClosed);
			this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox2))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		//Start Simulation button.  Updates speed per round and types of graphs chosen by user, then starts simulation.
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(button2->Text=="Start simulation"){
					 button2->Text="Pause simulation";
					 stop=1;
					 sleepytime=(100-hScrollBar1->Value)*10;
					 selectedindex1=comboBox1->SelectedIndex;
					
					 this->backendthread = gcnew Thread(gcnew ThreadStart(this,&Form2::ThreadProcSafe));
					 this->backendthread->Start();
				 }
				 else{
					 button2->Text="Start simulation";
					 stop=0;
				 }
			 }

	private: void ComboBox1_SelectedIndexChanged( Object^ sender, System::EventArgs^ e )
			 {
				 selectedindex1=comboBox1->SelectedIndex;
			 }
	


	private: System::Void Form2_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) {
				 stop=0;
				 delete town;
				 delete[] ultimategraph;
			 }

			 //Thread to run the backend.  Ultimate graph is where all the data is stored, and using an offset, data is retrieved.
	private: void ThreadProcSafe()
			 {
				 String ^ s;
				 int i;
				 string outputfilename;
				 MarshalString(filename,outputfilename);
				 ofstream outputFile;
				 outputFile.open(outputfilename.c_str());

				 while(stop){
					 outputFile << "Round: "<<turn<<endl<<endl;// if round == 1, use another turn function
					 t=town->turn();/////////////////////////////////////////////////////////////
					 for(i=0;i<numbars+1;i++){
						 ultimategraph[i+6000]+=(double) t->barCompare[i];
						 //outputFile << "Wins for Bar["<<i<<"]: "<<t->barCompare[i] << endl;
					 }
					 outputFile << "People with age of index: "<<endl;
					 for(i=0;i<100;i++){
						 ultimategraph[i+5000]=(double) t->age[i];
						 outputFile << t->age[i]<<"  ";
						 if((i+1)%10==0){
							 outputFile<<endl;
						 }
					 }
					 if(turn<1000){
						 ultimategraph[turn]=(double) t->grouperAdvantage;
						 //safety net.  for some reason numwinners goes to -1000000000
						 if(t->numWinners==0){
							 ultimategraph[turn+1000]=ultimategraph[turn-1+1000];
						 }
						 else{
						 ultimategraph[turn+1000]=(double) t->numWinners;
						 }
						 ultimategraph[turn+2000]=(double) t->avgStratScore;
						 ultimategraph[turn+3000]=(double) t->bestStratScore;
						 ultimategraph[turn+4000]=(double) t->deaths;
						 outputFile << "AvgCapital of Groupers - AvgCapital of NonGroupers: "<<t->grouperAdvantage << endl;
						 outputFile << "Number of Winners: "<<t->numWinners << endl;
						 outputFile << "Average Score of All Strategies: "<<t->avgStratScore << endl;
						 outputFile << "Best Strategy's Score: "<<t->bestStratScore << endl;
						 outputFile << "Number of Deaths: "<<t->deaths << endl;
					 }
					 else if(turn >=1000){
						 for(i=0;i<999;i++){
							 ultimategraph[i]=ultimategraph[i+1];
							 ultimategraph[i+1000]=ultimategraph[i+1000+1];
							 ultimategraph[i+2000]=ultimategraph[i+2000+1];
							 ultimategraph[i+3000]=ultimategraph[i+3001];
							 ultimategraph[i+4000]=ultimategraph[i+4001];
						 }
						 ultimategraph[999]=(double) t->grouperAdvantage;
						 ultimategraph[1999]=(double) t->numWinners;
						 ultimategraph[2999]=(double) t->avgStratScore;
						 ultimategraph[3999]=(double) t->bestStratScore;
						 ultimategraph[4999]=(double) t->deaths;
						 outputFile << "Round: "<<turn<<endl<<endl;
						 outputFile << "AvgCapital of Groupers - AvgCapital of NonGroupers: "<<t->grouperAdvantage << endl;
						 outputFile << "Number of Winners: "<<t->numWinners << endl;
						 outputFile << "Average Score of All Strategies: "<<t->avgStratScore << endl;
						 outputFile << "Best Strategy's Score: "<<t->bestStratScore << endl;
						 outputFile << "Number of Deaths: "<<t->deaths << endl;
					 }
					 outputFile<<"-----------------------------------------------------------"<<endl<<endl<<endl;
					 runner(ultimategraph);
					 sleepytime=(91-hScrollBar1->Value)*10;
					 Sleep(sleepytime);
					 s = Convert::ToString(turn);
					 this->SetText(s);
					 turn++;
				 }
				 outputFile.close();
			 }

	private: void MarshalString ( String ^ s, string& os ) {
				 using namespace Runtime::InteropServices;
				 const char* chars = 
					 (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
				 os = chars;
				 Marshal::FreeHGlobal(IntPtr((void*)chars));
			 }

	private: void SetText(String^ text){
				 // InvokeRequired required compares the thread ID of the
				 // calling thread to the thread ID of the creating thread.
				 // If these threads are different, it returns true.
				 if (this->textBox1->InvokeRequired)
				 {
					 SetTextDelegate^ d = 
						 gcnew SetTextDelegate(this, &Form2::SetText);
					 this->Invoke(d, gcnew array<Object^> { text });
				 }
				 else
				 {
					 this->textBox1->Text = text;
				 }
			 }
	private: System::Void label3_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void Form2_Load(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void pictureBox2_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
};
}
