From 48c4ae5f6135c80780dc2ad960adc967093a3ba2 Mon Sep 17 00:00:00 2001 From: Andreas Deitche Date: Mon, 30 Mar 2026 16:06:29 +0000 Subject: [PATCH] Dateien nach "/" hochladen --- input.h | 11 ++++ main.c | 54 ++++++++++++++++ makefile | 45 ++++++++++++++ unit_tests.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++ words.txt | 5 ++ 5 files changed, 288 insertions(+) create mode 100644 input.h create mode 100644 main.c create mode 100644 makefile create mode 100644 unit_tests.c create mode 100644 words.txt diff --git a/input.h b/input.h new file mode 100644 index 0000000..c25b568 --- /dev/null +++ b/input.h @@ -0,0 +1,11 @@ +#ifndef INPUT_H +#define INPUT_H + +#include + +#define MAX_WORD_LEN 100 +#define MAX_LINE_LEN 1024 + +int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount); + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..6f23fca --- /dev/null +++ b/main.c @@ -0,0 +1,54 @@ +#include +#include +#include "input.h" +#include "game.h" +#include "graphicalGame.h" + +#define MAX_NUMBER_OF_WORDS 100 +#define SALAD_SIZE 20 + +int main(int argc, char *argv[]) +{ + int exitCode = EXIT_SUCCESS; + + // Check if the correct number of arguments is provided + if(argc != 2) + { + fprintf(stderr, "Usage: %s \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) + { + unsigned int placedWords = 0; + char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad + + // 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 + + } + 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; +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..bf91da7 --- /dev/null +++ b/makefile @@ -0,0 +1,45 @@ +CC = gcc +CFLAGS = -g -Wall +LDFLAGS = -lopengl32 -lgdi32 -lwinmm +BINARIES = ./windows + +raylibfolder = ./raylib +unityfolder = ./unity + +# -------------------------- +# initiales Spiel bauen +# -------------------------- +wordsalad_initial: + $(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) + +# -------------------------- +# Normales Spiel bauen +# -------------------------- +all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a + $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) + +main.o: main.c + $(CC) -c $(CFLAGS) main.c + +input.o: input.c + $(CC) -c $(CFLAGS) input.c + +game.o: game.c + $(CC) -c $(CFLAGS) game.c + +graphicalGame.o: graphicalGame.c + $(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c + +# -------------------------- +# Unit Tests +# -------------------------- +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 + +# -------------------------- +# Clean +# -------------------------- +clean: + del /f *.o *.exe diff --git a/unit_tests.c b/unit_tests.c new file mode 100644 index 0000000..fc95d91 --- /dev/null +++ b/unit_tests.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include "unity.h" + +#include "input.h" +#include "game.h" + +// ANSI Escape Codes für Farben +// Hinweis: damit das in CLion mit der integrierten Konsole/Output funktioniert, muss man auf die Run-Config gehen und +// das Häkchen bei 'emulate terminal in the output console' setzen +// (s. auch: https://stackoverflow.com/questions/32742850/how-to-show-colored-console-output-in-clion) +#define RESET "\033[0m" +#define GREEN "\033[32m" +#define RED "\033[31m" +#define YELLOW "\033[33m" +#define CYAN "\033[36m" +#define BOLD "\033[1m" + +// Eine Funktion, um die Test-Ergebnisse zu färben +void print_test_result(int result) { + if (result == 0) { + // Test war erfolgreich + printf(GREEN "OK\n" RESET); + } else { + // Test ist fehlgeschlagen + printf(RED "FAILED\n" RESET); + } +} + +// ---------- Tests für input.c ---------- +void test_readWords_simple(void) { + FILE *f = fopen("testwords_simple.txt", "r"); + + char words[10][MAX_WORD_LEN]; + int count = readWords(f, words, 10); + fclose(f); + + TEST_ASSERT_EQUAL_INT(3, count); + TEST_ASSERT_EQUAL_STRING("APFEL", words[0]); + TEST_ASSERT_EQUAL_STRING("BANANE", words[1]); + TEST_ASSERT_EQUAL_STRING("KIWI", words[2]); +} + +void test_readWords_with_delimiters(void) { + FILE *f = fopen("testwords_delims.txt", "r"); + + char words[10][MAX_WORD_LEN]; + int count = readWords(f, words, 10); + fclose(f); + + TEST_ASSERT_EQUAL_INT(3, count); + TEST_ASSERT_EQUAL_STRING("HUND", words[0]); + TEST_ASSERT_EQUAL_STRING("KATZE", words[1]); + TEST_ASSERT_EQUAL_STRING("MAUS", words[2]); +} + +void test_readWords_empty_file(void) { + FILE *f = fopen("testwords_empty.txt", "r"); + + char words[10][MAX_WORD_LEN]; + int count = readWords(f, words, 10); + fclose(f); + + TEST_ASSERT_EQUAL_INT(0, count); +} + +// ---------- Tests für game.c ---------- +void test_createWordSalad_all_fit(void) { + char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"}; + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; + + int placed = createWordSalad(salad, 20, words, 3); + + TEST_ASSERT_EQUAL_INT(3, placed); + + // Sicherstellen, dass alle Felder gefüllt sind + for (int i = 0; i < 20; i++) { + for (int j = 0; j < 20; j++) { + TEST_ASSERT_GREATER_OR_EQUAL('A', salad[i][j]); + TEST_ASSERT_LESS_OR_EQUAL('Z', salad[i][j]); + } + } +} + +void test_createWordSalad_too_small(void) { + char words[2][MAX_WORD_LEN] = {"ELEPHANT", "GIRAFFE"}; + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; + + int placed = createWordSalad(salad, 5, words, 2); + + TEST_ASSERT_GREATER_OR_EQUAL(0, placed); + TEST_ASSERT_LESS_OR_EQUAL(2, placed); + + // Feld muss vollständig gefüllt sein + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + TEST_ASSERT_GREATER_OR_EQUAL('A', salad[i][j]); + TEST_ASSERT_LESS_OR_EQUAL('Z', salad[i][j]); + } + } +} + +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. + +void setUp(void) { + FILE *f = fopen("testwords_delims.txt", "w"); + fprintf(f, "Hund,Katze; Maus\n"); + fclose(f); + + f = fopen("testwords_simple.txt", "w"); + fprintf(f, "Apfel\nBanane\nKiwi"); + fclose(f); + + f = fopen("testwords_empty.txt", "w"); + fclose(f); +} + +void tearDown(void) { + remove("testwords_delims.txt"); + remove("testwords_simple.txt"); + remove("testwords_empty.txt"); +} + +// ---------- Test Runner ---------- + +int main(void) { + UNITY_BEGIN(); + + RUN_TEST(test_readWords_simple); + RUN_TEST(test_readWords_with_delimiters); + 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; +} diff --git a/words.txt b/words.txt new file mode 100644 index 0000000..43612ee --- /dev/null +++ b/words.txt @@ -0,0 +1,5 @@ +Yeti,Nessie Werwolf; Vampir +Monster +Hydra;Frankenstein +Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist +Gespenst, Oger \ No newline at end of file