Dateien nach "/" hochladen

This commit is contained in:
Andreas Deitche 2026-03-30 16:06:29 +00:00
parent ab10d16d9e
commit 48c4ae5f61
5 changed files with 288 additions and 0 deletions

11
input.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INPUT_H
#define INPUT_H
#include <stdio.h>
#define MAX_WORD_LEN 100
#define MAX_LINE_LEN 1024
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
#endif

54
main.c Normal file
View File

@ -0,0 +1,54 @@
#include <stdlib.h>
#include <stdio.h>
#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 <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)
{
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;
}

45
makefile Normal file
View File

@ -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

173
unit_tests.c Normal file
View File

@ -0,0 +1,173 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
}

5
words.txt Normal file
View File

@ -0,0 +1,5 @@
Yeti,Nessie Werwolf; Vampir
Monster
Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger