verbesserte kommentare und kleine änderung in input.c

This commit is contained in:
od49ukup 2025-11-06 12:49:21 +01:00
parent 76ecf700bb
commit 2b28d19e79
4 changed files with 31 additions and 30 deletions

View File

@ -10,22 +10,21 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
//edited
//enum für richtungen
typedef enum { HORIZONTAL, VERTICAL } Direction;
//edited: bestimmt zufällige richtung
//bestimmt zufällige richtung
static Direction randomDirection(void)
{
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
}
//edited
// Creates the word salad by placing words randomly and filling empty spaces
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
srand(time(NULL)); // seed bei jedem funktionsaufruf neu setzen
srand(time(NULL)); //damit bei jedme aufruf "neuer zufall" (seed wird immer auf aktuelle zeit gesetzt)
// spielfeld initialisieren (leer)
// spielfeld initialisieren mit nullen
for (int y = 0; y < searchFieldLen; y++) {
for (int x = 0; x < searchFieldLen; x++) {
salad[y][x] = EMPTY_CHAR;
@ -34,25 +33,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int placedWrdsAmnt = 0;
//wörter platzieren, 1 schleife = 1 wort
//wörter platzieren
for(int w = 0; w < wordCount; w++){
const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile)
const char *word = words[w];
int len = strlen(word);
if(len > searchFieldLen ) // wenn wort zu lang, nimm nächstes wort
if(len > searchFieldLen )
continue;
int placed = 0;
// versuche mehrere random positionen, 1 schleife = 1 position
//versucht max 10 versch. positionen
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){
Direction dir = randomDirection();
int startX = 0;
int startY = 0;
if(dir == HORIZONTAL){
startX = rand() % (searchFieldLen - len + 1); //zeile ist 10 (0-9) breit, wort 5 lang -> höchstens in 5-9 -> 10 - 5 + 1 = 6 (startet random iwo von 0 bis 5)✅
startY = rand() % searchFieldLen; //egal
startX = rand() % (searchFieldLen - len + 1); //zb zeile ist 10 breit (0-9), wort 5 lang -> start nur in 0-5 möglich -> 10 - 5 + 1 = 6 (rand() % 6 = 0 bis 5)
startY = rand() % searchFieldLen; //egal wo
}else{
startX = rand() % searchFieldLen;
startY = rand() % (searchFieldLen - len + 1);
@ -61,18 +60,19 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
//passt wort rein? (komplett leer/hat einen überschneidenen buchstaben) --> geht ganze wort platz durch (vert oder horiz), 1 schleife = 1 buchstabe im wort
int fits = 1;
for (int i = 0; i < len; i++) {
int x = startX + (dir == HORIZONTAL ? i : 0);
int x = startX + (dir == HORIZONTAL ? i : 0); //wenn zb horiz. dann erhöhe x um i, sonst bleibt x konst.
int y = startY + (dir == VERTICAL ? i : 0);
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) { // wenn die reihe mit einem buchstaben befüllt der sich nd mit wort überschneidet -> fitted nd
//wenn i.d. reihe/spalte ein buchstabe der sich nicht mit dem aktuellen wort überschneidet -> fitted nicht, stopt DIESE for loop
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) {
fits = 0;
break;
}
}
if(!fits){
continue; // versucht nächste position
continue; //wenn wort nicht fitted nächster positionsversuch
}
//place word wenn posi passt
//wenn wort fitted -> setze ins spielfeld
for(int i = 0; i < len; i++){
int x = startX + (dir == HORIZONTAL ? i : 0);
int y = startY + (dir == VERTICAL ? i : 0);

View File

@ -9,29 +9,30 @@
// Read words from file and store in 'words' array
char zeile[500];
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) //words[zeilen also wörter][spalten also anzahl]
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int anzahlWoerter = 0;
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ // eine zeile
char *wort = strtok(zeile, ",; \n");
//1 durchlauf = 1 zeile, solange wortlimit nicht erreicht und fgets eine zeile liefern kann
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
char *wort = strtok(zeile, ",; \n");
while(wort != NULL && anzahlWoerter <= maxWordCount){
if(strlen(wort) == 0){
//1 durchlauf = 1 wort, solange weiteres wort vorhanden und limit nicht erreicht
while(wort != NULL && anzahlWoerter <= maxWordCount){
if(strlen(wort) == 0){
wort = strtok(NULL, ",; \n");
continue;
}
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN -1);
words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0';
strcpy(words[anzahlWoerter], wort);
//aktuelles wort gross schreiben
for(int i = 0; words[anzahlWoerter][i]; i++){
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //jedes wort das eingelesen wurde wird komplett gross geschrieben, unsigned damit toupper kein neg. arg bekommt (zb umlaute)
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //unsigned damit toupper keie neg. zahlen übergeben bekommt bei zb sonderzeichen und umlauten
}
anzahlWoerter++;
wort = strtok(NULL, ",; \n");
}
}
return anzahlWoerter;

View File

@ -30,25 +30,25 @@ int main(int argc, char *argv[])
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //funktion aus input.c, returned anzahl wörter
fclose(file);
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //funktion aus game.c, returned in spielfeld platzierte wörter
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
int windowWidth = 880;
int windowWidth = 600;
//checkt ob alle wörter aus .txt übernommen wurden
if(placedWords == wordCount){
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
}else{
printf("Fehler: Nicht alle Wörter konnten platziert werden.\n");
}
}
else
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);

Binary file not shown.