Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <cstdio>
- #include <stdlib.h>
- #include <string.h>
- #include <omp.h>
- #include <ctime>
- using namespace std;
- #define RES "\x1B[0m"
- #define RED "\x1B[41m"
- #define GRN "\x1B[42m"
- #define YEL "\x1B[43m"
- #define BLU "\x1B[44m"
- #define MAG "\x1B[45m"
- #define CYN "\x1B[46m"
- #define WHT "\x1B[47m"
- static unsigned int rand_double(unsigned int *seed, int maximum)
- {
- const unsigned int rand_double = (unsigned int)rand() % maximum;
- return rand_double;
- }
- void generate_start(int start, int ** stab, int XMAX, int YMAX)
- {
- int i = 0;
- unsigned int x = 0;
- unsigned int y = 0;
- unsigned int seed;
- srand(time(NULL));
- seed = rand();
- for (i; i < start; i++)
- {
- x = rand_double(&seed, XMAX - 1);
- y = rand_double(&seed, YMAX - 1);
- //printf("x=%d, y=%d\n", x, y);
- if (x != 0 && y != 0)
- {
- if (stab[x][y] != 1)
- {
- stab[x][y] = 1;
- }
- else
- {
- i--;
- }
- }
- else
- {
- i--;
- }
- }
- }
- int check_da_hood(int ** tab, int x, int y)
- {
- int counter = 0;
- if (tab[x][y] >= 1)
- { ///kom�rka jest �ywa
- for (int i = y - 1; i<y + 2; i++)
- {
- for (int j = x - 1; j<x + 2; j++)
- {
- if (tab[j][i] > 0)
- {
- counter++;
- }
- if (counter - 1 > 3)
- {
- return 0;
- }
- }
- }
- if (counter - 1 <= 3 && counter - 1 >= 2)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- { ///kom�rka jest martwa
- for (int i = y - 1; i<y + 2; i++)
- {
- for (int j = x - 1; j<x + 2; j++)
- {
- if (tab[j][i] > 0)
- {
- counter++;
- }
- if (counter > 3)
- {
- return 0;
- }
- }
- }
- if (counter == 3)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- void thread_part(int sp, int ep, int ymax, int ** ctab, int ** ntab)
- {
- int x, y;
- for (x = sp; x <= ep; x++)
- {
- //start_point = 1+((ymax-2)/4)*omp_get_thread_num();
- //end_point = ((ymax-2)/4)*omp_get_thread_num()+1;
- //printf("thread %d\n", omp_get_thread_num());
- //#pragma omp for schedule(dynamic, 2) nowait
- for (y = 1; y<ymax - 1; y++)
- {
- //printf("thread %d x= %d, y= %d\n", omp_get_thread_num(), x, y);
- //printf("Sprawdzam s�siad�w x=%d, y%d\n", x, y);
- ntab[x][y] = check_da_hood(ctab, x, y);
- //printf("x=%d, y=%d\n", x, y);
- }
- }
- }
- void genetare_new(int ** ctab, int ** ntab, int xmax, int ymax)
- {
- int start_point, end_point, x, y;
- printf("xmax= %d\n", xmax);
- #pragma omp parallel private(start_point, end_point, x, y) shared(ntab, ctab, xmax, ymax)/*private(x, y, start_point, end_point) shared(ntab, ctab, ymax)*/ num_threads(4)
- {
- start_point = 1 + ((xmax - 2) / 4)*omp_get_thread_num();
- end_point = ((xmax - 2) / 4)*(omp_get_thread_num() + 1);
- //printf("thread %d start= %d, end= %d\n", omp_get_thread_num(), start_point, end_point);
- if (omp_get_thread_num() == 3)
- {
- //printf("xmax= %d\n", xmax);
- end_point = xmax - 2;
- }
- printf("thread %d start= %d, end= %d\n", omp_get_thread_num(), start_point, end_point);
- thread_part(start_point, end_point, ymax, ctab, ntab);
- //#pragma omp for schedule(dynamic, 2) nowait
- /* for(x=1; x<xmax-1; x++)
- {
- //start_point = 1+((ymax-2)/4)*omp_get_thread_num();
- //end_point = ((ymax-2)/4)*omp_get_thread_num()+1;
- printf("thread %d\n", omp_get_thread_num());
- #pragma omp for schedule(dynamic, 2) nowait
- for(y=1; y<ymax-1; y++)
- {
- printf("thread %d x= %d, y= %d\n", omp_get_thread_num(), x, y);
- //printf("Sprawdzam s�siad�w x=%d, y%d\n", x, y);
- ntab[x][y] = chceck_da_hood(ctab, x, y);
- //printf("x=%d, y=%d\n", x, y);
- }
- }*/
- }
- }
- void minus_it(int **tab, int xmax, int ymax)
- {
- for (int x = 0; x<xmax; x++)
- {
- for (int y = 0; y<ymax; y++)
- {
- tab[x][y] = -1;
- }
- }
- }
- void show_tab(int ** tab, int xmax, int ymax)
- {
- for (int x = 0; x<xmax; x++)
- {
- for (int y = 0; y<ymax; y++)
- {
- if (tab[x][y] == 1)
- {
- printf(YEL " ");
- }
- else
- {
- if (tab[x][y] == -1)
- {
- printf(BLU " ");
- }
- else
- printf(WHT " ");
- }
- }
- printf(RES "\n");
- }
- }
- void kopiuj(int ** ntab, int ** ctab, int xmax, int ymax)
- {
- for (int x = 0; x<xmax; x++)
- {
- for (int y = 0; y<ymax; y++)
- {
- ctab[x][y] = ntab[x][y];
- }
- }
- }
- int main(int argc, char** argv)
- {
- int XMAX = 52;
- int YMAX = 52;
- int ** ctab;
- int ** ntab;
- int k, interval, iteracje;
- int i = 0;
- ctab = new int*[XMAX]();
- ntab = new int*[XMAX]();
- for (i = 0; i<XMAX; i++)
- {
- ctab[i] = new int[YMAX]();
- ntab[i] = new int[YMAX]();
- }
- generate_start(500, ctab, XMAX, YMAX);
- show_tab(ctab, XMAX, YMAX);
- printf("Generated Random Cells\n");
- i = 0;
- while (1)
- {
- printf("Press ENTER to generate another one or L to generate randomly \n");
- k = getchar();
- printf("Pressed: %d\n", k);
- if (k == 10)
- {
- ///pojedyncza iteracja
- iteracje = 1;
- interval = 0;
- }
- else
- {
- if (k == 108 || k == 78)
- {
- printf("How many tables to generate");
- cin >> iteracje;
- printf("Set table generation interval\n");
- cin >> interval;
- }
- else
- {
- break;
- }
- }
- printf("running...\n");
- for (i = 0; i<iteracje; i++)
- {
- //show_tab(ntab, XMAX, YMAX);
- minus_it(ntab, XMAX, YMAX);
- //show_tab(ntab, XMAX, YMAX);
- //printf("Minusy wpisane\n");
- genetare_new(ctab, ntab, XMAX, YMAX);
- //printf("Wygenerowano now�\n");
- //show_tab(ntab, XMAX, YMAX);
- //memcpy(ctab, ntab, XMAX*YMAX * sizeof(int));
- kopiuj(ntab, ctab, XMAX, YMAX);
- //printf("Skopiowano wynik do obecnej\n");
- show_tab(ctab, XMAX, YMAX);
- //i++;
- _sleep(interval);
- }
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement