This commit is contained in:
Torsten Stock 2025-11-06 14:26:31 +01:00
commit 0aa55ef2d5
2 changed files with 48 additions and 2 deletions

View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 20
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: //TODO: Spiellogik implementieren:
@ -13,6 +13,13 @@
// 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)
{ {
int saladclone[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
int wordsplacedcounter=0;
for (int i=0;i<MAX_SEARCH_FIELD_LEN;i++){
for (int j=0;j<MAX_SEARCH_FIELD_LEN;j++) {
saladclone[i][j] = 0;
}
}
srand(time(NULL)); srand(time(NULL));
for (int i = 0;i < searchFieldLen; i++) { //Feld mit random Buchstaben füllen for (int i = 0;i < searchFieldLen; i++) { //Feld mit random Buchstaben füllen
@ -33,30 +40,61 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
int wordLen = strlen(word); //Länge des Wortes wird ermittelt int wordLen = strlen(word); //Länge des Wortes wird ermittelt
int horizontal = wordOrientation[w]; //horizontal bekommt 0 und 1 aus dem array int horizontal = wordOrientation[w]; //horizontal bekommt 0 und 1 aus dem array
int wordPlaced = 0; int wordPlaced = 0;
int check=0;
for (int tries = 0;tries < MAX_RAND_TRIES_PER_WORD && !wordPlaced;tries++) { //Das Wort wird entweder in eine Zeile oder Spalte random plaziert for (int tries = 0;tries < MAX_RAND_TRIES_PER_WORD && !wordPlaced;tries++) { //Das Wort wird entweder in eine Zeile oder Spalte random plaziert
int row = rand() % searchFieldLen; int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen; int col = rand() % searchFieldLen;
check = 0;
if (horizontal) { if (horizontal) {
if (col + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt if (col + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt
continue; continue;
for (int i=0;i < wordLen; i++) {
if (saladclone[row][col + i]==1) {
check=1;
}
}
if (check == 1) continue;
printf("Wort: %d,%d \n", row,col);
for (int i = 0;i < wordLen; i++) { //Wort wird in Spalte geschrieben for (int i = 0;i < wordLen; i++) { //Wort wird in Spalte geschrieben
salad[row][col + i] = word[i]; salad[row][col + i] = word[i];
saladclone[row][col + i] = 1;
} }
wordPlaced = 1; wordPlaced = 1;
wordsplacedcounter++;
} }
else { else {
if (row + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt if (row + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt
continue; continue;
for (int i=0;i < wordLen; i++) {
if (saladclone[row+i][col]==1) {
check=1;
}
}
if (check == 1) continue;
printf("Wort: %d,%d \n", row,col);
for (int i = 0;i < wordLen; i++) { //Wort wird in Zeile geschrieben for (int i = 0;i < wordLen; i++) { //Wort wird in Zeile geschrieben
salad[row + i][col] = word[i]; salad[row + i][col] = word[i];
saladclone[row+i][col] = 1;
} }
wordPlaced = 1; wordPlaced = 1;
wordsplacedcounter++;
} }
} }
} }
return 0; return wordsplacedcounter;
} }
// Prints the word salad to console // Prints the word salad to console

View File

@ -35,6 +35,14 @@ int main(int argc, char *argv[])
// 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); placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
if (placedWords == wordCount) {
showWordSalad(wordSalad, SALAD_SIZE);
startGame(wordSalad,SALAD_SIZE,words,wordCount,1000);
}else {
printf("Es konnten nur %d Woerter plaziert werden.\n", placedWords);
printf("-> Programm abgebrochen!!!");
}
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed