Posted By

autofasurer on 10/27/10


Tagged

random array terminal sleep multidimensional


Versions (?)

Game of life


 / Published in: C
 

URL: http://www.autofasurer.net/wp

First iteration of an ASCII game of life, running in the terminal (on OS X, don't know what it does on other systems)

  1. #include <stdio.h> //standard input/output
  2. #include <stdlib.h> // for the rand() and srand() function
  3. #include <time.h> // for seeding the rand() function with time()
  4. #include <unistd.h> //for the sleep function
  5. #include <math.h> // for the square root function
  6.  
  7. int i, j, k, telOp;
  8. int arraySize, iterations;
  9. int square[50][50];
  10. int squareTemp[50][50];
  11. void initRand()
  12. {
  13. srand((unsigned)(time(0)));
  14. }
  15.  
  16. void createMap(){
  17. for (i = 0; i < arraySize; i++) {
  18. for (j = 0; j < arraySize; j++) {
  19. square[i][j] = rand()%2;
  20. squareTemp[i][j] = 0;
  21. //**print coordinates**//
  22. //printf("%i, %i\n", i, j);
  23. }
  24. }
  25. }
  26.  
  27. void checkMap(){
  28. int iMin,iPlus,jMin,jPlus;
  29.  
  30. for (i = 0; i < arraySize; i++) {
  31. for (j = 0; j < arraySize; j++) {
  32. if (i == 0){ iMin = arraySize; } else {iMin = i-1;}
  33. if (i == arraySize){ iPlus = 0;} else {iPlus = i+1;}
  34. if (j == 0){ jMin = arraySize; } else {jMin = j-1;}
  35. if (j == arraySize){ jPlus = 0;} else {jPlus = j+1;}
  36.  
  37. telOp = square[iMin][jMin]+
  38. square[i][jMin]+
  39. square[iPlus][jMin]+
  40. square[iMin][j]+
  41. square[iPlus][j]+
  42. square[iMin][jPlus]+
  43. square[i][jPlus]+
  44. square[iPlus][jPlus];
  45. if (telOp < 2){
  46. squareTemp[i][j] = 0;
  47. }
  48. else if (telOp > 3){
  49. squareTemp[i][j] = 0;
  50. }
  51. else if (telOp == 3){
  52. squareTemp[i][j] = 1;
  53. }
  54. else if (telOp == 2){
  55. squareTemp[i][j] = square[i][j];
  56. }
  57. //printf("telop = %i\n", telOp);
  58.  
  59. }
  60.  
  61. }
  62.  
  63. for (i = 0; i < arraySize; i++) {
  64. for (j = 0; j < arraySize; j++) {
  65. square[i][j] = squareTemp[i][j];
  66. }
  67. }
  68. }
  69.  
  70.  
  71. void printMap(){
  72. system("clear");
  73.  
  74. for (i = 0; i < arraySize; i++) {
  75. for (j = 0; j < arraySize; j++) {
  76. if (square[i][j] == 0) {
  77. printf("..");}
  78. else {
  79. printf("[]");
  80. }
  81.  
  82. }
  83. printf("\n");
  84. }
  85. }
  86.  
  87.  
  88. int main (int argc, const char * argv[]) {
  89. // insert code here...
  90. //Initialize the map
  91. printf("What grid size would you like? (1 - 50): ");
  92. scanf("%i", &arraySize);
  93. printf("How many iterations would you like? ");
  94. scanf("%i", &iterations);
  95. if (arraySize <= 50 && arraySize > 0) {
  96. initRand();
  97. createMap();
  98.  
  99. for (k = 0; k < iterations; k++) {
  100. checkMap();
  101. printMap();
  102. usleep(20000);
  103. }
  104.  
  105. }
  106.  
  107. //printf("%i",sizeof(square) / sizeof(int));
  108. return 0;
  109. }

Report this snippet  

You need to login to post a comment.