Advertisement
jirkavavrik

conway

Apr 18th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2014 Jirka Vavřík
  3. *
  4. * Licensed under the NWL - No Whining License.
  5. *
  6. * You may use this, modify this, redistribute this provided you agree:
  7. * - not to whine about this product from Jirka Vavřík
  8. * - that there is no warranty of any kind.
  9. * - retain this copyright in full.
  10.  
  11.  
  12. please compile with -g
  13. */
  14.  
  15. #ifdef __cplusplus
  16.     #include <iostream>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22.  
  23. //this is for sleep function - its different ín Windows/UNIX
  24. #ifdef WIN32
  25.     #include <windows.h>
  26. #else
  27.     #include <unistd.h>
  28. #endif
  29.  
  30. short count_x;
  31. short count_y;
  32.  
  33. short actual_state[256][256];
  34.  
  35. void cls() {//the easiest solution
  36. #ifdef WIN32
  37.     system("cls");
  38. #else
  39.     system("clear");
  40. #endif
  41. }
  42.  
  43. short check_life(short, short);
  44.  
  45. int main()
  46. {
  47. int field;
  48. char _field[3];
  49. srand(time(NULL));
  50.  
  51.     entering_dimensions:
  52.     printf("\tField size: ");
  53.     #ifdef __cplusplus
  54.         std::cin >> field;
  55.     #else
  56.         fgets(_field, 4, stdin);
  57.         field = atoi(_field);
  58.     #endif
  59.     count_y = field;
  60.     count_x = field;
  61.     if( count_x > 256 || count_y > 256) {
  62.     printf("Number must not be more than 256!\n");
  63.     goto entering_dimensions;
  64.     }
  65.     short tmp_y;
  66.     short tmp_x;
  67.     for( tmp_y = 0; tmp_y < count_y; tmp_y++) {
  68.         for( tmp_x = 0; tmp_x < count_x; tmp_x++) {
  69.             short random = rand() % 2;
  70.             if(random == 1) {
  71.                 actual_state[tmp_x][tmp_y] = 1;
  72.             } else {
  73.                 actual_state[tmp_x][tmp_y] = 0;
  74.             }
  75.  
  76.         }
  77.     }
  78.  
  79.     begin:
  80.     cls();
  81.     short y_ax;
  82.     short x_ax;
  83.     for( y_ax = 0; y_ax < count_y; y_ax++) {
  84.         for(x_ax = 0; x_ax < count_x; x_ax++) {
  85.             if(actual_state[x_ax][y_ax] == 1) {
  86.                 printf("#");
  87.             } else if(actual_state[x_ax][y_ax] == 0){
  88.                 printf(" ");
  89.             }
  90.         }
  91.     printf("\n");
  92.     }
  93.  
  94.  
  95.     short next_state[256][256];
  96.  
  97.     for( y_ax = 0; y_ax < count_y; y_ax++) {
  98.         for( x_ax = 0; x_ax < count_x; x_ax++) {
  99.             next_state[x_ax][y_ax] = check_life(x_ax, y_ax);
  100.         }
  101.     }
  102.  
  103.     for(y_ax = 0; y_ax < count_y; y_ax++) {
  104.         for(x_ax = 0; x_ax < count_x; x_ax++) {
  105.             actual_state[x_ax][y_ax] = next_state[x_ax][y_ax];
  106.         }
  107.     }
  108.     //vait 1 second
  109.     #ifdef WIN32
  110.         Sleep(1000);
  111.     #else
  112.         sleep(1);
  113.     #endif
  114.     goto begin;
  115. }
  116.  
  117. short check_life(short x, short y) {
  118. short surr;
  119.  
  120. if(x == 0 && y == 0) {
  121. surr = actual_state[x+1][y] + actual_state[x+1][y+1] + actual_state[x][y+1];
  122. }
  123. else if (x == 0 && y != 0 && x != count_x) {
  124. surr = actual_state[x-1][y] + actual_state[x-1][y+1] + actual_state[x][y+1] + actual_state[x+1][y+1] + actual_state[x][y+1];
  125. }
  126. else if(x == count_x && y == 0) {
  127. surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
  128. }
  129. else if(y > 0 && x == 0 && y < count_y) {
  130. surr = actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y] + actual_state[x+1][y+1] + actual_state[x][y+1];
  131. }
  132. else if(x > 0 && y > 0 && x < count_x && y < count_y) {
  133. surr = actual_state[x-1][y-1] + actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[y][x+1] + actual_state[y+1][x+1] + actual_state[x][y+1] + actual_state[x-1][y+1] + actual_state[x-1][y];
  134. }
  135. else if(x == count_x && y > 0 && y != count_y) {
  136. surr = actual_state[x][y+1] + actual_state[x-1][y-1] + actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
  137. }
  138. else if(x == 0 && y == count_y) {
  139. surr = actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y];
  140. }
  141. else if(x > 0 && x != count_x && y == count_y) {
  142. surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1] + actual_state[x+1][y-1] + actual_state[x+1][y];
  143. }
  144. else if(x == count_x && y == count_y) {
  145. surr = actual_state[x-1][y] + actual_state[x-1][y-1] + actual_state[x][y-1];
  146. }
  147.  
  148. if (actual_state[x][y] == 1 && surr < 2)
  149.     return 0;
  150. else if (actual_state[x][y] == 1 && (surr == 2 || surr == 3) )
  151.     return 1;
  152. else if(actual_state[x][y] == 1 && surr > 3)
  153.     return 0;
  154. else if(actual_state[x][y] == 0 && surr == 3)
  155.     return 1;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement