This commit is contained in:
Selina Buhlheller 2025-11-06 14:46:28 +01:00
parent b4cc13d96a
commit 56b3b9efa1
5 changed files with 72 additions and 19 deletions

View File

@ -15,9 +15,9 @@
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));
int placed_words = 0;
int placedWords = 0;
// Array mit 0 füllen
// Array mit . füllen
for (int r = 0; r < searchFieldLen; r++){
for(int s = 0; s < searchFieldLen; s++){
salad[r][s] = '.';
@ -29,73 +29,77 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
// Wörter in salad einsortieren
for (int i = 0; i < wordCount; i++){
for (int num_words = 0; num_words < wordCount; num_words++){
int laenge = strlen(words[i]);
int laenge = strlen(words[num_words]);
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
// frei == 1 -> Zeile nicht frei
// isalpha == 0 -> kein Buchstabe
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++){
int richtung = rand()% 2;
int frei = 0;
int belegt = 0;
// horizontale Eingabe
if (richtung == 1){
int zeile = rand()% searchFieldLen;
int spalte = rand()% (searchFieldLen- laenge + 1);
int spalte = rand()% (searchFieldLen- laenge +1);
//prüft, ob die herausgesuchte Zeile noch frei ist
// belegt == 1 -> Zeile nicht frei
for (int o = spalte; o < spalte + laenge; o++){
if(salad[zeile][o] != '.'){
frei = 1;
belegt = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei == 0){
if (belegt == 0){
for(int k = 0; k < laenge; k++){
salad[zeile][spalte + k] = words[i][k];
salad[zeile][spalte + k] = words[num_words][k];
}
placed_words++;
placedWords++;
break;
}
}
// vertikale Eingabe
else if (richtung == 0){
int zeile = rand()% (searchFieldLen - laenge + 1);
int zeile = rand()% (searchFieldLen - laenge +1);
int spalte = rand()% searchFieldLen;
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int n = zeile; n < zeile + laenge; n++){
if(salad[n][spalte] != '.'){
frei = 1;
belegt = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei == 0){
if (belegt == 0){
for(int j = 0; j < laenge; j++){
salad[zeile + j][spalte] = words[i][j];
salad[zeile + j][spalte] = words[num_words][j];
}
placed_words++;
placedWords++;
break;
}
}
else{
break;
}
}
}
// fügt zufällige Buchstaben ein
for(int l = 0; l < searchFieldLen; l++){
for(int m = 0; m < searchFieldLen; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
char alphabet [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
@ -105,7 +109,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
}
}
return placed_words;
return placedWords;
}
// Prints the word salad to console

Binary file not shown.

View File

@ -2,6 +2,55 @@
#include <string.h>
#include <ctype.h>
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
// MAX_WORD_LEN 100
// MAX_LINE_LEN 1024
// *file ist Datei
// words ist Array
// MAX_WORD_LEN ist maximale Wortlänge
// maxWordCount ist maximale Anzahl an Wörtern
char puffer[MAX_LINE_LEN];
int counter = 0;
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
{
char *parts = strtok(puffer, ",;\n\t/. ");
while(parts != NULL && counter < maxWordCount)
{
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
//words[counter][MAX_WORD_LEN-1] = "\0";
//Großbuchstaben
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
{
words[counter][i] = toupper(parts[i]);
words[counter][i] = '\0';
}
}
counter++; // Wort eingelesen -> Wortzähler erhöhen
parts = strtok(NULL, ",;\n\t/. ");
}
return counter;
}
/* #include "input.h"
#include <string.h>
#include <ctype.h>
#define MAX_NUMBER_OF_WORDS 100
#define MAX_WORD_LEN 100
// TODO:

Binary file not shown.