#pragma once


namespace atokom2 {

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

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::DataGridView^  dataGridView1;
	protected: 
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  plikToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  wyjscieToolStripMenuItem;

	private: System::Windows::Forms::ToolStripMenuItem^  rodzajToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  graWZycieToolStripMenuItem;

	private: System::Windows::Forms::ToolStripMenuItem^  seedsToolStripMenuItem;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button3;

	private: System::Windows::Forms::Timer^  timer1;
	private: System::ComponentModel::IContainer^  components;

	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)
		{
			this->components = (gcnew System::ComponentModel::Container());
			System::Windows::Forms::DataGridViewCellStyle^  dataGridViewCellStyle3 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
			System::Windows::Forms::DataGridViewCellStyle^  dataGridViewCellStyle4 = (gcnew System::Windows::Forms::DataGridViewCellStyle());
			this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->plikToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->wyjscieToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->rodzajToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->graWZycieToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->seedsToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->BeginInit();
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// dataGridView1
			// 
			this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
			dataGridViewCellStyle3->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
			dataGridViewCellStyle3->BackColor = System::Drawing::Color::White;
			dataGridViewCellStyle3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(238)));
			dataGridViewCellStyle3->ForeColor = System::Drawing::SystemColors::ControlText;
			dataGridViewCellStyle3->SelectionBackColor = System::Drawing::SystemColors::Highlight;
			dataGridViewCellStyle3->SelectionForeColor = System::Drawing::SystemColors::HighlightText;
			dataGridViewCellStyle3->WrapMode = System::Windows::Forms::DataGridViewTriState::False;
			this->dataGridView1->DefaultCellStyle = dataGridViewCellStyle3;
			this->dataGridView1->Location = System::Drawing::Point(12, 37);
			this->dataGridView1->Name = L"dataGridView1";
			dataGridViewCellStyle4->Alignment = System::Windows::Forms::DataGridViewContentAlignment::MiddleLeft;
			dataGridViewCellStyle4->BackColor = System::Drawing::SystemColors::Window;
			dataGridViewCellStyle4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, 
				System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(238)));
			dataGridViewCellStyle4->ForeColor = System::Drawing::SystemColors::WindowText;
			dataGridViewCellStyle4->SelectionBackColor = System::Drawing::SystemColors::Highlight;
			dataGridViewCellStyle4->SelectionForeColor = System::Drawing::SystemColors::HighlightText;
			dataGridViewCellStyle4->WrapMode = System::Windows::Forms::DataGridViewTriState::True;
			this->dataGridView1->RowHeadersDefaultCellStyle = dataGridViewCellStyle4;
			this->dataGridView1->RowTemplate->Height = 10;
			this->dataGridView1->RowTemplate->ReadOnly = true;
			this->dataGridView1->Size = System::Drawing::Size(281, 229);
			this->dataGridView1->TabIndex = 0;
			this->dataGridView1->CellClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::CellClick);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->plikToolStripMenuItem, 
				this->rodzajToolStripMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(335, 24);
			this->menuStrip1->TabIndex = 1;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// plikToolStripMenuItem
			// 
			this->plikToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->wyjscieToolStripMenuItem});
			this->plikToolStripMenuItem->Name = L"plikToolStripMenuItem";
			this->plikToolStripMenuItem->Size = System::Drawing::Size(34, 20);
			this->plikToolStripMenuItem->Text = L"Plik";
			// 
			// wyjscieToolStripMenuItem
			// 
			this->wyjscieToolStripMenuItem->Name = L"wyjscieToolStripMenuItem";
			this->wyjscieToolStripMenuItem->Size = System::Drawing::Size(122, 22);
			this->wyjscieToolStripMenuItem->Text = L"Wyjście";
			// 
			// rodzajToolStripMenuItem
			// 
			this->rodzajToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->graWZycieToolStripMenuItem, 
				this->seedsToolStripMenuItem});
			this->rodzajToolStripMenuItem->Name = L"rodzajToolStripMenuItem";
			this->rodzajToolStripMenuItem->Size = System::Drawing::Size(52, 20);
			this->rodzajToolStripMenuItem->Text = L"Rodzaj";
			// 
			// graWZycieToolStripMenuItem
			// 
			this->graWZycieToolStripMenuItem->CheckOnClick = true;
			this->graWZycieToolStripMenuItem->Name = L"graWZycieToolStripMenuItem";
			this->graWZycieToolStripMenuItem->Size = System::Drawing::Size(140, 22);
			this->graWZycieToolStripMenuItem->Text = L"Gra w życie";
			this->graWZycieToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::graWZycieToolStripMenuItem_Click);
			// 
			// seedsToolStripMenuItem
			// 
			this->seedsToolStripMenuItem->CheckOnClick = true;
			this->seedsToolStripMenuItem->Name = L"seedsToolStripMenuItem";
			this->seedsToolStripMenuItem->Size = System::Drawing::Size(140, 22);
			this->seedsToolStripMenuItem->Text = L"Seeds";
			this->seedsToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::seedsToolStripMenuItem_Click);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(12, 285);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(40, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Krok";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(58, 285);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(42, 23);
			this->button2->TabIndex = 3;
			this->button2->Text = L"Auto";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(106, 285);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(42, 23);
			this->button3->TabIndex = 4;
			this->button3->Text = L"Stop";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// timer1
			// 
			this->timer1->Interval = 1000;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(335, 310);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->dataGridView1);
			this->Controls->Add(this->menuStrip1);
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->dataGridView1))->EndInit();
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
		private: array<int,2>^ plansza; // glowna plansza
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				  dataGridView1->ColumnCount=20;
				 dataGridView1->RowCount=20;
				 dataGridView1->AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode::Fill; //wypelnuj cały obszar kontrolki
				 dataGridView1->RowHeadersVisible=false;
				 dataGridView1->ColumnHeadersVisible=false;
				
				 //musi tak być bo po schowaniu nagłowków można rozciągać komórki na całej lini brzegowej czego nie chcemy
				 dataGridView1->AllowUserToResizeColumns=false;
				 dataGridView1->AllowUserToResizeRows=false;
				plansza = gcnew array<int,2>(31,31);
				
			 }

