Compare commits

..
17 Commits
5 changed files with 94 additions and 24 deletions
+15
View File
@@ -29,6 +29,8 @@ int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], shor
// 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)
{
unsigned int placedCount = 0;
for(short i = 0; i < searchFieldLen; i++)
{
for(short j = 0; j < searchFieldLen; j++)
@@ -72,9 +74,22 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
salad[x+xoffset][y+yoffset]=words[i-1][k];
}
placedCount++;
}
for(short i = 0; i < searchFieldLen; i++)
{
for(short j = 0; j < searchFieldLen; j++)
{
if(salad[i][j] == '#')
{
salad[i][j]='A' + (rand()%26);
}
}
}
return placedCount;
}
+49 -14
View File
@@ -6,24 +6,59 @@
// 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 isValidWord(const char *word)
{
for (int i = 0;word[i]; i++)
{
if (!('A' <= word[i] && word[i] <= 'Z'))
{
return 0;
}
}
return 1;
}
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
//Erstellt Pointer auf textdokument
FILE *textdokument = fopen(file, "r");
if (textdokument == NULL) {
printf("Datei konnte nicht geoffnet werden");
}
// 2D char Array um wörter zu speichern
char word[MAX_WORD_LEN];
char line[1024]; // Puffer für eine Zeile
unsigned int count = 0;
while (fscanf(textdokument, "%s", word) != EOF && count < maxWordCount) {
// Kopiere das gelesene Wort in das 2D-Array
strncpy(words[count], word, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // Sicherstellen, dass der String nullterminiert ist
// Alle gewünschten Trennzeichen
const char *delimiters = " \t\n,.;:!?\"'()[]{}";
// Zeile für Zeile lesen
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) {
// Tokenize Zeile nach Trennzeichen
char *token = strtok(line, delimiters);
while (token != NULL && count < maxWordCount)
{
// Überspringe leere Tokens
if (token && strlen(token) > 0)
{
// Alles in Großbuchstaben umwandeln
for (int i = 0; token[i]; i++)
{
token[i] = toupper(token[i]);
}
// Prüfen, ob das Wort nur A-Z enthält
if (!isValidWord(token))
{
fprintf(stderr, "Ungültiges Wort gefunden: '%s'. Nur Buchstaben A-Z erlaubt.\n", token);
token = strtok(NULL, delimiters);
continue; // Wort ignorieren
}
// Wort ins Array kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
}
fclose(textdokument);
return count;
token = strtok(NULL, delimiters);
}
}
return count; // Datei nicht schließen – gehört dem Aufrufer
}
+1
View File
@@ -7,5 +7,6 @@
#define MAX_LINE_LEN 1024
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
int isValidWord(const char *word);
#endif
+18 -3
View File
@@ -9,7 +9,8 @@
int main(int argc, char *argv[])
{
/*char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
/*
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
char words[MAX_NUMBER_OF_WORDS][100] =
{
"TESTN",
@@ -22,10 +23,11 @@ int main(int argc, char *argv[])
"SEE",
};
int wordCount = 8;
createWordSalad(salad,5, words,wordCount);
showWordSalad(salad,5);
createWordSalad(salad,7, words,wordCount);
showWordSalad(salad,7);
*/
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
@@ -57,6 +59,18 @@ 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)
{
// Start the game with the created word salad
printf("Squares: %u, Words: %u\n", SALAD_SIZE, placedWords);
unsigned int windowWidth = 800;
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
}
else
{
fprintf(stderr, "Could not place all words into the word salad. Placed %u out of %u words.\n", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
else
@@ -70,3 +84,4 @@ int main(int argc, char *argv[])
return exitCode;
}
+7 -3
View File
@@ -35,11 +35,15 @@ graphicalGame.o: graphicalGame.c
# --------------------------
TEST_BIN = runTests
test: input.o game.o unit_tests.c
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
#test: input.o game.o unit_tests.c
# $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
test: input.o game.o unit_tests.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(unityfolder)/unity.c
# --------------------------
# Clean
# --------------------------
#clean:
# del /f *.o *.exe
clean:
del /f *.o *.exe
rm -f *.o *.exe