Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <omp.h>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. #define RES "\x1B[0m"
  12. #define RED "\x1B[41m"
  13. #define GRN "\x1B[42m"
  14. #define YEL "\x1B[43m"
  15. #define BLU "\x1B[44m"
  16. #define MAG "\x1B[45m"
  17. #define CYN "\x1B[46m"
  18. #define WHT "\x1B[47m"
  19.  
  20.  
  21. static unsigned int rand_double(unsigned int *seed, int maximum)
  22. {
  23. const unsigned int rand_double = (unsigned int)rand() % maximum;
  24. return rand_double;
  25. }
  26.  
  27. void generate_start(int start, int ** stab, int XMAX, int YMAX)
  28. {
  29. int i = 0;
  30. unsigned int x = 0;
  31. unsigned int y = 0;
  32. unsigned int seed;
  33. srand(time(NULL));
  34. seed = rand();
  35.  
  36. for (i; i < start; i++)
  37. {
  38. x = rand_double(&seed, XMAX - 1);
  39. y = rand_double(&seed, YMAX - 1);
  40. //printf("x=%d, y=%d\n", x, y);
  41. if (x != 0 && y != 0)
  42. {
  43. if (stab[x][y] != 1)
  44. {
  45. stab[x][y] = 1;
  46. }
  47. else
  48. {
  49. i--;
  50. }
  51. }
  52. else
  53. {
  54. i--;
  55. }
  56. }
  57. }
  58.  
  59. int check_da_hood(int ** tab, int x, int y)
  60. {
  61. int counter = 0;
  62. if (tab[x][y] >= 1)
  63. { ///kom�rka jest �ywa
  64. for (int i = y - 1; i<y + 2; i++)
  65. {
  66. for (int j = x - 1; j<x + 2; j++)
  67. {
  68. if (tab[j][i] > 0)
  69. {
  70. counter++;
  71. }
  72. if (counter - 1 > 3)
  73. {
  74. return 0;
  75. }
  76. }
  77. }
  78. if (counter - 1 <= 3 && counter - 1 >= 2)
  79. {
  80. return 1;
  81. }
  82. else
  83. {
  84. return 0;
  85. }
  86. }
  87. else
  88. { ///kom�rka jest martwa
  89. for (int i = y - 1; i<y + 2; i++)
  90. {
  91. for (int j = x - 1; j<x + 2; j++)
  92. {
  93. if (tab[j][i] > 0)
  94. {
  95. counter++;
  96. }
  97. if (counter > 3)
  98. {
  99. return 0;
  100. }
  101. }
  102. }
  103. if (counter == 3)
  104. {
  105. return 1;
  106. }
  107. else
  108. {
  109. return 0;
  110. }
  111. }
  112. }
  113.  
  114. void thread_part(int sp, int ep, int ymax, int ** ctab, int ** ntab)
  115. {
  116. int x, y;
  117.  
  118. for (x = sp; x <= ep; x++)
  119. {
  120. //start_point = 1+((ymax-2)/4)*omp_get_thread_num();
  121. //end_point = ((ymax-2)/4)*omp_get_thread_num()+1;
  122. //printf("thread %d\n", omp_get_thread_num());
  123. //#pragma omp for schedule(dynamic, 2) nowait
  124. for (y = 1; y<ymax - 1; y++)
  125. {
  126. //printf("thread %d x= %d, y= %d\n", omp_get_thread_num(), x, y);
  127. //printf("Sprawdzam s�siad�w x=%d, y%d\n", x, y);
  128. ntab[x][y] = check_da_hood(ctab, x, y);
  129. //printf("x=%d, y=%d\n", x, y);
  130. }
  131. }
  132. }
  133.  
  134. void genetare_new(int ** ctab, int ** ntab, int xmax, int ymax)
  135. {
  136. int start_point, end_point, x, y;
  137. printf("xmax= %d\n", xmax);
  138. #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)
  139. {
  140. start_point = 1 + ((xmax - 2) / 4)*omp_get_thread_num();
  141. end_point = ((xmax - 2) / 4)*(omp_get_thread_num() + 1);
  142. //printf("thread %d start= %d, end= %d\n", omp_get_thread_num(), start_point, end_point);
  143. if (omp_get_thread_num() == 3)
  144. {
  145. //printf("xmax= %d\n", xmax);
  146. end_point = xmax - 2;
  147. }
  148. printf("thread %d start= %d, end= %d\n", omp_get_thread_num(), start_point, end_point);
  149. thread_part(start_point, end_point, ymax, ctab, ntab);
  150. //#pragma omp for schedule(dynamic, 2) nowait
  151. /* for(x=1; x<xmax-1; x++)
  152. {
  153. //start_point = 1+((ymax-2)/4)*omp_get_thread_num();
  154. //end_point = ((ymax-2)/4)*omp_get_thread_num()+1;
  155. printf("thread %d\n", omp_get_thread_num());
  156. #pragma omp for schedule(dynamic, 2) nowait
  157. for(y=1; y<ymax-1; y++)
  158. {
  159. printf("thread %d x= %d, y= %d\n", omp_get_thread_num(), x, y);
  160. //printf("Sprawdzam s�siad�w x=%d, y%d\n", x, y);
  161. ntab[x][y] = chceck_da_hood(ctab, x, y);
  162. //printf("x=%d, y=%d\n", x, y);
  163. }
  164. }*/
  165. }
  166. }
  167.  
  168. void minus_it(int **tab, int xmax, int ymax)
  169. {
  170. for (int x = 0; x<xmax; x++)
  171. {
  172. for (int y = 0; y<ymax; y++)
  173. {
  174. tab[x][y] = -1;
  175. }
  176. }
  177. }
  178.  
  179. void show_tab(int ** tab, int xmax, int ymax)
  180. {
  181.  
  182. for (int x = 0; x<xmax; x++)
  183. {
  184. for (int y = 0; y<ymax; y++)
  185. {
  186. if (tab[x][y] == 1)
  187. {
  188. printf(YEL " ");
  189. }
  190. else
  191. {
  192. if (tab[x][y] == -1)
  193. {
  194. printf(BLU " ");
  195. }
  196. else
  197. printf(WHT " ");
  198. }
  199. }
  200. printf(RES "\n");
  201. }
  202. }
  203.  
  204. void kopiuj(int ** ntab, int ** ctab, int xmax, int ymax)
  205. {
  206. for (int x = 0; x<xmax; x++)
  207. {
  208. for (int y = 0; y<ymax; y++)
  209. {
  210. ctab[x][y] = ntab[x][y];
  211. }
  212. }
  213. }
  214.  
  215. int main(int argc, char** argv)
  216. {
  217. int XMAX = 52;
  218. int YMAX = 52;
  219. int ** ctab;
  220. int ** ntab;
  221. int k, interval, iteracje;
  222. int i = 0;
  223.  
  224. ctab = new int*[XMAX]();
  225. ntab = new int*[XMAX]();
  226.  
  227. for (i = 0; i<XMAX; i++)
  228. {
  229. ctab[i] = new int[YMAX]();
  230. ntab[i] = new int[YMAX]();
  231. }
  232. generate_start(500, ctab, XMAX, YMAX);
  233.  
  234. show_tab(ctab, XMAX, YMAX);
  235. printf("Generated Random Cells\n");
  236. i = 0;
  237. while (1)
  238. {
  239.  
  240.  
  241. printf("Press ENTER to generate another one or L to generate randomly \n");
  242. k = getchar();
  243. printf("Pressed: %d\n", k);
  244. if (k == 10)
  245. {
  246. ///pojedyncza iteracja
  247. iteracje = 1;
  248. interval = 0;
  249.  
  250. }
  251. else
  252. {
  253. if (k == 108 || k == 78)
  254. {
  255. printf("How many tables to generate");
  256. cin >> iteracje;
  257. printf("Set table generation interval\n");
  258. cin >> interval;
  259. }
  260. else
  261. {
  262. break;
  263. }
  264. }
  265.  
  266. printf("running...\n");
  267. for (i = 0; i<iteracje; i++)
  268. {
  269. //show_tab(ntab, XMAX, YMAX);
  270. minus_it(ntab, XMAX, YMAX);
  271. //show_tab(ntab, XMAX, YMAX);
  272. //printf("Minusy wpisane\n");
  273. genetare_new(ctab, ntab, XMAX, YMAX);
  274. //printf("Wygenerowano now�\n");
  275. //show_tab(ntab, XMAX, YMAX);
  276. //memcpy(ctab, ntab, XMAX*YMAX * sizeof(int));
  277. kopiuj(ntab, ctab, XMAX, YMAX);
  278. //printf("Skopiowano wynik do obecnej\n");
  279. show_tab(ctab, XMAX, YMAX);
  280. //i++;
  281. _sleep(interval);
  282. }
  283. }
  284. system("PAUSE");
  285. return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement