unnötiges zeugs löschen

pipes2game
Noah69420 2024-12-13 18:07:35 +01:00
parent 68d4d99b59
commit 091e66c85d
1 changed files with 68 additions and 103 deletions

View File

@ -1,14 +1,16 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#ifdef __linux__
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h>
#include <termio.h> #include <termio.h>
#endif
#define DEBUGGER_PIPES_TO_NUMBERS 0 #define DEBUGGER_PIPES_TO_NUMBERS 0
#define DEBUGGER_RANDOM 0 #define DEBUGGER_RANDOM 0
#define AUTOMATISCH 0
#define INFILL_PLAIN_PAPER 0 #define INFILL_PLAIN_PAPER 0
#define INFILL_X_PIPE 1 #define INFILL_X_PIPE 1
@ -38,7 +40,7 @@ void random_color() {
color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))]; color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))];
} }
int getch_directions(){ int getch_pipes(){
int ch; int ch;
struct termios oldattr, newattr; struct termios oldattr, newattr;
@ -59,13 +61,6 @@ 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");
for (int j = 0; j < x; j++) { 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 (!DEBUGGER_PIPES_TO_NUMBERS){
if(grid[i][j] == 0){ if(grid[i][j] == 0){
printf(FILLER); printf(FILLER);
@ -166,7 +161,7 @@ void print_grid(int x, int y, char **grid) {
} }
} }
void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int laenge, int GRID_GROESSE_X, int GRID_GROESSE_Y) { void generate_pipe_x(int GRID_GROESSE_X, int GRID_GROESSE_Y, char **grid, int *x_start, int *y_start, int laenge) {
if(laenge < 0){ if(laenge < 0){
for (int i = *x_start; i > *x_start + laenge; i--) { for (int i = *x_start; i > *x_start + laenge; i--) {
if (i <= -laenge/2 -1) { if (i <= -laenge/2 -1) {
@ -175,7 +170,7 @@ void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int
} }
grid[*y_start][i] = INFILL_X_PIPE; grid[*y_start][i] = INFILL_X_PIPE;
printf("\33[H\033[J"); printf("\33[H\033[J");
print_grid(x, y, grid); print_grid(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
usleep(SLEEP_TIMER); usleep(SLEEP_TIMER);
} }
} else{ } else{
@ -186,7 +181,7 @@ void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int
} }
grid[*y_start][i] = INFILL_X_PIPE; grid[*y_start][i] = INFILL_X_PIPE;
printf("\33[H\033[J"); printf("\33[H\033[J");
print_grid(x, y, grid); print_grid(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
usleep(SLEEP_TIMER); usleep(SLEEP_TIMER);
} }
} }
@ -194,7 +189,7 @@ void generate_pipe_x(int x, int y, char **grid, int *x_start, int *y_start, int
*x_start += laenge; *x_start += laenge;
} }
void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int laenge, int GRID_GROESSE_X, int GRID_GROESSE_Y) { void generate_pipe_y(int GRID_GROESSE_X, int GRID_GROESSE_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 <= -laenge/2) { if (i <= -laenge/2) {
@ -203,7 +198,7 @@ void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int
} }
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
usleep(SLEEP_TIMER); usleep(SLEEP_TIMER);
} }
} else{ } else{
@ -214,24 +209,19 @@ void generate_pipe_y(int x, int y, char **grid, int *x_start, int *y_start, int
} }
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
usleep(SLEEP_TIMER); 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; *y_start += laenge/2;
} }
int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) { int pipes(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
//int main() {
// system("chcp 65001 >null");
// int GRID_GROESSE_X = 120; // x:y -> 2:1 for a square looking grid
// int GRID_GROESSE_Y = 60;
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;
@ -254,59 +244,65 @@ int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
} }
} }
//int richtungswechsler = 1; int richtungswechsler = 1;
while (!DEBUGGER_RANDOM) { while (!DEBUGGER_RANDOM) {
while (!AUTOMATISCH) {
if ((rand() %color_c_p) <= 1) { if ((rand() %color_c_p) <= 1) {
random_color(); random_color();
} }
laenge = ((rand() %max_l)+min_l); laenge = ((rand() %max_l)+min_l);
//if ((rand() %2)-1) { char key_input = (char)getch_pipes();
// laenge = ((rand() %max_l)+min_l);
//} else {
// laenge = ((rand() %max_l)+min_l)*-1;
//}
char key_input = (char)getch_directions();
switch (key_input) { switch (key_input) {
case 'h': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1, GRID_GROESSE_X, GRID_GROESSE_Y); break; 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); 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, GRID_GROESSE_X, GRID_GROESSE_Y); break; case 'w': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
case 'q': return 0; case 'q': return 0;
default: continue; default: continue;
} }
// 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); while (AUTOMATISCH) {
// generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); if ((rand() %color_c_p) <= 1) {
// generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); random_color();
// 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); if ((rand() %2)-1) {
// generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); laenge = ((rand() %max_l)+min_l);
// generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * 0.1); } else {
// generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * 0.9); laenge = ((rand() %max_l)+min_l)*-1;
// generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); }
// }
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);
}
@ -318,34 +314,3 @@ int main_program(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
return 0; return 0;
} }
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;
int status;
while (1) {
pid = fork(); // Erzeuge einen neuen Prozess
if (pid == 0) {
// Kindprozess führt die Hauptlogik aus
main_program(GRID_GROESSE_X, GRID_GROESSE_Y);
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;
}