private: System::Void CellClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
			 
			  if (dataGridView1->Rows[dataGridView1->CurrentCell->RowIndex]->Cells[dataGridView1->CurrentCell->ColumnIndex]->Style->BackColor!=System::Drawing::Color::Green)	 
			{dataGridView1->Rows[dataGridView1->CurrentCell->RowIndex]->Cells[dataGridView1->CurrentCell->ColumnIndex]->Style->BackColor=System::Drawing::Color::Green;}
			else
            dataGridView1->Rows[dataGridView1->CurrentCell->RowIndex]->Cells[dataGridView1->CurrentCell->ColumnIndex]->Style->BackColor=System::Drawing::Color::White;
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			  //glowna funkcja 
				 if (Form1::graWZycieToolStripMenuItem->Checked) {
				 //wypelnij plansze w pamieci
				 //tryb "gra w zycie"
				 int sasiad=0;
				 for (int j=0;j<20;j++) {			
				 for (int i=0;i<20;i++) {
                 // ustal ile komorek w otoczeniu danej komorki jest aktywne
					 for (int j1=j-1;j1<j+2;j1++) {
						 for (int i1=i-1;i1<i+2;i1++)
						 {
                             if ((j1>=0)&&(j1<20)&&(i1>=0)&&(i1<20))  //sprawdzenie czy punkt nie jest poza plansza
							 {if (dataGridView1->Rows[i1]->Cells[j1]->Style->BackColor==System::Drawing::Color::Green)
							 if (!((i1==i)&&(j1==j))) sasiad++;} //jezeli to sasiad (a nie aktualna komorka) to policz go
						 }
					 }
                   
				   
				   plansza[i,j]=0;// komorka martwa chyba że
				   if (sasiad==3) plansza[i,j]=1;// w otoczeniu komorki 3 aktywne
				   if ((sasiad==2)&&(dataGridView1->Rows[i]->Cells[j]->Style->BackColor==System::Drawing::Color::Green)) // 2 aktywne i aktualna aktywna
					   plansza[i,j]=1; //w tych 2 przypadkach komorka aktywna w nastepnym kroku
			   sasiad=0;
				 }
				 }
				//label1->Text=plansza[1,1]->ToString();
				 //przepisz plansze na ekran
				 for (int j=0;j<20;j++) {			
				 for (int i=0;i<20;i++) {
                 
					 if (plansza[i,j]==1)
					  dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Green;
					 else
					  dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::White;
				 }
				 }
				 }
				 if (seedsToolStripMenuItem->Checked) {
				 //wypelnij plansze w pamieci
				 //tryb "seeds"
				 int sasiad=0;
				 for	(int j=0;j<20;j++) {			
				 for (int i=0;i<20;i++) {
                 // ustal ile komorek w otoczeniu danej komorki jest aktywne
					 for (int j1=j-1;j1<j+2;j1++) {
						 for (int i1=i-1;i1<i+2;i1++)
						 {
                             if ((j1>=0)&&(j1<20)&&(i1>=0)&&(i1<20))  //sprawdzenie czy punkt nie jest poza plansza
							 {if (dataGridView1->Rows[i1]->Cells[j1]->Style->BackColor==System::Drawing::Color::Green)
							 if (!((i1==i)&&(j1==j))) sasiad++;} //jezeli to sasiad (a nie aktualna komorka) to policz go
						 }
					 }
                   
				   
				   plansza[i,j]=0;// komorka martwa chyba że
					   if (sasiad==2) plansza[i,j]=1;// w otoczeniu komorki 2 aktywne
				   
			   sasiad=0;
				 }
				 }
				//label1->Text=plansza[1,1]->ToString();
				 //przepisz plansze na ekran
				 for (int j=0;j<20;j++) {			
				 for (int i=0;i<20;i++) {
                 
					 if (plansza[i,j]==1)
					  dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Green;
					 else
					  dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::White;
				 }
				 }
				 }

		 }
private: System::Void graWZycieToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
             seedsToolStripMenuItem->Checked=false;
		 }
private: System::Void seedsToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			  graWZycieToolStripMenuItem->Checked=false;
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			  button2->Enabled=false;
			 timer1->Enabled=true;
			  button3->Enabled=true;
		 }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			 button2->Enabled=true;
             timer1->Enabled=false;
			 button3->Enabled=false;
		 }
};
}

