fork download
  1. /*
  2. ** A quick "life" (2-d cellular automaton) implementation done in Turbo C 2.0
  3. ** on the spur-of-the-moment by Jonathan Guthrie 9/20/1992 and donated to the
  4. ** public domain.
  5. **
  6. ** In keeping with the guidelines of the C_ECHO, this program has been tested,
  7. ** and does seem to operate properly.
  8. */
  9.  
  10. /* Modified into a native Win32 program, July 2001 by Jerry Coffin.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include <conio.h>
  17. #define WIN32_LEAN_AND_MEAN
  18. #include <windows.h>
  19.  
  20. #ifndef random
  21. #define random(num) (int)(((long)rand()*(num))/RAND_MAX)
  22. #endif
  23.  
  24. #ifndef randomize
  25. #define randomize() srand(((unsigned int)time(NULL))|1)
  26. #endif
  27.  
  28. #define ROWS 50
  29. #define COLS 80
  30. #define GENERATIONS 500
  31.  
  32. int civ1[ROWS+2][COLS+2], civ2[ROWS+2][COLS+2];
  33. CHAR_INFO disp[ROWS][COLS];
  34. HANDLE console;
  35. COORD size = { COLS, ROWS };
  36. COORD src = { 0, 0};
  37. SMALL_RECT dest = { 0, 0, COLS, ROWS };
  38.  
  39. void fill_edges(int civ1[ROWS+2][COLS+2]);
  40.  
  41. void ClrScrn(int attrib) {
  42.  
  43. HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
  44. COORD pos = { 0, 0};
  45. DWORD written;
  46. CONSOLE_SCREEN_BUFFER_INFO screen_attr;
  47. unsigned size;
  48.  
  49. GetConsoleScreenBufferInfo(screen, &screen_attr);
  50.  
  51. size = screen_attr.dwSize.X * screen_attr.dwSize.Y;
  52.  
  53. FillConsoleOutputCharacter( screen,attrib, size, pos, &written);
  54. SetConsoleCursorPosition(console, pos);
  55. }
  56.  
  57. void update_generation(int old[ROWS][COLS], int new[ROWS][COLS])
  58. {
  59. int i, j, count;
  60.  
  61. for (i = 1; i <= ROWS; ++i)
  62. {
  63. for (j = 1; j <= COLS; ++j)
  64. {
  65. count = old[(i + ROWS - 1) % ROWS][(j + COLS - 1) % COLS] +
  66. old[(i + ROWS - 1) % ROWS][j] +
  67. old[(i + ROWS - 1) % ROWS][(j + 1) % COLS] +
  68. old[i][(j + COLS - 1) % COLS] +
  69. old[i][(j + 1) % COLS] +
  70. old[(i + 1) % ROWS][(j + COLS - 1) % COLS] +
  71. old[(i + 1) % ROWS][j] +
  72. old[(i + 1) % ROWS][(j + 1) % COLS];
  73.  
  74. switch(count)
  75. {
  76. default:
  77. new[i][j] = 0;
  78. disp[i][j].Char.AsciiChar = ' ';
  79. break;
  80.  
  81. case 2:
  82. new[i][j] = old[i][j];
  83. break;
  84.  
  85. case 3:
  86. new[i][j] = 1;
  87. disp[i][j].Char.AsciiChar = '*';
  88. break;
  89. }
  90. }
  91. }
  92. WriteConsoleOutput(console,disp, size, src, &dest);
  93. }
  94.  
  95.  
  96. void initialize(void)
  97. {
  98. int i, j;
  99.  
  100. ClrScrn(0x71);
  101. randomize();
  102.  
  103. for (i = 1; i <= ROWS; ++i)
  104. {
  105. for (j = 1; j <= COLS; ++j)
  106. {
  107. civ1[i][j] = random(2);
  108. disp[i][j].Char.AsciiChar = civ1[i][j] ? '*' : ' ';
  109. disp[i][j].Attributes = 0x71;
  110. }
  111. }
  112. WriteConsoleOutput(console,disp, size, src, &dest);
  113. fill_edges(civ1);
  114. }
  115.  
  116. void fill_edges(int civ1[ROWS+2][COLS+2]) {
  117. int i;
  118.  
  119. for (i=1; i<ROWS; ++i) {
  120. civ1[i][0] = civ1[i][ROWS+1];
  121. civ1[i][ROWS+2] = civ[i][1];
  122. }
  123. for (j=1; j<COLS; ++j) {
  124. civ1[0][j] = civ1[COLS+1][j];
  125. civ1[COLS+2][j] = civ1[1][j];
  126. }
  127. civ1[0][0] = civ1[COLS+1][ROWS+1];
  128. civ1[COLS+2][ROWS+2] = civ1[1][1];
  129. civ1[COLS+2][0] = civ1[
  130.  
  131. }
  132.  
  133.  
  134. int main(void)
  135. {
  136. int i;
  137.  
  138. console = (HANDLE)_get_osfhandle(_fileno(stdout));
  139. initialize();
  140. for (i = 0; i < GENERATIONS; ++i)
  141. {
  142. update_generation(civ1, civ2);
  143. update_generation(civ2, civ1);
  144. }
  145. // getch();
  146. return EXIT_SUCCESS;
  147. }
  148.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:16:19: fatal error: conio.h: No such file or directory
 #include <conio.h>
                   ^
compilation terminated.
stdout
Standard output is empty