Example #1
0
int forest_fire(int x, int y, double density)
{
    Forest *forest;
    printf(COLOR_YELLOW"\nForest Fire Simulation will be executed. "
        COLOR_RESET "Details:\n");
    printf("Gridsize: %d by %d\n", x, y);
    printf("Vegetation density: %g\n", density);
    forest = init_grid(x, y);
    fill_grid(forest, density);
    //print_grid(forest);
    //printf("\n");
    while(forest_fire_sim(forest))
    {
        //print_grid(forest);
        //printf("\n");
        //sleep(1);
    }
    if(forest->crossed > 0)
    {
        printf(COLOR_GREEN "Opposite side of the forest reached in %d "
            "steps.\n", forest->crossed);
        printf(COLOR_RESET);
        return forest->crossed;
    }
    printf(COLOR_RED "Couldn't reach the other side of the forest. "
        "Burning stopped after %d steps.\n", abs(forest->crossed));
    printf(COLOR_RESET);
    cleanup_grid(forest);
    free(forest);
    return 0;
}
Example #2
0
int
main(int argc, char *argv[])
{
    char input[MAX_FILENAME_SIZE];
    int c;
    int iterations = 1;
    int generate = 0;
    struct Grid *grid;
    struct Grid *tmp_grid;

    if (argc < 2) {
        help();
        return 0;
    }

    while ((c = getopt(argc, argv, "?hv:n:i:g")) != -1) {
        switch (c) {
            case 'h':
            case 'v':
                help();
                return 0;
            case 'n':
                iterations = atoi((char *) getopt);
                break;
            case 'i':
                strncpy(input, optarg, MAX_FILENAME_SIZE);
                break;
            case 'g':
                generate = 1;
                break;
            default:
                help();
                return 0;
        }
    } 

    if (argc != optind) {
        help();
        return 0;
    }

    grid = allocate_grid();
    tmp_grid = allocate_grid();
    if (!grid || !tmp_grid) {
        printf("error: out of memory\n");
        return -1;
    }
    if (generate) {
        generate_grid(grid);
    } else {
        read_grid(input, grid); 
    }
    for (c = 0; c < iterations; c++) {
        mean_filter(grid, tmp_grid);
        struct Grid *tmp = grid;
        grid = tmp_grid;
        tmp_grid = tmp;
    }
    write_grid(grid);
    cleanup_grid(grid);
    cleanup_grid(tmp_grid);

    return 0;
}