Compare commits

..

8 Commits
main ... main

Author SHA1 Message Date
f637b84844 Alles fertig babakrass 2025-11-04 15:05:03 +01:00
yannick
ad5a4de563 alles bis auf makefile 2025-11-03 21:52:37 +01:00
yannick
5e77016df3 test 2025-10-30 15:17:40 +01:00
1f3236c18b input.c fertig 2025-10-28 14:43:46 +01:00
0feb82646e Wieder weg 2025-10-21 14:25:14 +02:00
yannick
cb9246caa5 test 2025-10-16 14:15:55 +02:00
5a730de455 Wieder weg 2025-10-14 15:24:29 +02:00
0b01ae2039 Test 2025-10-14 15:05:39 +02:00
14 changed files with 163 additions and 130 deletions

Binary file not shown.

View File

@ -1,9 +1,9 @@
CC = gcc
CFLAGS = -g -Wall
CFLAGS = -g -Wall -I$(raylibfolder)
LDFLAGS = -lGL -lX11 -lm
BINARIES = ./linux
raylibfolder = ./raylib
raylib_folder = ./raylib
unityfolder = ./unity
# --------------------------
@ -28,7 +28,7 @@ game.o: game.c
$(CC) $(CFLAGS) -c game.c
graphicalGame.o: graphicalGame.c
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
$(CC) $(CFLAGS) -c graphicalGame.c
# --------------------------
# Unit Tests

View File

@ -101,37 +101,7 @@ 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 ----------
// Hier Setup- und TearDown-Funktionen definieren,
// falls Vor- und Nachbereitungen für die Tests benötigt.
@ -164,10 +134,9 @@ int main(void) {
RUN_TEST(test_readWords_empty_file);
RUN_TEST(test_createWordSalad_all_fit);
RUN_TEST(test_createWordSalad_too_small);
RUN_TEST(test_createWordSalad_allWordsPlaced);
int result = UNITY_END(); // Test-Ergebnisse
print_test_result(result);
return result;
}
}

View File

@ -4,7 +4,7 @@ LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -f
ARCH := $(shell uname -m)
BINARIES = ./macos-$(ARCH)
raylibfolder = ./raylib
raylib_folder = ./raylib
unityfolder = ./unity
# --------------------------
@ -43,4 +43,4 @@ test: input.o game.o unit_tests.c $(BINARIES)/libunity.a
# Clean
# --------------------------
clean:
rm -f *.o wordsalad $(TEST_BIN)
rm -f *.o wordsalad

View File

@ -101,35 +101,6 @@ 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 ----------
// Hier Setup- und TearDown-Funktionen definieren,
@ -164,7 +135,6 @@ int main(void) {
RUN_TEST(test_readWords_empty_file);
RUN_TEST(test_createWordSalad_all_fit);
RUN_TEST(test_createWordSalad_too_small);
RUN_TEST(test_createWordSalad_allWordsPlaced);
int result = UNITY_END(); // Test-Ergebnisse
print_test_result(result);

View File

@ -6,6 +6,7 @@
#define MAX_RAND_TRIES_PER_WORD 10
#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 */
@ -13,11 +14,81 @@
// 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)
{
srand((unsigned int)time(NULL));
// Spielfeld leeren
for (unsigned int i = 0; i < searchFieldLen; i++)
for (unsigned int j = 0; j < searchFieldLen; j++)
salad[i][j] = EMPTY_CHAR;
int placedCount = 0;
for (unsigned int w = 0; w < wordCount; w++) {
const char *word = words[w];
unsigned int len = strlen(word);
int placed = 0;
if (len > searchFieldLen)
continue; // Wort passt niemals ins Feld
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD * searchFieldLen && !placed; tries++) {
int horizontal = rand() % 2;
unsigned int row = rand() % searchFieldLen;
unsigned int col = rand() % searchFieldLen;
// Stelle sicher, dass Wort im Spielfeld bleibt
if (horizontal) {
if (col + len > searchFieldLen) continue;
} else {
if (row + len > searchFieldLen) continue;
}
// Prüfen, ob Platz frei ist oder gleiche Buchstaben überlappen
int fits = 1;
for (unsigned int i = 0; i < len; i++) {
char c = horizontal ? salad[row][col + i] : salad[row + i][col];
if (c != EMPTY_CHAR && c != word[i]) {
fits = 0;
break;
}
}
if (!fits) continue;
// Wort einsetzen
for (unsigned int i = 0; i < len; i++) {
if (horizontal)
salad[row][col + i] = word[i];
else
salad[row + i][col] = word[i];
}
placed = 1;
placedCount++;
}
if (!placed) {
fprintf(stderr, "WARNUNG: Wort \"%s\" konnte nicht platziert werden.\n", word);
}
}
// Leere Felder auffüllen
for (unsigned int i = 0; i < searchFieldLen; i++)
for (unsigned int j = 0; j < searchFieldLen; j++)
if (salad[i][j] == EMPTY_CHAR)
salad[i][j] = 'A' + (rand() % 26);
return placedCount;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
printf("%c ", salad[i][j]);
}
printf("\n");
}
}

