Untitled
unknown
plain_text
2 months ago
2.3 kB
3
Indexable
#include <stdio.h> #include <string.h> #include "snake_utils.h" #include "game.h" int main(int argc, char *argv[]) { bool io_stdin = false; char *in_filename = NULL; char *out_filename = NULL; game_t *game = NULL; // Parse arguments for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-i") == 0 && i < argc - 1) { if (io_stdin) { fprintf(stderr, "Usage: %s [-i filename | --stdin] [-o filename]\n", argv[0]); return 1; } in_filename = argv[i + 1]; i++; continue; } else if (strcmp(argv[i], "--stdin") == 0) { if (in_filename != NULL) { fprintf(stderr, "Usage: %s [-i filename | --stdin] [-o filename]\n", argv[0]); return 1; } io_stdin = true; continue; } else if (strcmp(argv[i], "-o") == 0 && i < argc - 1) { out_filename = argv[i + 1]; i++; continue; } else { fprintf(stderr, "Usage: %s [-i filename | --stdin] [-o filename]\n", argv[0]); return 1; } } // Do not modify anything above this line. /* Task 7 */ // Read board from file, or create default board if (in_filename != NULL) { FILE *file = fopen(in_filename, "r"); if (!file) { fprintf(stderr, "File not found or unable to open the file.\n"); return -1; } game = load_board(file); fclose(file); } else if (io_stdin) { game = load_board(stdin); } else { game = create_default_game(); } // Handle game loading failure if (!game) { fprintf(stderr, "Failed to load the game.\n"); return -1; } initialize_snakes(game); update_game(game, deterministic_food); if (out_filename) { FILE *out_file = fopen(out_filename, "w"); if (!out_file) { fprintf(stderr, "Unable to open output file.\n"); free_game(game); return -1; } save_board(game, out_file); fclose(out_file); } else { print_board_to_stdout(game); // Ensure this function is defined in your game.h or another relevant file } free_game(game); return 0; }
Editor is loading...
Leave a Comment