2 Commits
Author SHA1 Message Date
od49ukup 538528e5ec lauffähiges programm, besteht alle unit tests 2025-11-02 20:49:43 +01:00
od49ukup 300afb0db8 lauffähig und alle unit tests bestanden 2025-11-02 20:33:46 +01:00
4 changed files with 30 additions and 31 deletions
+15 -15
View File
@@ -10,21 +10,22 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
//enum für richtungen //edited
typedef enum { HORIZONTAL, VERTICAL } Direction; typedef enum { HORIZONTAL, VERTICAL } Direction;
//bestimmt zufällige richtung //edited: bestimmt zufällige richtung
static Direction randomDirection(void) static Direction randomDirection(void)
{ {
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL; return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
} }
//edited
// Creates the word salad by placing words randomly and filling empty spaces // 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) 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)); //damit bei jedme aufruf "neuer zufall" (seed wird immer auf aktuelle zeit gesetzt) srand(time(NULL)); // seed bei jedem funktionsaufruf neu setzen
// spielfeld initialisieren mit nullen // spielfeld initialisieren (leer)
for (int y = 0; y < searchFieldLen; y++) { for (int y = 0; y < searchFieldLen; y++) {
for (int x = 0; x < searchFieldLen; x++) { for (int x = 0; x < searchFieldLen; x++) {
salad[y][x] = EMPTY_CHAR; salad[y][x] = EMPTY_CHAR;
@@ -33,25 +34,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int placedWrdsAmnt = 0; int placedWrdsAmnt = 0;
//wörter platzieren //wörter platzieren, 1 schleife = 1 wort
for(int w = 0; w < wordCount; w++){ for(int w = 0; w < wordCount; w++){
const char *word = words[w]; const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile)
int len = strlen(word); int len = strlen(word);
if(len > searchFieldLen ) if(len > searchFieldLen ) // wenn wort zu lang, nimm nächstes wort
continue; continue;
int placed = 0; int placed = 0;
//versucht max 10 versch. positionen // versuche mehrere random positionen, 1 schleife = 1 position
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){ for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){
Direction dir = randomDirection(); Direction dir = randomDirection();
int startX = 0; int startX = 0;
int startY = 0; int startY = 0;
if(dir == HORIZONTAL){ if(dir == HORIZONTAL){
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) 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 wo startY = rand() % searchFieldLen; //egal
}else{ }else{
startX = rand() % searchFieldLen; startX = rand() % searchFieldLen;
startY = rand() % (searchFieldLen - len + 1); startY = rand() % (searchFieldLen - len + 1);
@@ -60,19 +61,18 @@ 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 //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; int fits = 1;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
int x = startX + (dir == HORIZONTAL ? i : 0); //wenn zb horiz. dann erhöhe x um i, sonst bleibt x konst. int x = startX + (dir == HORIZONTAL ? i : 0);
int y = startY + (dir == VERTICAL ? i : 0); int y = startY + (dir == VERTICAL ? i : 0);
//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]) { // wenn die reihe mit einem buchstaben befüllt der sich nd mit wort überschneidet -> fitted nd
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) {
fits = 0; fits = 0;
break; break;
} }
} }
if(!fits){ if(!fits){
continue; //wenn wort nicht fitted nächster positionsversuch continue; // versucht nächste position
} }
//wenn wort fitted -> setze ins spielfeld //place word wenn posi passt
for(int i = 0; i < len; i++){ for(int i = 0; i < len; i++){
int x = startX + (dir == HORIZONTAL ? i : 0); int x = startX + (dir == HORIZONTAL ? i : 0);
int y = startY + (dir == VERTICAL ? i : 0); int y = startY + (dir == VERTICAL ? i : 0);
+10 -11
View File
@@ -9,30 +9,29 @@
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
char zeile[500]; char zeile[500];
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) //words[zeilen also wörter][spalten also anzahl]
{ {
int anzahlWoerter = 0; int anzahlWoerter = 0;
//1 durchlauf = 1 zeile, solange wortlimit nicht erreicht und fgets eine zeile liefern kann while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ // eine zeile
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ char *wort = strtok(zeile, ",; \n");
char *wort = strtok(zeile, ",; \n");
//1 durchlauf = 1 wort, solange weiteres wort vorhanden und limit nicht erreicht while(wort != NULL && anzahlWoerter <= maxWordCount){
while(wort != NULL && anzahlWoerter <= maxWordCount){ if(strlen(wort) == 0){
if(strlen(wort) == 0){
wort = strtok(NULL, ",; \n"); wort = strtok(NULL, ",; \n");
continue; 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++){ for(int i = 0; words[anzahlWoerter][i]; i++){
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //unsigned damit toupper keie neg. zahlen übergeben bekommt bei zb sonderzeichen und umlauten 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)
} }
anzahlWoerter++; anzahlWoerter++;
wort = strtok(NULL, ",; \n"); wort = strtok(NULL, ",; \n");
} }
} }
return anzahlWoerter; return anzahlWoerter;
+5 -5
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 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 // Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //funktion aus input.c, returned anzahl wörter wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file); fclose(file);
// Create the word salad by placing words into grid // Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //funktion aus game.c, returned in spielfeld platzierte wörter placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed
// Start the game if successful // Start the game if successful
// error message if some words couldn't be placed // error message if some words couldn't be placed
int windowWidth = 600; int windowWidth = 880;
//checkt ob alle wörter aus .txt übernommen wurden
if(placedWords == wordCount){ if(placedWords == wordCount){
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth); startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
}else{ }else{
printf("Fehler: Nicht alle Wörter konnten platziert werden.\n"); printf("Fehler: Nicht alle Wörter konnten platziert werden.\n");
} }
} }
else else
{ {
// Print error message if file couldn't be opened // Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
Binary file not shown.