【解決方法】コマンドラインとファイル入力を正しく機能させる方法は?


#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 = atof(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(wait_time);
	}

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

	return 0;
}

私が試したこと:

今、コードを実行するときは、コマンド ライン引数として待機時間を指定して実行します。 マップの寸法、アリの位置、アリの歩数はすべて入力ファイルに含まれています。 ステップ数 – num_steps – をコマンドライン引数として機能させたい。 「num_steps」がコマンドライン引数として機能するようにコードを変更しようとすると、端末に十分な引数を与えたにもかかわらず、端末に「引数の数が無効です」と表示され続けました。

また、次の形式のファイルを読み取るようにコードを変更するにはどうすればよいですか。

行の列
ant1_row ant1_col “v”
ant2_row ant2_col “>”

各説明は、txt ファイル内の新しい行にする必要があります。 現在のファイルにはすべてが 1 行に含まれています。 また、「v」と「>」は、アリが開始する方向です。 “<" ">” “^” “v”

解決策 1

デバッガーを使用して、取得している値を正確に確認することから始めます。 argcargv – 引数の数とその内容がわかれば、問題が何であるかは明らかです。

私たちはあなたのためにそれをすることはできません: 私たちはあなたが入力として何を提供しているのか分かりません!

コメント

タイトルとURLをコピーしました