Compare commits

...
4 Commits
3 changed files with 153 additions and 2 deletions

No files matched your search

+89 -1
View File
@@ -10,8 +10,96 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces
enum PlaceDirection {
HORIZONTAL,
VERTICAL
};
bool canPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word, unsigned int wordLength, unsigned int row, unsigned int col, PlaceDirection dir)
{
if (dir == HORIZONTAL) // HORIZONTAL
{
if (col + wordLength > searchFieldLen) return false;
for (unsigned int i = 0; i < wordLength; i++)
{
char current = salad[row][col + i];
if (current != EMPTY_CHAR && current != word[i]) return false;
}
}
else // VERTICAL
{
if (row + wordLength > searchFieldLen) return false;
for (unsigned int i = 0; i < wordLength; i++)
{
char current = salad[row + i][col];
if (current != EMPTY_CHAR && current != word[i]) return false;
}
}
return true;
}
// write word in the grid
void writeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char *word, unsigned int wordLength, unsigned int row, unsigned int col, PlaceDirection dir)
{
for (unsigned int i = 0; i < wordLength; i++)
{
if (dir == HORIZONTAL)
{
salad[row][col + i] = word[i];
}
else
{
salad[row + i][col] = word[i];
}
}
}
// try to place a word with a return value
bool placeWordAttempt(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word, unsigned int wordLength, PlaceDirection dir)
{
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
{
unsigned int row, col;
if (dir == HORIZONTAL)
{
row = rand() % searchFieldLen;
col = rand() % (searchFieldLen - wordLength + 1);
}
else // VERTICAL
{
row = rand() % (searchFieldLen - wordLength + 1);
col = rand() % searchFieldLen;
}
if (canPlaceWord(salad, searchFieldLen, word, wordLength, row, col, dir))
{
writeWord(salad, word, wordLength, row, col, dir);
return true;
}
}
return false; // could not be placed after MAX_RAND_TRIES_PER_WORD tries
}
// 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)
{
// place words randomly in salad array
for (unsigned int i = 0; i < wordCount; i++)
{
const char *word = words[i];
unsigned int len = strlen(word);
PlaceDirection dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
placeWordAttempt(salad, searchFieldLen, word, len, dir);
}
// fill up empty fields
for (unsigned int r = 0; r < searchFieldLen; r++)
{
for (unsigned int c = 0; c < searchFieldLen; c++)
{
if (salad[r][c] == EMPTY_CHAR)
{
salad[r][c] = 'A' + (rand() % 26);
}
}
}
return 0;
}
+54
View File
@@ -4,9 +4,63 @@
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Liest eine Zeile und uns schreibt oder speichert Sie im buffer. Unserre Rückgabe wird sein: A) entweder buffer (als C-String) oder NULL, wenn kein Wort mehr gelesen werden kann.
static char *readWord(FILE *file, char *buffer, unsigned int bufferSize)
{
char line[MAX_LINE_LEN];
unsigned int length = 0;
unsigned int i = 0;
if (file == NULL || buffer == NULL || bufferSize == 0)
{
return NULL;
}
// Zeile für Zeile lesen, bis ein nicht-leeres Wort gefunden ist
while (fgets(line, MAX_LINE_LEN, file) != NULL)
{
// Zeilenumbruch, Wagenrücklauf und Leerzeichen am Ende entfernen
length = strlen(line);
while (length > 0 && (line[length - 1] == '\n' ||
line[length - 1] == '\r' ||
line[length - 1] == ' '))
{
length--;
line[length] = '\0';
}
// Leere Zeilen überspringen
if (length > 0)
{
// Sicher in den Zielpuffer kopieren und terminieren
strncpy(buffer, line, bufferSize - 1);
buffer[bufferSize - 1] = '\0';
// In Großbuchstaben umwandeln (das Spielfeld wird mit 'A'-'Z' gefüllt)
for (i = 0; buffer[i] != '\0'; i++)
{
buffer[i] = toupper((unsigned char)buffer[i]);
}
return buffer;
}
}
return NULL; // Dateiende erreicht
}
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
unsigned int count = 0;
// Wörter lesen, bis das Array voll ist oder die Datei zu Ende ist
while (count < maxWordCount && readWord(file, words[count], MAX_WORD_LEN) != NULL)
{
count++;
}
return count;
}
+10 -1
View File
@@ -40,7 +40,13 @@ 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 (placedWords != wordCount)
{
printf("Error: Some words couldn't be placed!");
return EXIT_FAILURE;
}
startGame(wordSalad, SALAD_SIZE, words, wordCount, 500);
}
else
{
@@ -51,4 +57,7 @@ int main(int argc, char *argv[])
}
return exitCode;
}
}
hallii halloo probee