Compare commits

..

6 Commits
main ... jonas

Author SHA1 Message Date
b6bc24b6d2 updated game.c / game.h 2025-10-20 22:18:45 +02:00
f3cdd0b307 Update game.c 2025-10-20 16:55:22 +02:00
e2747c285f Großbuchstaben einlesen 2025-10-20 15:02:54 +02:00
8d94db8e56 gitignore geändert 2025-10-19 14:32:34 +02:00
2895866ef0 input vervollständigt 2025-10-19 14:30:44 +02:00
0c0a0f39c2 gitignore erweitert 2025-10-17 21:56:40 +02:00
5 changed files with 137 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
I2_Wortsalat/Start_Mac/wordsalad_initial
I2_Wortsalat/Start_Mac/.DS_Store
I2_Wortsalat/.DS_Store
I2_Wortsalat/Start_Mac/input.o
.gitignore
I2_Wortsalat/Start_Mac/game.o
I2_Wortsalat/Start_Mac/runTests
.o
.a

View File

@ -10,10 +10,106 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
}
}
}
}
// Creates the word salad by placing words randomly and filling empty spaces
// - 2D array salad -> fertiger Wortsalat
// - searchFieldLen -> Dimension der Salatschüssel
// - 2D array words -> gegebene Wörter
// - wordCount -> anzahl an Wörtern
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 placedWords = 0;
//Feld komplettmit EMPTY_CHAR füllen
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
//Wörter plazieren
for (int wordNumber = 0; wordNumber < wordCount; wordNumber++)
{
int wordLength = strlen(words[wordNumber]);
int placed = 0;
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
{
int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal
int x = rand() % searchFieldLen;
int y = rand() % searchFieldLen;
if (horizontal)
{
if (x + wordLength > searchFieldLen) continue; // passt nicht
//Prüfen ob kein anderes Wort im Weg
int fits = 1;
for (int i = 0; i < wordLength; i++)
{
if (salad[y][x + i] != EMPTY_CHAR)
{
fits = 0;
break;
}
}
//Wort plazieren
if (fits)
{
for (int i = 0; i < wordLength; i++)
{
salad[y][x + i] = words[wordNumber][i];
}
placed = 1;
placedWords++;
}
}
else //Wort vertikal plazieren
{
if (y + wordLength > searchFieldLen) continue;
//Prüfen ob kein anderes Wort im Weg
int fits = 1;
for (int i = 0; i < wordLength; i++)
{
if (salad[y + i][x] != EMPTY_CHAR)
{
fits = 0;
break;
}
}
//Wort plazieren
if (fits)
{
for (int i = 0; i < wordLength; i++)
{
salad[y + i][x] = words[wordNumber][i];
}
placed = 1;
placedWords++;
}
}
}
}
fillRestWithRandom(salad, searchFieldLen);
return placedWords;
}
// Prints the word salad to console

View File

@ -7,5 +7,6 @@
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif

View File

@ -8,5 +8,36 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char lines [(MAX_WORD_LEN + 2) * maxWordCount]; //Es wird davon ausgegangen, dass die Wörter nur durch eine Trennzeichen und ein Leerzeichen von einander gertrennt gespeichert werden
int word_counter = 0;
while (fgets(lines, sizeof(lines) , file) != NULL)
{
for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String
{
lines[i] = toupper(lines[i]);
if (lines[i] == '\n')
{
lines[i] = '\0';
break;
}
}
char *single_word = strtok(lines, " ;,");
while (single_word != NULL && word_counter < maxWordCount)
{
strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1);
words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist
word_counter++;
single_word = strtok(NULL, " ;,");
}
}
return word_counter;
}