Advertisement
ChickyMasala

Untitled

Oct 24th, 2019
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. /*Set limit on Rows and Column*/
  6. #define R 40
  7. #define C 40
  8.  
  9. /*Set number of generations*/
  10. #define GEN 1000
  11.  
  12. int create(char a[R][C], FILE *b);
  13. int print(char w[R][C]);
  14. int parse(char w[R][C]);
  15. int parse_InvChar(char a[R][C]);
  16. int parse_SplitElectron(char a[R][C]);
  17. int update(char w[R][C]);
  18. int copy(char a[R][C], char b[R][C]);
  19. int check(char a);
  20. char rule(char a);
  21. int adjacent(char a[R][C], char b, int x, int y);
  22. int write(char w[R][C], char t, int r, int c);
  23. int test();
  24.  
  25. /*Main*/
  26. int main(int argc, char **argv) {
  27. /*Local copy of input. To be used in functions*/
  28. char wireworld[R][C];
  29. FILE *f;
  30. int i, j, k;
  31.  
  32. /*Run test() when no input is given (just run "./wireworld")*/
  33. if (argc != 2) {
  34. test();
  35. exit(EXIT_SUCCESS);
  36. }
  37.  
  38. /*Fill wireworld with blank spaces*/
  39. for (i=0;i<R;i++) {
  40. for (j=0;j<C;j++) {
  41. wireworld[i][j] = ' ';
  42. }
  43. }
  44.  
  45. /*Write strings from file to wireworld*/
  46. f = fopen(argv[1], "r");
  47. if (f == NULL) {
  48. printf("I can't find that file...Check it again.\n");
  49. exit(EXIT_FAILURE);
  50. }
  51. create(wireworld, f);
  52. fclose(f);
  53.  
  54. /*Sanitize wireworld*/
  55. if (!parse(wireworld)) {
  56. printf("Operation aborted\n");
  57. exit(EXIT_FAILURE);
  58. };
  59.  
  60. /*Main program: print and update wireworld*/
  61. for (k=0;k<GEN;k++) {
  62. print(wireworld);
  63. update(wireworld);
  64. }
  65.  
  66. printf("Operation Completed!\n");
  67. exit(EXIT_SUCCESS);
  68. }
  69.  
  70. /*Create an array from file*/
  71. int create(char a[R][C], FILE *b) {
  72. int i;
  73.  
  74. for (i=0;i<R;i++) {
  75. fgets(a[i], 255, b);
  76. }
  77. return 0;
  78. }
  79.  
  80. /*Check input*/
  81. int parse(char a[R][C]) {
  82. int error = 0;
  83.  
  84. /*1. Check for invalid Characters*/
  85. if(!parse_InvChar(a)) {
  86. printf("Log: Invalid character detected\n");
  87. error++;
  88. }
  89.  
  90. /*2. Checks for disconnected electrons (H separated from t)*/
  91. if (!parse_SplitElectron(a)) {
  92. printf("Log: Separated electrons (Invalid configuration)\n");
  93. error++;
  94. }
  95.  
  96. if (error) {
  97. return 0;
  98. }
  99. return 1;
  100. }
  101.  
  102. /*Parse for invalid characters*/
  103. int parse_InvChar(char a[R][C]) {
  104. int i,j;
  105.  
  106. for (i=0;i<R;i++) {
  107. for (j=0;j<C;j++) {
  108. if (!check(a[i][j]) && a[i][j] != ' ') {
  109. return 0;
  110. }
  111. }
  112. }
  113. return 1;
  114. }
  115.  
  116. /*Parse for separated 'H' and 't'*/
  117. int parse_SplitElectron(char a[R][C]) {
  118. int i,j;
  119.  
  120. for (i=0;i<R;i++) {
  121. for (j=0;j<C;j++) {
  122. if (check(a[i][j]) && ((a[i][j] == 'H') || (a[i][j] == 't'))) {
  123. char b;
  124. switch(a[i][j]) {
  125. case 'H': b = 't'; break;
  126. case 't': b = 'H'; break;
  127. default : b = 0; break;
  128. }
  129. if(!adjacent(a, b, i, j)) {
  130. return 0;
  131. }
  132. }
  133. }
  134. }
  135. return 1;
  136. }
  137.  
  138. /*Print wireworld*/
  139. int print(char a[R][C]) {
  140. int i,j;
  141.  
  142. for (i=0;i<R;i++) {
  143. for (j=0;j<C;j++) {
  144. printf("%c", a[i][j]);
  145. }
  146. printf ("\n");
  147. }
  148. return 1;
  149. }
  150.  
  151. /*Update wireworld*/
  152. int update(char a[R][C]) {
  153. int i, j;
  154. /*Temp array to store changes*/
  155. char tmp[R][C];
  156. /*Copy wireworld to temp array*/
  157. copy(a, tmp);
  158.  
  159. for (i=0;i<R;i++) {
  160. for (j=0;j<C;j++) {
  161. /*Is character part of operation?*/
  162. if (check(a[i][j])) {
  163. /*What character does a[i][j] interact with?*/
  164. char c = rule(a[i][j]);
  165. if (c) {
  166. /*Check adjacent characters for a match*/
  167. if (adjacent(a, c, i, j)) {
  168. write(tmp, c, i, j);
  169. }
  170. }
  171. }
  172. }
  173. }
  174.  
  175. /*Copy temp array to wireworld*/
  176. copy(tmp, a);
  177. return 1;
  178. }
  179.  
  180. /*Copy to new array*/
  181. int copy(char a[R][C], char b[R][C]) {
  182. int i,j;
  183.  
  184. for (i=0;i<R;i++) {
  185. for (j=0;j<C;j++) {
  186. b[i][j] = a[i][j];
  187. }
  188. }
  189. return 1;
  190. }
  191.  
  192. /*Checks character*/
  193. int check(char a) {
  194. switch(a) {
  195. case 'H': return 1;
  196. case 't': return 1;
  197. case 'c': return 1;
  198. default : return 0;
  199. }
  200. }
  201.  
  202. /*Returns character in wireworld Rules*/
  203. char rule(char a) {
  204. switch(a) {
  205. case 't': return 'c';
  206. case 'H': return 't';
  207. case 'c': return 'H';
  208. default : return 0;
  209. }
  210. }
  211.  
  212. int adjacent(char a[R][C], char b, int x, int y) {
  213. int i,j;
  214. int num = 0;
  215.  
  216. for (i=x-1;i<=x+1;i++) {
  217. for (j=y-1;(j<=y+1) && (j<C);j++) {
  218. /*Detect borders*/
  219. if ((j>-1 && j<R) && (i>-1 && i<R)) {
  220. if (a[i][j] == b) {
  221. num++;
  222. }
  223. }
  224. }
  225. }
  226.  
  227. /*copper -> Electron Head rule: less than 2*/
  228. /*For clarification: copper checks for 'H'*/
  229. if (num) {
  230. if (b != 'H') {
  231. return 1;
  232. }else {
  233. if (num <= 2) {
  234. return 1;
  235. }
  236. }
  237. }
  238. return 0;
  239. }
  240.  
  241. /*Writes character to world*/
  242. int write(char a[R][C], char b, int r, int c) {
  243. a[r][c] = b;
  244. return 1;
  245. }
  246.  
  247. /*Test function*/
  248. int test() {
  249. char test_array[R][C];
  250. /*print - Disabled by default as test_array prints gibberish.*/
  251. /*assert(print(test_array));*/
  252. /*parse - Disabled by default as test_array will print error logs*/
  253. /*assert(!parse(test_array));*/
  254. /*parse_InvChar*/
  255. assert(!parse_InvChar(test_array));
  256. /*parse_SplitElectron*/
  257. assert(!parse_SplitElectron(test_array));
  258. /*update*/
  259. /*assert(update(test_array);*/
  260. /*copy*/
  261. assert(copy(test_array,test_array));
  262. /*check*/
  263. assert(check('H'));
  264. assert(!check('n'));
  265. /*rule*/
  266. assert(rule('H') == 't');
  267. assert(!rule('n'));
  268. /*adjacent*/
  269. assert(!adjacent(test_array, 'n', R, C));
  270. /*write*/
  271. assert(write(test_array, 't', R, C));
  272. return 1;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement