Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. /* DNA.cpp
  2. *
  3. * This automata was designed by Christopher G. Langton at the University
  4. * of Michigan, and is described in his article "Self-Reproduction in
  5. * Cellular Automata", published in the book "Cellular Automata" in 1984.
  6. *
  7. * As described in the above article, this automata is "realistic" and
  8. * "non-trivial", in the following ways:
  9. *
  10. * The reproduction is not simply due to the particular rules used in
  11. * the automata. Many automata can reproduce patterns, but such
  12. * behavior is a simple mathematical property of the rules, and is
  13. * not based on the starting pattern. In this automata, the correct
  14. * starting pattern is essential, otherwise nothing interesting happens.
  15. * True, the rules are VERY convenient for this pattern, but the rules
  16. * themselves do not make the reproduction occur. It is the rules
  17. * acting on this special pattern which enables reproduction (just
  18. * like our own universe!)
  19. *
  20. * Like real organisms, this pattern contains "DNA" which directs the
  21. * reproduction. The "DNA" is processed in two ways, by replicating
  22. * it and by transcribing it. This is necessary for "true" reproduction.
  23. *
  24. * Like bacteria, the reproduction doesn't simply create another copy
  25. * of the starting pattern. It creates two identical copies of
  26. * itself, thus both the parent and the child can continue to make
  27. * new copies of themselves.
  28. *
  29. * There are two questions I have about this automata:
  30. *
  31. * The first one is whether larger patterns can be made with these rules
  32. * which can also reproduce, and whether or not there are any interesting
  33. * "mutations" of the DNA in such patterns to make other interesting things.
  34. * It seems to me that it should be possible to "double" the size of the
  35. * pattern, for example.
  36. *
  37. * The second one is whether a simple change to the rules will allow the
  38. * pattern to run as it does now, EXCEPT that when the pattern "dies"
  39. * because of overcrowding, it would totally disappear, so that another
  40. * copy of the pattern could reuse its space. This would me more interesting.
  41. *
  42. * I hope you enjoy watching this run! If you can answer either of the
  43. * above questions, please let me know.
  44. * - David I. Bell -
  45. * dbell@pdact.pd.necisa.oz.au
  46. */
  47.  
  48. #include "stdafx.h"
  49.  
  50. #define STATES 8
  51. #define STATESIZE (STATES * STATES * STATES * STATES * STATES)
  52. #define STATEINDEX(a,b,c,d,e) \
  53. (((((a) * STATES + (b)) * STATES + (c)) * STATES + (d)) * STATES + (e))
  54.  
  55. /*
  56. * The rules for the automata: The automata is run on a square grid, with
  57. * a neighborhood of the nearest four orthagonal cells (left, right, up,
  58. * and down). Each cell can be in one of 8 states, numbered from 0 to 7.
  59. * State 0 is the "empty" or "off" state (and is the same as blanks).
  60. * In the following table, the entries are in the following format:
  61. * "cwxyz n".
  62. * Here c is the old state of the current cell, w, x, y, and z are the
  63. * states of the four orthagonal neighbors of the current cell in clockwise
  64. * order, and n is the new state of the current cell. Each rule is used
  65. * for all four starting points of the four orthagonal cells. For example,
  66. * the rule "01725 5" means that a cell in state 0, surrounded by cells
  67. * in states 1, 7, 2, and 5, in that clockwise order, changes to state 5.
  68. * For all rules not given in this table, the cell changes to state 0.
  69. */
  70. static char *rules[] = {
  71. "00001 2",
  72. "00006 3",
  73. "00007 1",
  74. "00011 2",
  75. "00012 2",
  76. "00013 2",
  77. "00021 2",
  78. "00026 2",
  79. "00027 2",
  80. "00052 5",
  81. "00062 2",
  82. "00072 2",
  83. "00102 2",
  84. "00212 5",
  85. "00232 2",
  86. "00522 2",
  87. "01232 1",
  88. "01242 1",
  89. "01252 5",
  90. "01262 1",
  91. "01272 1",
  92. "01275 1",
  93. "01422 1",
  94. "01432 1",
  95. "01442 1",
  96. "01472 1",
  97. "01625 1",
  98. "01722 1",
  99. "01725 5",
  100. "01752 1",
  101. "01762 1",
  102. "01772 1",
  103. "02527 1",
  104. "10001 1",
  105. "10006 1",
  106. "10007 7",
  107. "10011 1",
  108. "10012 1",
  109. "10021 1",
  110. "10024 4",
  111. "10027 7",
  112. "10051 1",
  113. "10101 1",
  114. "10111 1",
  115. "10124 4",
  116. "10127 7",
  117. "10202 6",
  118. "10212 1",
  119. "10221 1",
  120. "10224 4",
  121. "10226 3",
  122. "10227 7",
  123. "10232 7",
  124. "10242 4",
  125. "10262 6",
  126. "10264 4",
  127. "10267 7",
  128. "10272 7",
  129. "10542 7",
  130. "11112 1",
  131. "11122 1",
  132. "11124 4",
  133. "11125 1",
  134. "11126 1",
  135. "11127 7",
  136. "11152 2",
  137. "11212 1",
  138. "11222 1",
  139. "11224 4",
  140. "11225 1",
  141. "11227 7",
  142. "11232 1",
  143. "11242 4",
  144. "11262 1",
  145. "11272 7",
  146. "11322 1",
  147. "12224 4",
  148. "12227 7",
  149. "12243 4",
  150. "12254 7",
  151. "12324 4",
  152. "12327 7",
  153. "12425 5",
  154. "12426 7",
  155. "12527 5",
  156. "20001 2",
  157. "20002 2",
  158. "20004 2",
  159. "20007 1",
  160. "20012 2",
  161. "20015 2",
  162. "20021 2",
  163. "20022 2",
  164. "20023 2",
  165. "20024 2",
  166. "20026 2",
  167. "20027 2",
  168. "20032 6",
  169. "20042 3",
  170. "20051 7",
  171. "20052 2",
  172. "20057 5",
  173. "20072 2",
  174. "20102 2",
  175. "20112 2",
  176. "20122 2",
  177. "20142 2",
  178. "20172 2",
  179. "20202 2",
  180. "20203 2",
  181. "20205 2",
  182. "20207 3",
  183. "20212 2",
  184. "20215 2",
  185. "20221 2",
  186. "20222 2",
  187. "20227 2",
  188. "20232 1",
  189. "20242 2",
  190. "20245 2",
  191. "20255 2",
  192. "20262 2",
  193. "20272 2",
  194. "20312 2",
  195. "20321 6",
  196. "20322 6",
  197. "20342 2",
  198. "20422 2",
  199. "20512 2",
  200. "20521 2",
  201. "20522 2",
  202. "20552 1",
  203. "20572 5",
  204. "20622 2",
  205. "20672 2",
  206. "20712 2",
  207. "20722 2",
  208. "20742 2",
  209. "20772 2",
  210. "21122 2",
  211. "21126 1",
  212. "21222 2",
  213. "21224 2",
  214. "21226 2",
  215. "21227 2",
  216. "21422 2",
  217. "21522 2",
  218. "21622 2",
  219. "21722 2",
  220. "22227 2",
  221. "22244 2",
  222. "22246 2",
  223. "22276 2",
  224. "22277 2",
  225. "30001 3",
  226. "30002 2",
  227. "30004 1",
  228. "30007 6",
  229. "30012 3",
  230. "30042 1",
  231. "30062 2",
  232. "30102 1",
  233. "30251 1",
  234. "40222 1",
  235. "40232 6",
  236. "40322 1",
  237. "50002 2",
  238. "50021 5",
  239. "50022 5",
  240. "50023 2",
  241. "50027 2",
  242. "50202 2",
  243. "50212 2",
  244. "50215 2",
  245. "50224 4",
  246. "50272 2",
  247. "51212 2",
  248. "51242 2",
  249. "51272 2",
  250. "60001 1",
  251. "60002 1",
  252. "61212 5",
  253. "61213 1",
  254. "61222 5",
  255. "70007 7",
  256. "70222 1",
  257. "70225 1",
  258. "70232 1",
  259. "70252 5",
  260. NULL
  261. };
  262.  
  263.  
  264. static char states[STATESIZE];
  265.  
  266.  
  267. /*
  268. * Initialize the states table.
  269. */
  270. static void initstates()
  271. {
  272. char **rpp;
  273. char *rp;
  274. int c0, c1, c2, c3, c4, dest;
  275.  
  276. memset(states, '0', STATESIZE);
  277. for (rpp = rules; *rpp; rpp++)
  278. {
  279. rp = *rpp;
  280. c0 = *rp++ - '0';
  281. c1 = *rp++ - '0';
  282. c2 = *rp++ - '0';
  283. c3 = *rp++ - '0';
  284. c4 = *rp++ - '0';
  285. dest = rp[1];
  286. states[STATEINDEX(c0, c1, c2, c3, c4)] = dest;
  287. states[STATEINDEX(c0, c2, c3, c4, c1)] = dest;
  288. states[STATEINDEX(c0, c3, c4, c1, c2)] = dest;
  289. states[STATEINDEX(c0, c4, c1, c2, c3)] = dest;
  290. }
  291. }
  292.  
  293.  
  294. BOOL APIENTRY DllMain( HANDLE hModule,
  295. DWORD ul_reason_for_call,
  296. LPVOID lpReserved
  297. )
  298. {
  299. return TRUE;
  300. }
  301. //
  302. // Calculate the new state of the 'Me' cell.
  303. int __declspec(dllexport) __stdcall CARule(int Generation,int col,int row,
  304. int NW, int N, int NE,
  305. int W, int Me, int E,
  306. int SW, int S, int SE)
  307. {
  308. return states[STATEINDEX(Me, N, E, S, W)] - '0';
  309. }
  310. //
  311. // Setup the rule.
  312. // The function is called immediatelly after this rule is selected in MCell.
  313. void __declspec(dllexport) __stdcall CASetup(int* RuleType, int* CountOfColors, char* ColorPalette, char* Misc)
  314. {
  315. *RuleType = 2; // 1 - 1D, 2 - 2D
  316. *CountOfColors = 8; // count of states, 0..n-1
  317. strcpy(ColorPalette, "8 colors"); // optional color palette specification
  318. strcpy(Misc, ""); // optional extra parameters; none supported at the moment
  319.  
  320. initstates(); // initialize states array
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement