Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <cstdlib>
- #include <ctime>
- using std::cout;
- using std::endl;
- const unsigned int WIDTH = 1000;
- const unsigned int HEIGHT = 1000;
- const unsigned char LIFE = '1';
- const unsigned char DEATH = ' ';
- unsigned char LifeBoard[WIDTH][HEIGHT];
- unsigned char LifeBoardCopy[WIDTH][HEIGHT];
- void ClearBoard()
- {
- for (int i=0; i<WIDTH; i++)
- for (int j=0; j<HEIGHT; j++)
- LifeBoard[i][j] = DEATH;
- }
- void GenerateBoard()
- {
- for (int i=0; i<WIDTH; i++)
- for (int j=0; j<HEIGHT; j++)
- LifeBoard[i][j] = rand() % 2 == 0 ? LIFE : DEATH;
- //ClearBoard();
- //LifeBoard[1][1] = LIFE;
- //LifeBoard[1][2] = LIFE;
- //LifeBoard[1][3] = LIFE;
- }
- void PrintBoard()
- {
- system("cls");
- system("color 02");
- for (int i=0; i<WIDTH; i++)
- {
- for (int j=0; j<HEIGHT; j++)
- cout << LifeBoard[i][j];
- cout << endl;
- }
- }
- void CopyBoard()
- {
- for (int i=0; i<WIDTH; i++)
- for (int j=0; j<HEIGHT; j++)
- LifeBoardCopy[i][j] = LifeBoard[i][j];
- }
- void CopyBoardReverse()
- {
- for (int i=0; i<WIDTH; i++)
- for (int j=0; j<HEIGHT; j++)
- LifeBoard[i][j] = LifeBoardCopy[i][j];
- }
- bool IsBoardEmpty()
- {
- for (int i=0; i<WIDTH; i++)
- for (int j=0; j<HEIGHT; j++)
- if (LifeBoard[i][j] == LIFE) return false;
- return true;
- }
- void UpdateBoard()
- {
- int celle_adiacenti = 0;
- for (int i=0; i<WIDTH; i++)
- {
- for (int j=0; j<HEIGHT; j++)
- {
- for (int k=0; k<8; k++)
- {
- if (k==0)
- if (j-1 >= 0)
- if (LifeBoard[i][j-1] == LIFE)
- celle_adiacenti++;
- if (k==1)
- if (i+1 < WIDTH && j-1 >= 0)
- if (LifeBoard[i+1][j-1] == LIFE)
- celle_adiacenti++;
- if (k==2)
- if (i+1 < WIDTH)
- if (LifeBoard[i+1][j] == LIFE)
- celle_adiacenti++;
- if (k==3)
- if (i+1 < WIDTH && j+1 < HEIGHT)
- if (LifeBoard[i+1][j+1] == LIFE)
- celle_adiacenti++;
- if (k==4)
- if (j+1 < HEIGHT)
- if (LifeBoard[i][j+1] == LIFE)
- celle_adiacenti++;
- if (k==5)
- if (i-1 >= 0 && j+1 < HEIGHT)
- if (LifeBoard[i-1][j+1] == LIFE)
- celle_adiacenti++;
- if (k==6)
- if (i-1 >= 0)
- if (LifeBoard[i-1][j] == LIFE)
- celle_adiacenti++;
- if (k==7)
- if (i-1 >= 0 && j-1 >= 0)
- if (LifeBoard[i-1][j-1] == LIFE)
- celle_adiacenti++;
- }
- if (LifeBoard[i][j] == DEATH)
- if (celle_adiacenti == 3)
- LifeBoardCopy[i][j] = LIFE;
- if (LifeBoard[i][j] == LIFE)
- {
- if (celle_adiacenti < 2)
- LifeBoardCopy[i][j] = DEATH;
- if (celle_adiacenti == 2 || celle_adiacenti == 3)
- LifeBoardCopy[i][j] = LIFE;
- if (celle_adiacenti > 3)
- LifeBoardCopy[i][j] = DEATH;
- }
- celle_adiacenti = 0;
- }
- }
- CopyBoardReverse();
- }
- int main()
- {
- srand((unsigned)time(0));
- GenerateBoard();
- CopyBoard();
- PrintBoard();
- while (!IsBoardEmpty())
- {
- UpdateBoard();
- PrintBoard();
- Sleep(250);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement