#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
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");
for (int j = 0; j < cols; j++) {
printf("* ");
}
printf("\n");
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");
}
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]);
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] = ' ';
}
}
}
struct ant langton_ant = { ant1_row, ant1_col, UP };
struct ant random_ant = { ant2_row, ant2_col, DOWN };
srand(time(NULL));
int wait_time = 1;
if (argc > 1) {
wait_time = atoi(argv[1]);
}
for (int step = 0; step < num_steps; step++) {
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;
}
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++;
}
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;
}
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_map(map, rows, cols, langton_ant, random_ant);
sleep(waiting_time);
}
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.