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 10
- //Temporary: test array for functionality
- char test_array[R][C];
- int copy(char dest[R][C], char src[R][C]);
- int print(char w[R][C]);
- int parse(char w[R][C]);
- int update(char w[R][C]);
- int check(char a);
- char target(char a);
- int adjacent(char w[R][C], char f, int r, int c);
- int write(char w[R][C], char t, int r, int c);
- int test();
- int main(int argc, char **argv) {
- //Local copy of input. To be used in functions
- char wireworld[R][C];
- //Implement test: test() runs when no input is given (just run "./wireworld")
- if (argc != 2) {
- test();
- exit(0);
- }
- //copy [] to wireworld
- copy(wireworld, test_array);
- //sanitize wireworld
- parse(wireworld);
- //Main program: print and update wireworld
- for (int i=0;i<GEN;i++) {
- print(wireworld);
- update(wireworld);
- }
- printf("Operation Completed!\n");
- return 0;
- }
- //////Function
- int parse(char w[R][C]) {
- return 1;
- }
- //////Function
- int print(char w[R][C]) {
- //Print received array
- for (int i=0;i<R;i++) {
- for (int j=0;j<C;j++) {
- printf("%c ", w[i][j]);
- }
- printf("\n");
- }
- //Adds spacing between generations
- printf("\n\n**NEW GENERATION**\n\n");
- return 1;
- }
- //////Function
- int update(char w[R][C]) {
- for (int i=0;i<R;i++) {
- for (int j=0;j<C;j++) {
- //if int
- if (check(w[i][j])) {
- char frm = w[i][j];
- adjacent(w, frm, i, j);
- }
- }
- }
- return 1;
- }
- //////Function
- int check(char a) {
- switch(a) {
- case 'H': return 1;
- case 't': return 1;
- case 'c': return 1;
- default : return 0;
- }
- }
- //////Function
- char target(char a) {
- switch(a) {
- case 'H': return 'c';
- case 't': return 'H';
- case 'c': return 't';
- default : return 0;
- }
- }
- //////Function
- int adjacent(char w[R][C], char f, int r, int c) {
- char t = target(f);
- if (!t) {
- return 0;
- }else {
- for (int i=r-1;i<r+2;r++ && i>0 && i<R) {
- for (int j=c-1; i<c+2; c++ && j>0 && j<C) {
- if (w[i][j] == t) {
- write(w, t, i, j);
- }
- }
- }
- }
- return 1;
- }
- //////Function
- int write(char w[R][C], char t, int r, int c) {
- w[r][c] = t;
- return 1;
- }
- //////Function
- int copy(char dest[R][C], char src[R][C]) {
- for (int i=0;i<R;i++) {
- for(int j=0;j<C;j++) {
- dest[i][j] = src[i][j];
- }
- }
- return 1;
- }
- //////Function
- int test() {
- //copy()
- assert(copy(test_array, test_array));
- //print()
- assert(print(test_array));
- //parse()
- assert(parse(test_array));
- //update()
- //assert(update(test_array);
- //check()
- assert(check('H'));
- assert(!check('n'));
- //target()
- assert(target('H') == 'c');
- assert(!target('n'));
- //adjacent()
- assert(adjacent(test_array,'H', R, C));
- 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