3 Commits
Author SHA1 Message Date
fischerch103508 1c30711a9c finales Programm 2026-10-23 10:24:40 +02:00
fischerch103508 1f5b8ad0a2 game.c implementiert 2026-10-17 17:58:42 +02:00
fischerch103508 7eaf6e2f1f main abgeaendert 2026-10-17 15:29:22 +02:00
3 changed files with 57 additions and 61 deletions

No files matched your search

+52 -59
View File
@@ -5,6 +5,7 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
#define WORTLAENGE strlen(words[i])
//TODO: Spiellogik implementieren: //TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
@@ -18,8 +19,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,81 +29,75 @@ 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 )
if(zufaellige_richtung == 0 && salad[y][x] == '0')
{
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-y)) // -1, da Nullzeichen sowieso weg muss
{
for(int j = 0; j < sizeof(words[i]-1); j++)
{
if(salad[(y+j)][x] == '0')
{
strncpy(&salad[(y+j)][x], &words[i][j], 1);
}
else if(&salad[(y+j)][x] == &words[i][j])
{
}
else
{
nichtPlatziert = 1;
}
}
if(nichtPlatziert == 0)
{
placedWords++;
i++;
}
}
}
else if( zufaellige_richtung == 1 && salad[y][x] == '0')
{ {
if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-x)) // -1, da Nullzeichen sowieso weg muss platzierversuche++;
x = rand() % searchFieldLen;
y = rand() % searchFieldLen;
Platzierungsrichtung zufaellige_richtung = rand() % 2;
if(zufaellige_richtung == vertikal && (WORTLAENGE <= (searchFieldLen-y)))
{
int check = 0;
for(int k = 0; k < WORTLAENGE; 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][(x+j)] == '0') check ++;
{ }
strncpy(&salad[y][(x+j)], &words[i][j], 1); }
if(check == WORTLAENGE)
} {
else if(&salad[y][(x+j)] == &words[i][j]) for(int j = 0; j < WORTLAENGE; j++)
{ {
salad[y+j][x] = words[i][j];
}
else
{
nichtPlatziert = 1;
}
} }
if(nichtPlatziert == 0) placedWords++;
platziert = 1;
}
}
else if( zufaellige_richtung == horizontal && WORTLAENGE <= (searchFieldLen-x))
{
int check = 0;
for(int k = 0; k < WORTLAENGE; k++)
{ {
if(salad[y][x+k] == '0' || salad[y][x+k] == words[i][k])
{
check ++;
}
}
if(check == WORTLAENGE)
{
for(int j = 0; j < WORTLAENGE; 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++)
{ {
if(salad[i][j] == '0') if(salad[i][j] == '0')
{ {
salad[i][j] = rand() % (90-65+1)+ 65; salad[i][j] = rand() % (90-65+1)+ 65; //Großbuchstaben
} }
} }
} }
return placedWords; return placedWords;
} }
-1
View File
@@ -25,7 +25,6 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
} }
strncpy(words[wordCount], token, MAX_WORD_LEN -1); //speichert einzelnes Wort in das gegeben word array ab und holt sich das nächste im Puffer, bis array leer token == NULL strncpy(words[wordCount], token, MAX_WORD_LEN -1); //speichert einzelnes Wort in das gegeben word array ab und holt sich das nächste im Puffer, bis array leer token == NULL
//words[wordCount][MAX_WORD_LEN - 1] = '\0';
wordCount++; wordCount++;
+5 -1
View File
@@ -41,7 +41,11 @@ int main(int argc, char *argv[])
{ {
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
} }
else
{
fprintf(stderr, "konnte nicht alle Woerter platzieren %d von %d \n", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
} }
else else
{ {