#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 = atof(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(wait_time);
}
for (int i = 0; i < rows; i++) {
free(map[i]);
}
free(map);
return 0;
}
What I have tried:
Right now when I run the code, I run it along with the wait time as a command line argument. The dimensions of the map, position of the ants and the number of steps the ants take are all in the file input. I want to make the number of steps - num_steps - work as a command line argument. When I tried to change the code to have the 'num_steps' work as a command line argument the terminal kept displaying 'invalid number of arguments' even though I gave it enough arguments to take in.
Also, how would I modify the code to have it read a file in the following format:
rows cols
ant1_row ant1_col "v"
ant2_row ant2_col ">"
Each description should be a new line in the txt file. The current file has everything in a single line. Also the "v" and ">" are the direction the ants start in, it can any of; "<" ">" "^" "v"