View File

@ -8,5 +8,32 @@
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
if (file == NULL)
return 0;
unsigned int count = 0;
char line[512];
// Lese Datei zeilenweise
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
{
// Zerlege die Zeile in Tokens (Wörter), getrennt durch gängige Delimiter
char *token = strtok(line, " ,;:.!?\"\n\r\t");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i]; i++)
token[i] = (char)toupper((unsigned char)token[i]);
// Wort kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
token = strtok(NULL, " ,;:.!?\"\n\r\t");
}
}
return count;
}

View File

@ -6,49 +6,66 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_SIZE 800 // Fenstergröße für das grafische Spiel
int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
// Check if the correct number of arguments is provided
if(argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r");
if(file != NULL)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
FILE *file = fopen(argv[1], "r");
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
if(file != NULL)
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// Check if all words were successfully placed
if (placedWords == wordCount)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
printf("All %u words successfully placed!\n\n", placedWords);
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
// Show the generated word salad on console
showWordSalad(wordSalad, SALAD_SIZE);
// Start the graphical game (raylib-basierte Version)
printf("\nStarting graphical word search game ...\n");
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_SIZE);
}
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
fprintf(stderr,
"Error: Only %u of %u words could be placed.\n"
"Please reduce the number of words or increase the grid size.\n",
placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
exitCode = EXIT_FAILURE;
}
}
return exitCode;
}
return exitCode;
}

View File

@ -1,9 +1,9 @@
CC = gcc
CFLAGS = -g -Wall
CFLAGS = -g -Wall -I$(raylib_folder)
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows
raylibfolder = ./raylib
raylib_folder = ./raylib
unityfolder = ./unity
# --------------------------
@ -12,6 +12,15 @@ unityfolder = ./unity
wordsalad_initial:
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
# ------------------------------
# Eigene Version mit libwordsalad.a
# ------------------------------
wordsalad_myversion:
$(CC) -o wordsalad_myversion main.o game.o input.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
# .o Datein -> kompilierte .c Datein (Objektdateien)
#libraylib.a -> Graphik Bibliothek
# --------------------------
# Normales Spiel bauen
# --------------------------
@ -28,7 +37,7 @@ game.o: game.c
$(CC) -c $(CFLAGS) game.c
graphicalGame.o: graphicalGame.c
$(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
# --------------------------
# Unit Tests

View File

@ -101,35 +101,6 @@ 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 ----------
// Hier Setup- und TearDown-Funktionen definieren,
@ -164,7 +135,6 @@ int main(void) {
RUN_TEST(test_readWords_empty_file);
RUN_TEST(test_createWordSalad_all_fit);
RUN_TEST(test_createWordSalad_too_small);
RUN_TEST(test_createWordSalad_allWordsPlaced);
int result = UNITY_END(); // Test-Ergebnisse
print_test_result(result);