Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <stdlib.h>
- #define DELAY 300
- #define WIDTH 240
- #define HEIGHT 66
- using namespace std;
- void updateField(bool field[WIDTH][HEIGHT]);
- void printField(bool field[WIDTH][HEIGHT]);
- void randomizeField(bool field[WIDTH][HEIGHT]);
- bool field[WIDTH][HEIGHT];
- const char BLCK = 219;
- int main(){
- chrono::milliseconds dura(DELAY);
- randomizeField(field);
- while(true){
- printField(field);
- this_thread::sleep_for(dura);
- updateField(field);
- }
- return 0;
- }
- void randomizeField(bool field[WIDTH][HEIGHT]){
- for(register int y = 0; y < HEIGHT; y++)
- for(register int x = 0; x < WIDTH; x++)
- if(rand() % 100 < 9) field[x][y] = true;
- }
- void printField(bool field[WIDTH][HEIGHT]){
- for(register int y = 0; y < HEIGHT; y++){
- for(register int x = 0; x < WIDTH; x++){
- if(field[x][y]) cout << BLCK;
- else cout << ' ';
- }
- cout << '\n';
- }
- }
- void updateField(bool field[WIDTH][HEIGHT]){
- int neighbors[WIDTH][HEIGHT];
- for(register int y = 0; y < HEIGHT; y++)
- for(register int x = 0; x < WIDTH; x++)
- neighbors[x][y] = 0;
- for(register int y = 0; y < HEIGHT; y++){
- for(register int x = 0; x < WIDTH; x++){
- if(field[x][y]){
- for(int x2 = x - 1; x2 <= x + 1; x2++){
- for(int y2 = y - 1; y2 <= y + 1; y2++){
- if(x2 == x && y2 == y) continue;
- int x3 = x2 % WIDTH, y3 = y2 % HEIGHT;
- if(x3 < 0) x3 += WIDTH;
- if(y3 < 0) y3 += HEIGHT;
- neighbors[x3][y3]++;
- }
- }
- }
- }
- }
- for(register int y = 0; y < HEIGHT; y++){
- for(register int x = 0; x < WIDTH; x++){
- if(field[x][y]){
- int neighb = neighbors[x][y];
- if(neighb == 2 || neighb == 3) field[x][y] = true;
- else field[x][y] = false;
- }
- else{
- if(neighbors[x][y] == 3) field[x][y] = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement