diff --git a/src/main.c b/src/main.c index 7503f33..5aeed88 100644 --- a/src/main.c +++ b/src/main.c @@ -12,7 +12,7 @@ #endif // import sub programme -#include "test_prog.h" +#include "pipes_test.h" /* Photonics helper main programm @@ -255,6 +255,7 @@ int main(int argc, char **argv){ case 'G': line = 4; break; case '?': print_help(); break; + case 'p': pipes(); break; case 'q': run = false; break; case '\n': printf(" Es wurde die Enter Taste gedrückt"); getchar(); break; diff --git a/src/pipes_test.c b/src/pipes_test.c new file mode 100644 index 0000000..8d31022 --- /dev/null +++ b/src/pipes_test.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include + +#define DEBUGGER_PIPES_TO_NUMBERS 0 +#define DEBUGGER_RANDOM 0 + +#define INFILL_PLAIN_PAPER 0 +#define INFILL_X_PIPE 1 +#define INFILL_Y_PIPE 2 +#define FILLER "░" // ░ + +#define GRID_GROESSE_X 200 // x:y -> 2:1 for a square looking grid +#define GRID_GROESSE_Y 60 +#define MIN_LAENGE_PIPE 6 +#define MAX_LAENGE_PIPE 6 +#define SLEEP_TIMER 10000 // in nano seconds +#define COLOR_CHANGING_PROBABILITY 10 // format: "(1 : your_number)", for every direction change + +#define RESET_COLOR "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" + +const char* color; +const char *colors[] = {RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE}; + +void random_color() { + color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))]; +} + +void print_grid(int x, int y, char **grid) { + for (int i = 0; i < y; i++) { + printf("\n"); + for (int j = 0; j < x; j++) { + /* + switch (grid[i][j]) { + case 0: printf(" "); break; + case 1: printf("━"); break; + case 2: printf("┃"); break; + } + */ + if (!DEBUGGER_PIPES_TO_NUMBERS){ + if(grid[i][j] == 0){ + printf(FILLER); + } else if(grid[i][j] != 0 && grid[i][j] != 1 && grid[i][j] != 2){ + return; + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 0 && grid[i][j - 1] != 0 && grid[i][j + 1] != 0 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] != 0 && grid[i + 1][j] != 0 && grid[i][j - 1] == 0 && grid[i][j + 1] == 0 && grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s╋", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 2 && grid[i][j - 1] == 1 && grid[i][j + 1] == 0 && grid[i][j] != 0 ){ + printf("%s┓", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 0 && grid[i][j - 1] == 0 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s┗", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 2 && grid[i][j - 1] == 0 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s┏", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 0 && grid[i][j - 1] == 1 && grid[i][j + 1] == 0 && grid[i][j] != 0 ){ + printf("%s┛", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 0 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s┣", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 1 && grid[i][j + 1] == 0 && grid[i][j] != 0 ){ + printf("%s┫", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 2 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s┳", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 0 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] != 0 ){ + printf("%s┻", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 0 && grid[i][j - 1] != 0 && grid[i][j + 1] == 0 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 0 && grid[i][j - 1] == 0 && grid[i][j + 1] != 0 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] != 0 && grid[i + 1][j] == 0 && grid[i][j - 1] == 0 && grid[i][j + 1] == 0 && grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] != 0 && grid[i][j - 1] == 0 && grid[i][j + 1] == 0 && grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] == 1 && grid[i + 1][j] == 0 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] == 1 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] == 1 && grid[i + 1][j] == 1 && grid[i][j - 1] == 1 && grid[i][j + 1] == 1 && grid[i][j] == 1 ){ + printf("%s━", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 0 && grid[i][j + 1] == 2 && grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 2 && grid[i][j + 1] == 0 && grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] == 2 && grid[i + 1][j] == 2 && grid[i][j - 1] == 2 && grid[i][j + 1] == 2 && grid[i][j] == 2 ){ + printf("%s┃", color); + + + + } else if(grid[i - 1][j] != 0 && grid[i + 1][j] != 0 && grid[i][j - 1] != 0 && grid[i][j + 1] != 0 && grid[i][j] == 0 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] != 0 && grid[i + 1][j] != 0 && grid[i][j - 1] != 0 && grid[i][j + 1] == 0 && grid[i][j] != 0 ){ + printf("%s┃", color); + + } else if(grid[i - 1][j] != 0 && grid[i + 1][j] == 0 && grid[i][j - 1] != 0 && grid[i][j + 1] != 0 && grid[i][j] != 0 ){ + printf("%s┻", color); + + } else if(grid[i - 1][j] == 0 && grid[i + 1][j] != 0 && grid[i][j - 1] != 0 && grid[i][j + 1] != 0 && grid[i][j] != 0 ){ + printf("%s┳", color); + + } else if(grid[i - 1][j + 1] == 0 && grid[i - 1][j] != 0 && grid[i + 1][j] != 0 && grid[i][j - 1] != 0 && grid[i][j + 1] != 0 && grid[i][j] != 0 ){ + printf("%s┗", color); + + } else if(grid[i][j] == 2 ){ + printf("%s┃", color); + + } else if(grid[i][j] == 1 ){ + printf("%s━", color); + } else{printf("x");} + printf(RESET_COLOR); // Setzt die Farbe zurück + }else{ + printf("%i", grid[i][j]); + } + } + } +} + +void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int laenge) { + if(laenge < 0){ + for (int i = *x_start; i > *x_start + laenge; i--) { + grid[*y_start][i] = INFILL_X_PIPE; + printf("\33[H\033[J"); + print_grid(x, y, grid); + usleep(SLEEP_TIMER); + } + } else{ + for (int i = *x_start; i < *x_start + laenge; i++) { + grid[*y_start][i] = INFILL_X_PIPE; + printf("\33[H\033[J"); + print_grid(x, y, grid); + usleep(SLEEP_TIMER); + } + } + + *x_start += laenge; +} + +void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int laenge) { + if(laenge < 0){ + for (int i = *y_start; i > *y_start + laenge/2; i--) { + grid[i][*x_start] = INFILL_Y_PIPE; + printf("\33[H\033[J"); + print_grid(x, y, grid); + usleep(SLEEP_TIMER); + } + } else{ + for (int i = *y_start; i < *y_start + laenge/2; i++) { + grid[i][*x_start] = INFILL_Y_PIPE; + printf("\33[H\033[J"); + print_grid(x, y, grid); + usleep(SLEEP_TIMER); + } + } + /* + for (int i = *y_start; i < *y_start + laenge/2; i++) { + grid[i][*x_start] = 2; + printf("\33[H\033[J"); + //fflush(stdout); + //usleep(100000); + print_grid(x, y, grid); + sleep_seconds(SLEEP_TIMER); + }*/ + + *y_start += laenge/2; +} + +int main_program() { + + int x_start = GRID_GROESSE_X / 2; + int y_start = GRID_GROESSE_Y / 2; + int laenge = 20; + int max_l = MAX_LAENGE_PIPE + 1 - MIN_LAENGE_PIPE; + int min_l = MIN_LAENGE_PIPE - 1; + int color_c_p = COLOR_CHANGING_PROBABILITY + 1; + srand(time(NULL)); + + random_color(); + + char **grid = malloc(GRID_GROESSE_Y * sizeof(char *)); + for (int i = 0; i < GRID_GROESSE_Y; i++) { + grid[i] = malloc(GRID_GROESSE_X * sizeof(char)); + } + + for (int i = 0; i < GRID_GROESSE_Y; i++) { + for (int j = 0; j < GRID_GROESSE_X; j++) { + grid[i][j] = INFILL_PLAIN_PAPER; + } + } + + int richtungswechsler = 1; + + while (!DEBUGGER_RANDOM) { + + if ((rand() %color_c_p) <= 1) { + random_color(); + } + + if ((rand() %2)-1) { + laenge = ((rand() %max_l)+min_l); + } else { + laenge = ((rand() %max_l)+min_l)*-1; + } + if (richtungswechsler == 1) { + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + richtungswechsler *= -1; + } else { + generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + richtungswechsler *= -1; + } + + } + + if (DEBUGGER_RANDOM) { + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -0.1); + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1.05); + generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * 0.1); + generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * 0.9); + generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); + } + + + + for (int i = 0; i < GRID_GROESSE_Y; i++) { + free(grid[i]); + } + free(grid); + + return 0; +} + +int pipes() { + pid_t pid; + int status; + + while (1) { + pid = fork(); // Erzeuge einen neuen Prozess + if (pid == 0) { + // Kindprozess führt die Hauptlogik aus + main_program(); + exit(0); // Normales Beenden, falls kein Fehler + } else if (pid > 0) { + // Elternprozess wartet auf das Kind + wait(&status); + if (WIFEXITED(status)) { + break; // Beende die Schleife, falls alles ok ist + } else if (WIFSIGNALED(status)) { + } + } else { + perror("Fehler bei fork()"); + exit(1); + } + } + + return 0; +} + diff --git a/src/pipes_test.h b/src/pipes_test.h new file mode 100644 index 0000000..9d60512 --- /dev/null +++ b/src/pipes_test.h @@ -0,0 +1,7 @@ + + + + +int pipes(); + +