Click here to Skip to main content
15,867,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h> // for sleep() function

enum direction { UP, DOWN, LEFT, RIGHT };

struct ant {
    int row;
    int col;
    enum direction dir;
};

void print_map(char **map, int rows, int cols, struct ant langton_ant, struct ant random_ant) {
    system("clear");//clear the console
    // Print the borders of the map
    for (int j = 0; j < cols; j++) {
        printf("* ");
    }
    printf("\n");

    // Print the map contents
    for (int i = 1; i < rows - 1; i++) {
        printf("* ");
        for (int j = 1; j < cols - 1; j++) {
            if (i == langton_ant.row && j == langton_ant.col) {
                if (langton_ant.dir == UP) {
                    printf("^ ");
                } else if (langton_ant.dir == RIGHT) {
                    printf("> ");
                } else if (langton_ant.dir == LEFT) {
                    printf("< ");
                } else if (langton_ant.dir == DOWN) {
                    printf("v ");
                }
            } else if (i == random_ant.row && j == random_ant.col) {
                if (random_ant.dir == UP) {
                    printf("^ ");
                } else if (random_ant.dir == RIGHT) {
                    printf("> ");
                } else if (random_ant.dir == LEFT) {
                    printf("< ");
                } else if (random_ant.dir == DOWN) {
                    printf("v ");
                }
            } else {
                printf("%c ", map[i][j]);
            }
        }
        printf("*\n");
    }

    // Print the borders of the map
    for (int j = 0; j < cols; j++) {
        printf("* ");
    }
    printf("\n");
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Error: Invalid number of arguments\n");
        return 1;
    }

    int waiting_time = atoi(argv[1]); // get waiting time from command line argument
    FILE *input_file = fopen("input.txt", "r");
    if (input_file == NULL) {
        printf("Error: Unable to open input file\n");
        return 1;
    }

    int rows, cols, num_steps, ant1_row, ant1_col, ant2_row, ant2_col;

    if (fscanf(input_file, "%d %d %d %d %d %d %d", &rows, &cols, &num_steps, &ant1_row, &ant1_col, &ant2_row, &ant2_col) != 7) {
        printf("Error: Invalid input format\n");
        return 1;
    }

    char **map = (char**)malloc(rows * sizeof(char*));

    for (int i = 0; i < rows; i++) {
        map[i] = (char*)malloc(cols * sizeof(char));
        for (int j = 0; j < cols; j++) {
            if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
                map[i][j] = '*';
            } else {
                map[i][j] = ' ';
            }
        }
    }

    // Initialize the first ant
    struct ant langton_ant = { ant1_row, ant1_col, UP };

	// Initialize the second ant
    struct ant random_ant = { ant2_row, ant2_col, DOWN };

    srand(time(NULL)); // Seed the random number generator with the current time

    int wait_time = 1; //default wait time between each step is 1 second
    if (argc > 1) {
        wait_time = atoi(argv[1]);
    }

    // Run the simulation
    for (int step = 0; step < num_steps; step++) {
        // Update the direction of the Langton ant based on the color of the cell
    	int langton_ant_row = langton_ant.row;
    	int langton_ant_col = langton_ant.col;

   	 if (map[langton_ant_row][langton_ant_col] == '*') {
        	langton_ant.dir = (langton_ant.dir + 1) % 4;
    	} else {
        	langton_ant.dir = (langton_ant.dir - 1 + 4) % 4;
    	}

   	 // Update the color of the cell and move the Langton ant
   	 if (map[langton_ant_row][langton_ant_col] == '*') {
   	     map[langton_ant_row][langton_ant_col] = ' ';
   	 } else {
  	      map[langton_ant_row][langton_ant_col] = '*';
  	  }
    	if (langton_ant.dir == UP) {
        	langton_ant.row--;
    	} else if (langton_ant.dir == RIGHT) {
    	    langton_ant.col++;
    	} else if (langton_ant.dir == LEFT) {
        	langton_ant.col--;
    	} else if (langton_ant.dir == DOWN) {
        	langton_ant.row++;
    	}

    	// Update the direction of the random ant based on a random number
    	int random_number = rand() % 4;
    	if (random_number == 0) {
     	   random_ant.dir = UP;
    	} else if (random_number == 1) {
        	random_ant.dir = RIGHT;
    	} else if (random_number == 2) {
        	random_ant.dir = LEFT;
    	} else if (random_number == 3) {
    	    random_ant.dir = DOWN;
    	}

    	// Move the random ant
    	if (random_ant.dir == UP) {
        	random_ant.row--;
    	} else if (random_ant.dir == RIGHT) {
       	 	random_ant.col++;
   	} else if (random_ant.dir == LEFT) {
        	random_ant.col--;
    	} else if (random_ant.dir == DOWN) {
        	random_ant.row++;
    	}

    	// Print the current state of the map
    	print_map(map, rows, cols, langton_ant, random_ant);

    	// Wait for some time
	sleep(waiting_time);
	}

	// Free the memory used by the map
	for (int i = 0; i < rows; i++) {
    		free(map[i]);
	}
	free(map);

	return 0;
}


What I have tried:

I want the 'sleep(waiting_time)' function in 'int main()' to work in a way that it makes the ants wait a certain number of seconds - 0.001 being the lowest number of seconds and 10 being the highest number of seconds - before each step/iteration. And I want to implement it so that I run it as a command line argument when I run the code. So an example of how I may look is like this: ./ant 0.1 input.txt. And I want to make it so the 'simulation' shows the ants moving after each step, because as of right now, it only shows the final step.

This was something I tried to implement:

void newSleep(float sec)
{
	struct timespec ts;
	ts.tv_sec = (int) sec;
	ts.tv_nsec = (sec - ((int) sec)) * 1000000000;
	nanosleep(&ts,NULL);
}


But I didn't figure out how to incorporate it into the overall code.
Posted
Updated 15-Apr-23 2:49am

1 solution

You should capture all arguments at the beginning of your main method. As it is you are capturing argv[1] at the beginning as waiting_time, and then later on as wait_time. That is bound to cause confusion. Also, you use the atoi function to convert the string into an integer, so that will not work for a value of "0.1"; you need to use atof to capture a float value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900