Advertisement
Guest User

Untitled

a guest
May 5th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // 2d.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "time.h"
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13.  
  14.     srand(time(NULL));
  15.  
  16.     int tab[20][20];
  17.     int tab2[20][20];
  18.  
  19.     for(int i=0;i<20;i++)
  20.     {
  21.         for(int j=0;j<20;j++)
  22.         {
  23.             tab[i][j]=rand()%2;
  24.         }
  25.     }
  26.  
  27. //  tab[10][10]=1;
  28. //tab[11][10]=1;
  29. //tab[10][11]=1;
  30. //tab[11][11]=1;
  31.  
  32.  
  33.     for(int i=0;i<20;i++)
  34.     {
  35.         for(int j=0;j<20;j++)
  36.         {
  37.             tab2[i][j]=tab[i][j];
  38.         }
  39. }
  40.  
  41.     for(int i=0;i<20;i++)
  42.     {
  43.         for(int j=0;j<20;j++)
  44.         {
  45.             cout << tab[i][j] << " ";
  46.         }
  47.         cout << endl;
  48. }
  49.     cout << endl;
  50.  
  51.  
  52.     int z=0;
  53.     while(z<5)
  54.     {
  55.     for(int i=1;i<19;i++)
  56.     {
  57.         for(int j=1;j<19;j++)
  58.         {
  59.             if(tab[i][j]==1)
  60.             {
  61.                 int licz=0;
  62.                 if(tab[i-1][j-1]==1)
  63.                     licz++;
  64.                 if(tab[i-1][j]==1)
  65.                     licz++;
  66.                 if(tab[i-1][j+1]==1)
  67.                     licz++;
  68.                 if(tab[i][j-1]==1)
  69.                     licz++;
  70.                 if(tab[i][j+1]==1)
  71.                     licz++;
  72.                 if(tab[i+1][j-1]==1)
  73.                     licz++;
  74.                 if(tab[i+1][j]==1)
  75.                     licz++;
  76.                 if(tab[i+1][j+1]==1)
  77.                     licz++;
  78.  
  79.                 //cout << "licznik: " << licz << endl;
  80.                 if(licz==0 || licz==1 || licz==4)
  81.                 {
  82.                     tab2[i][j]=0;
  83.                     //cout << "zmieniam wartosc 1 na 0 w komorce" << i << " " << j << endl;
  84.                 }
  85.             }
  86.             if(tab[i][j]==0)
  87.             {
  88.                 int licz=0;
  89.                 if(tab[i-1][j-1]==1)
  90.                     licz++;
  91.                 if(tab[i-1][j]==1)
  92.                     licz++;
  93.                 if(tab[i-1][j+1]==1)
  94.                     licz++;
  95.                 if(tab[i][j-1]==1)
  96.                     licz++;
  97.                 if(tab[i][j+1]==1)
  98.                     licz++;
  99.                 if(tab[i+1][j-1]==1)
  100.                     licz++;
  101.                 if(tab[i+1][j]==1)
  102.                     licz++;
  103.                 if(tab[i+1][j+1]==1)
  104.                     licz++;
  105.  
  106.                 if(licz==3)
  107.                 {
  108.                     tab2[i][j]=1;
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.  
  115.     for(int i=0;i<20;i++)
  116.     {
  117.         for(int j=0;j<20;j++)
  118.         {
  119.             tab[i][j]=tab2[i][j];
  120.         }
  121. }
  122.  
  123.  
  124. for(int i=0;i<20;i++)
  125.     {
  126.         for(int j=0;j<20;j++)
  127.         {
  128.             cout << tab[i][j] << " ";
  129.         }
  130.         cout << endl;
  131. }
  132. cout << endl;
  133. cout << endl;
  134. cout << endl;
  135. z++;
  136.     }
  137.  
  138.  
  139.  
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement