Compare commits

..

No commits in common. "916ad84e18df6fcb38245dc57771fcb77a8f122b" and "273de59faa059471d91d42f926ec637c96f63235" have entirely different histories.

2 changed files with 30 additions and 73 deletions

View File

@ -4,9 +4,6 @@
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/ioctl.h>
#include <termio.h>
#define DEBUGGER_PIPES_TO_NUMBERS 0 #define DEBUGGER_PIPES_TO_NUMBERS 0
#define DEBUGGER_RANDOM 0 #define DEBUGGER_RANDOM 0
@ -15,12 +12,12 @@
#define INFILL_Y_PIPE 2 #define INFILL_Y_PIPE 2
#define FILLER "░" // ░ #define FILLER "░" // ░
// #define GRID_GROESSE_X 200 // x:y -> 2:1 for a square looking grid #define GRID_GROESSE_X 200 // x:y -> 2:1 for a square looking grid
// #define GRID_GROESSE_Y 60 #define GRID_GROESSE_Y 60
#define MIN_LAENGE_PIPE 6 #define MIN_LAENGE_PIPE 6
#define MAX_LAENGE_PIPE 6 #define MAX_LAENGE_PIPE 6
#define SLEEP_TIMER 5000 // in nano seconds #define SLEEP_TIMER 10000 // in nano seconds
#define COLOR_CHANGING_PROBABILITY 5 // format: "(1 : your_number)", for every direction change #define COLOR_CHANGING_PROBABILITY 10 // format: "(1 : your_number)", for every direction change
#define RESET_COLOR "\033[0m" #define RESET_COLOR "\033[0m"
#define RED "\033[31m" #define RED "\033[31m"
@ -38,23 +35,6 @@ void random_color() {
color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))]; color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))];
} }
int getch_directions(){
int ch;
struct termios oldattr, newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~ICANON;
newattr.c_lflag &= ~ECHO;
newattr.c_cc[VMIN] = 1;
newattr.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
return ch;
}
void print_grid(int x, int y, char **grid) { void print_grid(int x, int y, char **grid) {
for (int i = 0; i < y; i++) { for (int i = 0; i < y; i++) {
printf("\n"); printf("\n");
@ -189,9 +169,6 @@ void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int
void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int laenge) { void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int laenge) {
if(laenge < 0){ if(laenge < 0){
for (int i = *y_start; i > *y_start + laenge/2; i--) { for (int i = *y_start; i > *y_start + laenge/2; i--) {
if (i <= 0) {
return;
}
grid[i][*x_start] = INFILL_Y_PIPE; grid[i][*x_start] = INFILL_Y_PIPE;
printf("\33[H\033[J"); printf("\33[H\033[J");
print_grid(x, y, grid); print_grid(x, y, grid);
@ -199,9 +176,6 @@ void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int
} }
} else{ } else{
for (int i = *y_start; i < *y_start + laenge/2; i++) { for (int i = *y_start; i < *y_start + laenge/2; i++) {
if (i <= 0) {
return;
}
grid[i][*x_start] = INFILL_Y_PIPE; grid[i][*x_start] = INFILL_Y_PIPE;
printf("\33[H\033[J"); printf("\33[H\033[J");
print_grid(x, y, grid); print_grid(x, y, grid);
@ -221,7 +195,7 @@ void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int
*y_start += laenge/2; *y_start += laenge/2;
} }
int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) { int main_program() {
int x_start = GRID_GROESSE_X / 2; int x_start = GRID_GROESSE_X / 2;
int y_start = GRID_GROESSE_Y / 2; int y_start = GRID_GROESSE_Y / 2;
@ -244,44 +218,26 @@ int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
} }
} }
//int richtungswechsler = 1; int richtungswechsler = 1;
while (!DEBUGGER_RANDOM) { while (!DEBUGGER_RANDOM) {
if ((rand() %color_c_p) <= 1) { if ((rand() %color_c_p) <= 1) {
random_color(); random_color();
} }
laenge = ((rand() %max_l)+min_l); if ((rand() %2)-1) {
laenge = ((rand() %max_l)+min_l);
//if ((rand() %2)-1) { } else {
// laenge = ((rand() %max_l)+min_l); laenge = ((rand() %max_l)+min_l)*-1;
//} 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;
char key_input = (char)getch_directions(); } else {
generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge);
switch (key_input) { richtungswechsler *= -1;
case 'h': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
case 'a': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
case 'l': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
case 'd': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
case 'j': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
case 's': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
case 'k': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
case 'w': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
default: return 0;
} }
// 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;
// }
} }
@ -300,27 +256,22 @@ int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
for (int i = 0; i < GRID_GROESSE_Y; i++) { for (int i = 0; i < GRID_GROESSE_Y; i++) {
free(grid[i]); free(grid[i]);
} }
free(grid); free(grid);
return 0; return 0;
} }
int pipes() {
int pipes(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
//int main() {
//int GRID_GROESSE_X = 100;
//int GRID_GROESSE_Y = 50;
pid_t pid; pid_t pid;
int status; int status;
while (1) { while (1) {
pid = fork(); // Erzeuge einen neuen Prozess pid = fork(); // Erzeuge einen neuen Prozess
if (pid == 0) { if (pid == 0) {
// Kindprozess führt die Hauptlogik aus // Kindprozess führt die Hauptlogik aus
main_program(GRID_GROESSE_X, GRID_GROESSE_Y); main_program();
exit(0); // Normales Beenden, falls kein Fehler exit(0); // Normales Beenden, falls kein Fehler
} else if (pid > 0) { } else if (pid > 0) {
// Elternprozess wartet auf das Kind // Elternprozess wartet auf das Kind

View File

@ -1 +1,7 @@
int pipes(int GRIG_GROESSE_X, int GRID_GROESSE_Y);
int pipes();