Compare commits

..
5 Commits
Author SHA1 Message Date
wehnerla99053 985dac6d03 Merge remote-tracking branch 'origin/main' 2025-10-27 15:01:10 +01:00
wehnerla99053 ad5fbfb626 finale Änderungen 2025-10-27 14:59:35 +01:00
wehnerla99053 5f5c6cfb1b finale Änderungen 2025-10-27 14:59:17 +01:00
bergerma98875 7b66c00935 Kommentierung game.c angepasst 2025-10-27 07:00:26 +01:00
wehnerla99053 940526fa7e Kommentierungen 2025-10-26 23:20:43 +01:00
8 changed files with 49 additions and 17 deletions
+17 -14
View File
@@ -6,50 +6,50 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
/* * 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 // 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)
{// Länge der searchFieldLen wird in Main unter Salad size festgelegt (20) {
srand((unsigned int)time(NULL)); // Seed für Zufallsgenerator srand((unsigned int)time(NULL)); // Seed für Zufallsgenerator
// Initialisiere das Spielfeld mit EMPTY_CHAR // Initialisiere das Spielfeld
for (unsigned int i = 0; i < searchFieldLen; ++i) for (unsigned int i = 0; i < searchFieldLen; ++i)
{ {
for (unsigned int j = 0; j < searchFieldLen; ++j) for (unsigned int j = 0; j < searchFieldLen; ++j)
{ {
salad[i][j] = ' '; salad[i][j] = EMPTY_CHAR;
} }
} }
unsigned int placedWords = 0; unsigned int placedWords = 0; // Zähler für die Anzahl platzierter Wörter für spätere Überprüfung
for (unsigned int word = 0; word < wordCount; ++word) for (unsigned int word = 0; word < wordCount; ++word)
{ {
int placed = 0; int placed = 0; //Flag um zu kennzechneinen, ob ein Wort platziert worden ist oder nicht
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries) for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries)
{ {
// Festlegen der Parameter zum Platzieren des Worts
int direction = rand() % 2; // 1 = horizontal, 0 = vertical int direction = rand() % 2; // 1 = horizontal, 0 = vertical
int row = rand() % searchFieldLen; int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen; int col = rand() % searchFieldLen;
int len = (int)strlen(words[word]); int len = (int)strlen(words[word]);
if (direction == 1 && col + len <= searchFieldLen) // prüft ob das Wort horizontal in die Tabelle passt // prüfen, ob das Wort horizontal in die Tabelle passt
if (direction == 1 && col + len <= searchFieldLen)
{ {
int canPlace = 1; int canPlace = 1; // Flag um zu kennzeichnen, dass das Wort platziert werden kann
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
{ {
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[word][i]) if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[word][i])
{ {
canPlace = 0; canPlace = 0;
break; break;
} }
} }
// Wenn das Wort platziert werden kann, wird es platziert
if (canPlace) if (canPlace)
{ {
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
@@ -57,11 +57,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
placed = 1; placed = 1;
} }
} }
// prüfen, ob das Wort vertikal in die Tabelle passt
else if (direction == 0 && row + len <= searchFieldLen) // vertical else if (direction == 0 && row + len <= searchFieldLen) // vertical
{ {
int canPlace = 1; int canPlace = 1;// Flag um zu kennzeichnen, dass das Wort platziert werden kann
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
{ {
// Wenn in den Feldern schon andere Buchstaben stehen, kann das Feld nicht platziert werden
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[word][i]) if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[word][i])
{ {
canPlace = 0; canPlace = 0;
@@ -69,6 +71,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
break; break;
} }
} }
// Wenn das Wort platziert werden kann, wird es platziert
if (canPlace) if (canPlace)
{ {
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
@@ -77,7 +80,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
} }
} }
// Zähler hochzählen, wenn ein Wort platziert wurde
if (placed) if (placed)
placedWords++; placedWords++;
} }
@@ -90,7 +93,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
{ {
if (salad[i][j] == EMPTY_CHAR) if (salad[i][j] == EMPTY_CHAR)
{ {
salad[i][j] = 'A' + rand() % 26; salad[i][j] = 'A' + (rand() % 26);
} }
} }
} }
Binary file not shown.
Binary file not shown.
+2 -3
View File
@@ -43,9 +43,8 @@ TEST_BIN = runTests
test: input.o game.o unit_tests.c 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 $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
$(BINARIES)/libwordsalad_complete.a: input.o game.o $(BINARIES)/libwordsalad_complete.a: input.o game.o graphicalGame.o
ar rcs $(BINARIES)/libwordsalad_complete.a input.o game.o ar rcs $(BINARIES)/libwordsalad_complete.a input.o game.o graphicalGame.o
wordsalad_myversion: main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a wordsalad_myversion: main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a
$(CC) $(CFLAGS) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) $(CC) $(CFLAGS) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
Binary file not shown.
+30
View File
@@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
} }
} }
void test_createWordSalad_allWordsPlaced() {
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
int placed = createWordSalad(saladHoriz, 20, words, 3);
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
saladVert[j][i] = saladHoriz[i][j];
}
}
for(int i = 0; i < 3; i++) {
const char* word = words[i];
int wordFound = 0;
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
const char* row = saladHoriz[j];
const char* col = saladVert[j];
wordFound |= strstr(row, word) || strstr(col, word);
}
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
}
TEST_ASSERT_EQUAL_INT(3, placed);
}
// ---------- Test Setup und TearDown Funktionen ---------- // ---------- Test Setup und TearDown Funktionen ----------
// Hier Setup- und TearDown-Funktionen definieren, // Hier Setup- und TearDown-Funktionen definieren,
@@ -135,6 +164,7 @@ int main(void) {
RUN_TEST(test_readWords_empty_file); RUN_TEST(test_readWords_empty_file);
RUN_TEST(test_createWordSalad_all_fit); RUN_TEST(test_createWordSalad_all_fit);
RUN_TEST(test_createWordSalad_too_small); RUN_TEST(test_createWordSalad_too_small);
RUN_TEST(test_createWordSalad_allWordsPlaced);
int result = UNITY_END(); // Test-Ergebnisse int result = UNITY_END(); // Test-Ergebnisse
print_test_result(result); print_test_result(result);
Binary file not shown.
Binary file not shown.