game.c implementiert

This commit is contained in:
fischerch103508 committed 2026-10-17 17:58:42 +02:00
1 parent 7eaf6e2f1f
commit 1f5b8ad0a2
2 files changed
+39 -46

No files matched your search

+37 -45
View File
@@ -18,8 +18,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int x = 0; int x = 0;
int y = 0; int y = 0;
int placedWords = 0; int placedWords = 0;
int i = 0;
int nichtPlatziert =0;
typedef enum {vertikal, horizontal} Platzierungsrichtung; typedef enum {vertikal, horizontal} Platzierungsrichtung;
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen
@@ -30,72 +28,66 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
while(placedWords < wordCount) for(int i = 0; i < wordCount; i++)
{ {
x = rand() % 20 +1; int platziert = 0;
y = rand() % 20 +1; int platzierversuche = 0;
Platzierungsrichtung zufaellige_richtung = rand() % 2; while(platziert == 0 && platzierversuche < MAX_RAND_TRIES_PER_WORD )
{
platzierversuche++;
x = rand() % searchFieldLen;
y = rand() % searchFieldLen;
if(zufaellige_richtung == 0 && salad[y][x] == '0') Platzierungsrichtung zufaellige_richtung = rand() % 2;
if(zufaellige_richtung == vertikal && strlen(words[i]) <= (searchFieldLen-y))
{ {
int check = 0;
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-y)) // -1, da Nullzeichen sowieso weg muss for(int k = 0; k < strlen(words[i]); k++)
{ {
for(int j = 0; j < sizeof(words[i]-1); j++) if(salad[y+k][x] == '0' || salad[y+k][x] == words[i][k])
{ {
if(salad[(y+j)][x] == '0') check ++;
{
strncpy(&salad[(y+j)][x], &words[i][j], 1);
}
else if(&salad[(y+j)][x] == &words[i][j])
{
}
else
{
nichtPlatziert = 1;
}
} }
if(nichtPlatziert == 0) }
if(check == strlen(words[i]))
{
for(int j = 0; j < strlen(words[i]); j++)
{ {
placedWords++; salad[y+j][x] = words[i][j];
i++;
} }
placedWords++;
platziert = 1;
} }
} }
else if( zufaellige_richtung == 1 && salad[y][x] == '0') else if( zufaellige_richtung == horizontal && strlen(words[i]) <= (searchFieldLen-x))
{
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-x)) // -1, da Nullzeichen sowieso weg muss
{ {
for(int j = 0; j < sizeof(words[i]-1); j++) int check = 0;
for(int k = 0; k < strlen(words[i]); k++)
{ {
if(salad[y][(x+j)] == '0') if(salad[y][x+k] == '0' || salad[y][x+k] == words[i][k])
{ {
strncpy(&salad[y][(x+j)], &words[i][j], 1); check ++;
}
else if(&salad[y][(x+j)] == &words[i][j])
{
}
else
{
nichtPlatziert = 1;
} }
} }
if(nichtPlatziert == 0)
if(check == strlen(words[i]))
{ {
for(int j = 0; j < strlen(words[i]); j++)
{
salad[y][x+j] = words[i][j];
}
placedWords++; placedWords++;
i++; platziert = 1;
} }
} }
} }
} }
for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++)
{ {
for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{ {
@@ -106,5 +98,5 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
return placedWords; return placedWords;
} }
+2 -1
View File
@@ -43,9 +43,10 @@ int main(int argc, char *argv[])
} }
else else
{ {
fprintf(stderr, "konnte nicht alle Wörter platzieren %d von %d \n", placedWords, wordCount); fprintf(stderr, "konnte nicht alle Woerter platzieren %d von %d \n", placedWords, wordCount);
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
}
else else
{ {
// Print error message if file couldn't be opened // Print error message if file couldn't be opened