Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. #include <iostream>
  2. #define CIMGGIP_MAIN
  3. #include "CImgGIP05.h"
  4. using namespace std;
  5. using namespace cimg_library;
  6.  
  7. const int grid_size = 10; // Anzahl an Kaestchen in x- und y-Richtung
  8. const int box_size = 30; // size der einzelnen Kaestchen (in Pixel)
  9. const int border = 20; // Rand links und oben bis zu den ersten Kaestchen (in Pixel)
  10.  
  11. // Prototyp der Funktionen zum Vorbelegen des Grids ...
  12. void grid_init(bool grid[][grid_size]);
  13.  
  14. int main()
  15. {
  16. bool grid[grid_size][grid_size] = { 0 };
  17. bool next_grid[grid_size][grid_size] = { 0 };
  18.  
  19. // Erstes Grid vorbelegen
  20. grid_init(grid);
  21.  
  22. while (gip_window_not_closed())
  23. {
  24. // Spielfeld anzeigen
  25. // Ggfs gip_stop_updates();
  26. // TO DO
  27. gip_stop_updates();
  28. gip_draw_rectangle(border, border, border + grid_size * box_size, border + grid_size * box_size, white);
  29. for (int y = 0; y < grid_size; y++)
  30. {
  31. for (int x = 0; x < grid_size; x++)
  32. {
  33.  
  34. if (grid[x][y] == true)
  35. {
  36. gip_draw_rectangle(border + x * box_size, border + y * box_size, border + (x + 1) * box_size, border + (y + 1) * box_size, green);
  37. }
  38. }
  39. }
  40. gip_start_updates();
  41. gip_sleep(3);
  42.  
  43. // Berechne das naechste Spielfeld ...
  44. // Achtung; Fьr die Zelle (x,y) darf die Position (x,y) selbst *nicht*
  45. // mit in die Betrachtungen einbezogen werden.
  46. // Ausserdem darf bei zellen am rand nicht ьber den Rand hinausgegriffen
  47. // werden (diese Zellen haben entsprechend weniger Nachbarn) ...
  48.  
  49. // TO DO
  50. for (int y = 0; y < grid_size; y++)
  51. {
  52. for (int x = 0; x < grid_size; x++)
  53. {
  54. int living_neighbors = 0;
  55. for (int y1 = 0; y1 < 3; y1++)
  56. {
  57. for (int x1 = 0; x1 < 3; x1++)
  58. {
  59. if (x - 1 + x1 < 0 || x - 1 + x1 >= grid_size || y - 1 + y1 < 0 || y - 1 + y1 >= grid_size) //Abfangen der Werte, die ueber dem Rand des arrays sind
  60. {
  61. //Falls man den Rand von Tot auf lebendig aendern will
  62. //living_neighbors++;
  63. }
  64. else if (x1 == 1 && y1 == 1)
  65. {
  66. //sich selber ausser betracht zeihen
  67. }
  68. else if (grid[x - 1 + x1][y - 1 + y1] == true) //zaehlen der Lebenden Nachbarn
  69. {
  70. living_neighbors++;
  71. }
  72. }
  73. }
  74. if (grid[x][y] == false && living_neighbors == 3)
  75. next_grid[x][y] = true;
  76. else if (living_neighbors < 2 || living_neighbors > 3)
  77. {
  78. next_grid[x][y] = false;
  79. }
  80. else
  81. next_grid[x][y] = grid[x][y];
  82. }
  83. }
  84.  
  85. // Kopiere das naechste Spielfeld in das aktuelle Spielfeld ...
  86.  
  87. // TO DO
  88. for (int y = 0; y < grid_size; y++)
  89. {
  90. for (int x = 0; x < grid_size; x++)
  91. {
  92. grid[x][y] = next_grid[x][y];
  93. }
  94. }
  95.  
  96. }
  97. return 0;
  98. }
  99.  
  100. void grid_init(bool grid[][grid_size])
  101. {
  102. int eingabe = -1;
  103. do {
  104. cout << "Bitte waehlen Sie die Vorbelegung des Grids aus:" << endl
  105. << "0 - Zufall" << endl
  106. << "1 - Statisch" << endl
  107. << "2 - Blinker" << endl
  108. << "3 - Oktagon" << endl
  109. << "4 - Gleiter" << endl
  110. << "5 - Segler 1 (Light-Weight Spaceship)" << endl
  111. << "6 - Segler 2 (Middle-Weight Spaceship)" << endl
  112. << "? ";
  113. cin >> eingabe;
  114. cin.clear();
  115. cin.ignore(1000, '\n');
  116. } while (eingabe < 0 || eingabe > 6);
  117.  
  118. if (eingabe == 0)
  119. {
  120. // Erstes Grid vorbelegen (per Zufallszahlen) ...
  121.  
  122. // TO DO
  123. for (int y = 0; y < grid_size; y++)
  124. for (int x = 0; x < grid_size; x++)
  125. grid[x][y] = gip_random(0, 1);
  126. }
  127. else if (eingabe == 1)
  128. {
  129. const int pattern_size = 3;
  130. char pattern[pattern_size][pattern_size] =
  131. {
  132. { '.', '*', '.' },
  133. { '*', '.', '*' },
  134. { '.', '*', '.' },
  135. };
  136. for (int y = 0; y < pattern_size; y++)
  137. for (int x = 0; x < pattern_size; x++)
  138. if (pattern[y][x] == '*')
  139. grid[x][y + 3] = true;
  140. }
  141. else if (eingabe == 2)
  142. {
  143. const int pattern_size = 3;
  144. char pattern[pattern_size][pattern_size] =
  145. {
  146. { '.', '*', '.' },
  147. { '.', '*', '.' },
  148. { '.', '*', '.' },
  149. };
  150. for (int y = 0; y < pattern_size; y++)
  151. for (int x = 0; x < pattern_size; x++)
  152. if (pattern[y][x] == '*')
  153. grid[x][y + 3] = true;
  154. }
  155. else if (eingabe == 3)
  156. {
  157. const int pattern_size = 8;
  158. char pattern[pattern_size][pattern_size] =
  159. {
  160. { '.', '.', '.', '*', '*', '.', '.', '.' },
  161. { '.', '.', '*', '.', '.', '*', '.', '.' },
  162. { '.', '*', '.', '.', '.', '.', '*', '.' },
  163. { '*', '.', '.', '.', '.', '.', '.', '*' },
  164. { '*', '.', '.', '.', '.', '.', '.', '*' },
  165. { '.', '*', '.', '.', '.', '.', '*', '.' },
  166. { '.', '.', '*', '.', '.', '*', '.', '.' },
  167. { '.', '.', '.', '*', '*', '.', '.', '.' },
  168. };
  169. for (int y = 0; y < pattern_size; y++)
  170. for (int x = 0; x < pattern_size; x++)
  171. if (pattern[y][x] == '*')
  172. grid[x][y + 1] = true;
  173. }
  174. else if (eingabe == 4)
  175. {
  176. const int pattern_size = 3;
  177. char pattern[pattern_size][pattern_size] =
  178. {
  179. { '.', '*', '.' },
  180. { '.', '.', '*' },
  181. { '*', '*', '*' },
  182. };
  183. for (int y = 0; y < pattern_size; y++)
  184. for (int x = 0; x < pattern_size; x++)
  185. if (pattern[y][x] == '*')
  186. grid[x][y + 3] = true;
  187. }
  188. else if (eingabe == 5)
  189. {
  190. const int pattern_size = 5;
  191. char pattern[pattern_size][pattern_size] =
  192. {
  193. { '*', '.', '.', '*', '.' },
  194. { '.', '.', '.', '.', '*' },
  195. { '*', '.', '.', '.', '*' },
  196. { '.', '*', '*', '*', '*' },
  197. { '.', '.', '.', '.', '.' },
  198. };
  199. for (int y = 0; y < pattern_size; y++)
  200. for (int x = 0; x < pattern_size; x++)
  201. if (pattern[y][x] == '*')
  202. grid[x][y + 3] = true;
  203. }
  204. else if (eingabe == 6)
  205. {
  206. const int pattern_size = 6;
  207. char pattern[pattern_size][pattern_size] =
  208. {
  209. { '.', '*', '*', '*', '*', '*' },
  210. { '*', '.', '.', '.', '.', '*' },
  211. { '.', '.', '.', '.', '.', '*' },
  212. { '*', '.', '.', '.', '*', '.' },
  213. { '.', '.', '*', '.', '.', '.' },
  214. { '.', '.', '.', '.', '.', '.' },
  215. };
  216. for (int y = 0; y < pattern_size; y++)
  217. for (int x = 0; x < pattern_size; x++)
  218. if (pattern[y][x] == '*')
  219. grid[x][y + 3] = true;
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement