Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- /*Set limit on Rows and Column*/
- #define R 40
- #define C 40
- /*Set number of generations*/
- #define GEN 1000
- int create(char a[R][C], FILE *b);
- int print(char w[R][C]);
- int parse(char w[R][C]);
- int parse_InvChar(char a[R][C]);
- int parse_SplitElectron(char a[R][C]);
- int update(char w[R][C]);
- int copy(char a[R][C], char b[R][C]);
- int check(char a);
- char rule(char a);
- int adjacent(char a[R][C], char b, int x, int y);
- int write(char w[R][C], char t, int r, int c);
- int test();
- /*Main*/
- int main(int argc, char **argv) {
- /*Local copy of input. To be used in functions*/
- char wireworld[R][C];
- FILE *f;
- int i, j, k;
- /*Run test() when no input is given (just run "./wireworld")*/
- if (argc != 2) {
- test();
- exit(EXIT_SUCCESS);
- }
- /*Fill wireworld with blank spaces*/
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- wireworld[i][j] = ' ';
- }
- }
- /*Write strings from file to wireworld*/
- f = fopen(argv[1], "r");
- if (f == NULL) {
- printf("I can't find that file...Check it again.\n");
- exit(EXIT_FAILURE);
- }
- create(wireworld, f);
- fclose(f);
- /*Sanitize wireworld*/
- if (!parse(wireworld)) {
- printf("Operation aborted\n");
- exit(EXIT_FAILURE);
- };
- /*Main program: print and update wireworld*/
- for (k=0;k<GEN;k++) {
- print(wireworld);
- update(wireworld);
- }
- printf("Operation Completed!\n");
- exit(EXIT_SUCCESS);
- }
- /*Create an array from file*/
- int create(char a[R][C], FILE *b) {
- int i;
- for (i=0;i<R;i++) {
- fgets(a[i], 255, b);
- }
- return 0;
- }
- /*Check input*/
- int parse(char a[R][C]) {
- int error = 0;
- /*1. Check for invalid Characters*/
- if(!parse_InvChar(a)) {
- printf("Log: Invalid character detected\n");
- error++;
- }
- /*2. Checks for disconnected electrons (H separated from t)*/
- if (!parse_SplitElectron(a)) {
- printf("Log: Separated electrons (Invalid configuration)\n");
- error++;
- }
- if (error) {
- return 0;
- }
- return 1;
- }
- /*Parse for invalid characters*/
- int parse_InvChar(char a[R][C]) {
- int i,j;
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- if (!check(a[i][j]) && a[i][j] != ' ') {
- return 0;
- }
- }
- }
- return 1;
- }
- /*Parse for separated 'H' and 't'*/
- int parse_SplitElectron(char a[R][C]) {
- int i,j;
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- if (check(a[i][j]) && ((a[i][j] == 'H') || (a[i][j] == 't'))) {
- char b;
- switch(a[i][j]) {
- case 'H': b = 't'; break;
- case 't': b = 'H'; break;
- default : b = 0; break;
- }
- if(!adjacent(a, b, i, j)) {
- return 0;
- }
- }
- }
- }
- return 1;
- }
- /*Print wireworld*/
- int print(char a[R][C]) {
- int i,j;
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- printf("%c", a[i][j]);
- }
- printf ("\n");
- }
- return 1;
- }
- /*Update wireworld*/
- int update(char a[R][C]) {
- int i, j;
- /*Temp array to store changes*/
- char tmp[R][C];
- /*Copy wireworld to temp array*/
- copy(a, tmp);
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- /*Is character part of operation?*/
- if (check(a[i][j])) {
- /*What character does a[i][j] interact with?*/
- char c = rule(a[i][j]);
- if (c) {
- /*Check adjacent characters for a match*/
- if (adjacent(a, c, i, j)) {
- write(tmp, c, i, j);
- }
- }
- }
- }
- }
- /*Copy temp array to wireworld*/
- copy(tmp, a);
- return 1;
- }
- /*Copy to new array*/
- int copy(char a[R][C], char b[R][C]) {
- int i,j;
- for (i=0;i<R;i++) {
- for (j=0;j<C;j++) {
- b[i][j] = a[i][j];
- }
- }
- return 1;
- }
- /*Checks character*/
- int check(char a) {
- switch(a) {
- case 'H': return 1;
- case 't': return 1;
- case 'c': return 1;
- default : return 0;
- }
- }
- /*Returns character in wireworld Rules*/
- char rule(char a) {
- switch(a) {
- case 't': return 'c';
- case 'H': return 't';
- case 'c': return 'H';
- default : return 0;
- }
- }
- int adjacent(char a[R][C], char b, int x, int y) {
- int i,j;
- int num = 0;
- for (i=x-1;i<=x+1;i++) {
- for (j=y-1;(j<=y+1) && (j<C);j++) {
- /*Detect borders*/
- if ((j>-1 && j<R) && (i>-1 && i<R)) {
- if (a[i][j] == b) {
- num++;
- }
- }
- }
- }
- /*copper -> Electron Head rule: less than 2*/
- /*For clarification: copper checks for 'H'*/
- if (num) {
- if (b != 'H') {
- return 1;
- }else {
- if (num <= 2) {
- return 1;
- }
- }
- }
- return 0;
- }
- /*Writes character to world*/
- int write(char a[R][C], char b, int r, int c) {
- a[r][c] = b;
- return 1;
- }
- /*Test function*/
- int test() {
- char test_array[R][C];
- /*print - Disabled by default as test_array prints gibberish.*/
- /*assert(print(test_array));*/
- /*parse - Disabled by default as test_array will print error logs*/
- /*assert(!parse(test_array));*/
- /*parse_InvChar*/
- assert(!parse_InvChar(test_array));
- /*parse_SplitElectron*/
- assert(!parse_SplitElectron(test_array));
- /*update*/
- /*assert(update(test_array);*/
- /*copy*/
- assert(copy(test_array,test_array));
- /*check*/
- assert(check('H'));
- assert(!check('n'));
- /*rule*/
- assert(rule('H') == 't');
- assert(!rule('n'));
- /*adjacent*/
- assert(!adjacent(test_array, 'n', R, C));
- /*write*/
- assert(write(test_array, 't', R, C));
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement