niedziela, 28 maja 2017

The Game of LIFE (Visual C++)



#pragma endregion
private:
System::Void init_Random()
{
srand(time(NULL));
}
private:
array<System::Int64,2>^ game_table;
private:
System::Void init_Table()
{
game_table=gcnew array<System::Int64,2>(30,30);
System::Int64 i;
System::Int64 j;
for(i=0;i<30;i++)
for(j=0;j<30;j++)
game_table[i,j]=0;

}
private:
System::Void change_Table()
{
System::Int64 i;
System::Int64 j;
System::Int64 k;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
k=rand()%10;
if(k>6)
game_table[i,j]=1;
}
}
}
 private:
System::Boolean in_Area(System::Int64 x, System::Int64 y)
{
if((x>=0) && (x<30) && (y>=0) && (y<30))
return true;
else
return false;
}
private:
System::Void step_GridView()
{
            System::Int64 i;
System::Int64 j;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
if(game_table[i,j]==0)
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Yellow;
else
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Black;

}
}
}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
init_Random();
init_Table();
change_Table();
System::Int64 i;
    System::Int64 j;
 
dataGridView1->ColumnCount=30;
dataGridView1->RowCount=30;
dataGridView1->AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode::Fill;
dataGridView1->ColumnHeadersVisible=false;
dataGridView1->RowHeadersVisible=false;
dataGridView1->AllowUserToResizeRows=false;
dataGridView1->AllowUserToResizeColumns=false;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
if(game_table[i,j]==0)
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Yellow;
else
dataGridView1->Rows[i]->Cells[j]->Style->BackColor=System::Drawing::Color::Black;

}
}

}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
System::Int64 i;
    System::Int64 j;
System::Int64 k;
System::Int64 l;
System::Int64 value;
value=0;
for(i=0;i<30;i++)
{
for(j=0;j<30;j++)
{
for(k=i-1;k<i+2;k++)
{
                           for(l=j-1;l<j+2;l++)
  {
  if(in_Area(k,l))
  {
  if(game_table[k,l]==0)
  if(!((l==j)&&(k==i)))
  value++;
  }
  }
}
game_table[i,j]=1;
if(value==3)
game_table[i,j]=0;
if((value==2) && (dataGridView1->Rows[i]->Cells[j]->Style->BackColor==System::Drawing::Color::Yellow))
game_table[i,j]=0;
value=0;

}
}
              step_GridView();

}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
};
}


Brak komentarzy:

Prześlij komentarz