#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h> // for sleep() function
#include <errno.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+2; j++) {
printf("* ");
}
printf("\n");
for (int i = 0; i < rows; i++) {
printf("* ");
for (int j = 0; j < cols; j++) {
if (i == langton_ant.row && j == langton_ant.col) {
printf("\x1b[41m"); 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 ");
}
printf("\x1b[0m"); } else if (i == random_ant.row && j == random_ant.col) {
printf("\x1b[44m"); 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 ");
}
printf("\x1b[0m"); } else if (i == rows/2 && j == cols/2) {
printf("\x1b[42m"); printf(" ");
printf("\x1b[0m"); } else {
printf("%c ", map[i][j]);
}
}
printf("*\n");
}
for (int j = 0; j < cols+2; j++) {
printf("* ");
}
printf("\n");
}
void update_langton_ant(char **map, struct ant *ant) {
char cell_color = map[ant->row][ant->col];
if (cell_color == ' ') { ant->dir = (ant->dir + 1) % 4;
map[ant->row][ant->col] = 'R';
} else if (cell_color == 'R') { ant->dir = (ant->dir + 3) % 4;
map[ant->row][ant->col] = ' ';
} else if (cell_color == 'B') { ant->dir = (ant->dir + 1) % 4;
map[ant->row][ant->col] = ' ';
}
if (ant->dir == UP) {
ant->row--;
} else if (ant->dir == RIGHT) {
ant->col++;
} else if (ant->dir == LEFT) {
ant->col--;
} else if (ant->dir == DOWN) {
ant->row++;
}
if (langton_ant.row < 0 || langton_ant_row >= rows || langton_ant_col < 0 || langton_ant_col >= cols) {
langton_ant.row = (langton_ant.row + rows) % rows;
langton_ant_col = (langton_ant_col + cols) % cols;
}
}
void update_random_ant(char **map, struct ant *ant) {
char cell_color = map[ant->row][ant->col];
if (cell_color == ' ') { map[ant->row][ant->col] = 'B';
} else if (cell_color == 'R' || cell_color == 'B') { map[ant->row][ant->col] = ' ';
}
int rand_dir = rand() % 4;
if (rand_dir == UP) {
ant->row--;
} else if (rand_dir == RIGHT) {
ant->col++;
} else if (rand_dir == LEFT) {
ant->col--;
} else if (rand_dir == DOWN) {
ant->row++;
}
if (random_ant.row < 0 || random_ant.row >= rows || random_ant.col < 0 || random_ant.col >= cols) {
random_ant.row = (random_ant.row + rows) % rows;
random_ant.col = (random_ant.col + cols) % cols;
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Error: Invalid number of arguments\n");
return 1;
}
errno = 0;
char *endptr;
long waiting_time = strtol(argv[1], &endptr, 10);
if (errno != 0 || *endptr != '\0') {
printf("Error: Invalid waiting time\n");
return 1;
}
errno = 0;
long num_steps = strtol(argv[2], &endptr, 10);
if (errno != 0 || *endptr != '\0') {
printf("Error: Invalid number of steps\n");
return 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, ant1_row, ant1_col, ant2_row, ant2_col;
if (fscanf(input_file, "%d %d %d %d %d %d", &rows, &cols, &ant1_row, &ant1_col, &ant2_row, &ant2_col) != 6) {
printf("Error: Invalid input format\n");
fclose(input_file); return 1;
}
fclose(input_file);
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));
for (int step = 0; step < num_steps; step++) {
update_langton_ant(map, &langton_ant)
update_random_ant(map, &random_ant)
print_map(map, rows, cols, langton_ant, random_ant);
usleep(waiting_time * 1000); }
for (int i = 0; i < rows; i++) {
free(map[i]);
}
free(map);
return 0;
}
What I have tried:
issue with this code is that the variables langton_ant and random_ant are not declared anywhere in the code, but are used in both update_langton_ant and update_random_ant. This will cause compilation errors.
Another issue is that the if statement in update_langton_ant that checks for out-of-bounds access refers to variables that are not defined in that function (rows and cols). These variables should be passed as arguments to the function or declared as global variables.
Finally, the code does not use any sort of synchronization mechanism to ensure that the two ants do not occupy the same cell at the same time, which could lead to unpredictable behavior.