Compare commits
No commits in common. "48bee0edc1ecfdbfe2b841a173ae72c197fd0480" and "b89e6d7abaf0f2acb662f04efb12bb2cd47bae74" have entirely different histories.
48bee0edc1
...
b89e6d7aba
171
src/pipes_test.c
171
src/pipes_test.c
|
@ -1,28 +1,23 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
#ifdef __linux__
|
#include <sys/wait.h>
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <unistd.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
|
||||||
#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"
|
||||||
|
@ -40,27 +35,17 @@ void random_color() {
|
||||||
color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))];
|
color = colors[rand() % (sizeof(colors) / sizeof(colors[0]))];
|
||||||
}
|
}
|
||||||
|
|
||||||
int getch_pipes(){
|
|
||||||
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");
|
||||||
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);
|
||||||
|
@ -161,27 +146,19 @@ void print_grid(int x, int y, char **grid) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_pipe_x(int GRID_GROESSE_X, int GRID_GROESSE_Y, char **grid, int *x_start, int *y_start, int laenge) {
|
void generate_pipe_x(int x, int 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) {
|
|
||||||
*x_start = GRID_GROESSE_X + laenge/2;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
|
print_grid(x, y, grid);
|
||||||
usleep(SLEEP_TIMER);
|
usleep(SLEEP_TIMER);
|
||||||
}
|
}
|
||||||
} else{
|
} else{
|
||||||
for (int i = *x_start; i < *x_start + laenge; i++) {
|
for (int i = *x_start; i < *x_start + laenge; i++) {
|
||||||
if (i >= GRID_GROESSE_X - laenge/2+1) {
|
|
||||||
*x_start = laenge/2;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
|
print_grid(x, y, grid);
|
||||||
usleep(SLEEP_TIMER);
|
usleep(SLEEP_TIMER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,39 +166,36 @@ void generate_pipe_x(int GRID_GROESSE_X, int GRID_GROESSE_Y, char **grid, int *x
|
||||||
*x_start += laenge;
|
*x_start += laenge;
|
||||||
}
|
}
|
||||||
|
|
||||||
void generate_pipe_y(int GRID_GROESSE_X, int GRID_GROESSE_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 <= -laenge/2) {
|
|
||||||
*y_start = GRID_GROESSE_Y + laenge/2;
|
|
||||||
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
|
print_grid(x, y, grid);
|
||||||
usleep(SLEEP_TIMER);
|
usleep(SLEEP_TIMER);
|
||||||
}
|
}
|
||||||
} 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 >= (GRID_GROESSE_Y / 2)*2 - 1) {
|
|
||||||
*y_start = laenge/2;
|
|
||||||
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(GRID_GROESSE_X, GRID_GROESSE_Y, grid);
|
print_grid(x, 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 pipes(int GRID_GROESSE_X, int GRID_GROESSE_Y) {
|
int main_program() {
|
||||||
//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;
|
||||||
|
@ -245,51 +219,26 @@ int pipes(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();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if ((rand() %2)-1) {
|
||||||
laenge = ((rand() %max_l)+min_l);
|
laenge = ((rand() %max_l)+min_l);
|
||||||
|
} else {
|
||||||
char key_input = (char)getch_pipes();
|
laenge = ((rand() %max_l)+min_l)*-1;
|
||||||
|
}
|
||||||
switch (key_input) {
|
if (richtungswechsler == 1) {
|
||||||
case 'h': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
|
generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge);
|
||||||
case 'a': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge * -1); break;
|
richtungswechsler *= -1;
|
||||||
case 'l': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
|
} else {
|
||||||
case 'd': generate_pipe_x(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
|
generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge);
|
||||||
case 'j': generate_pipe_y(GRID_GROESSE_X, GRID_GROESSE_Y, grid, &x_start, &y_start, laenge); break;
|
richtungswechsler *= -1;
|
||||||
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;
|
|
||||||
case 'q': return 0;
|
|
||||||
default: continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
while (AUTOMATISCH) {
|
|
||||||
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) {
|
if (DEBUGGER_RANDOM) {
|
||||||
|
@ -307,9 +256,35 @@ int pipes(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
int pipes(int GRIG_GROESSE_X, int GRID_GROESSE_Y);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int pipes();
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue