fertige Version Selina

This commit is contained in:
Selina Buhlheller 2025-11-06 09:53:31 +01:00
parent 3d0b1d6729
commit 765879d085
2 changed files with 82 additions and 42 deletions

View File

@ -13,57 +13,96 @@
// 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)
{
// Wörter in salad einsortieren
// strtok durch words ersetzen, pro Zeile ein Wort
char * einzelwort = strtok(words, "\0");
for (int i = 0; i < wordCount; i++){
int einzelwort_laenge = strlen(einzelwort);
int richtung = rand()% 2;
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
if (richtung == 1){
for(int k = 0; k < einzelwort_laenge; k++){
int zeile = rand()% MAX_SEARCH_FIELD_LEN;
int spalte = rand()% MAX_SEARCH_FIELD_LEN- einzelwort_laenge + 1;
salad[zeile][spalte + k] = einzelwort[k];
}
}
else if (richtung == 0){
for(int j = 0; j < einzelwort_laenge; j++){
int zeile = rand()% MAX_SEARCH_FIELD_LEN - einzelwort_laenge + 1;
int spalte = rand()% MAX_SEARCH_FIELD_LEN;
salad[zeile + k][spalte] = einzelwort[k];
}
}
strtok(NULL, "\0");
}
// zufällige Buchstaben erzeugen
srand(time(NULL));
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
//prüfen, ob Wörter vorhanden sind mit isalpha
// -> erst wenn die ganze Länge des Wortes frei ist, array hinzufügen
// MAX Versuche das Wort zu platzieren == 10 -> hinzufügen
// MAX Versuche das Wort zu platzieren == 10
// isalpha == 0 -> kein Buchstabe
for(int l = 0; l < MAX_SEARCH_FIELD_LEN; l++){
for(int m = 0; m < MAX_SEARCH_FIELD_LEN; m++ ){
if(isalpha(salad[l][m]) == 0 )
salad[m][n] == zufallsbuchstabe;
// Wörter in salad einsortieren
for (int i = 0; i < wordCount; i++){
char einzelwort[] = words[i]
int einzelwort_laenge = strlen(einzelwort);
// muss noch prüfen, ob in der Zeile schon was ist
// 1 ist horizontal, 0 ist vertikal
// frei == 1 -> Zeile nicht frei
for(int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++ ){
int richtung = rand()% 2;
int frei = 0;
// horizontale Eingabe
if (richtung == 1){
int zeile = rand()% MAX_SEARCH_FIELD_LEN;
int spalte = rand()% MAX_SEARCH_FIELD_LEN- einzelwort_laenge + 1;
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int o = spalte; o < spalte + einzelwort_laenge; o++){
if(isalpha(salad[zeile][o]) == 0){
frei = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei = 0){
for(int k = 0; k < einzelwort_laenge; k++){
salad[zeile][spalte + k] = einzelwort[k];
versuch = MAX_RAND_TRIES_PER_WORD;
}
}
}
// vertikale Eingabe
else if (richtung == 0){
int zeile = rand()% MAX_SEARCH_FIELD_LEN - einzelwort_laenge + 1;
int spalte = rand()% MAX_SEARCH_FIELD_LEN;
//prüft, ob die herausgesuchte Zeile noch frei ist
for (int n = zeile; n < zeile + einzelwort_laenge; n++){
if(isalpha(salad[n][spalte]) == 0){
frei = 1;
break;
}
}
// setzt in das Array ein, wenn frei oder erhöht die Versuchsanzahl
if (frei = 0){
for(int j = 0; j < einzelwort_laenge; j++){
salad[zeile + j][spalte] = einzelwort[j];
versuch = MAX_RAND_TRIES_PER_WORD;
}
}
}
// gibt über searchFieldLen zurück, dass ein Wort nicht einsortiert wurde
if(versuch = MAX_RAND_TRIES_PER_WORD-1){
searchFieldLen = 1;
}
}
}
// fügt zufällige Buchstaben ein
for(int l = 0; l < MAX_SEARCH_FIELD_LEN; l++){
for(int m = 0; m < MAX_SEARCH_FIELD_LEN; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
srand(time(NULL));
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
// zufällige Buchstaben einfügen
salad[m][n] == zufallsbuchstabe;
}
}
}
return searchFieldLen
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for(int p = 0; p < MAX_SEARCH_FIELD_LEN; p++){
for(int q = 0; q < MAX_SEARCH_FIELD_LEN; q++){
printf("%s", salad[p][q]);
}
}
}

View File

@ -40,7 +40,8 @@ int main(int argc, char *argv[])
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (wordsalad[] == words[]){
int placed = showWordSalad(salad, searchFieldLen)
if ( placed != 1){
showWordSalad(salad, searchFieldLen);
}
else{