Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eaa16ed84c | |||
| 32a6133d18 | |||
| 39e8380916 | |||
| ffaed665b9 | |||
| e20bf82c19 | |||
| 1ed03a3444 | |||
| 6b435cf82d | |||
| 92c0ab2c28 |
Binary file not shown.
@ -1,9 +1,9 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
CFLAGS = -g -Wall
|
||||||
LDFLAGS = -lGL -lX11 -lm
|
LDFLAGS = -lGL -lX11 -lm
|
||||||
BINARIES = ./linux
|
BINARIES = ./linux
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -28,7 +28,7 @@ game.o: game.c
|
|||||||
$(CC) $(CFLAGS) -c game.c
|
$(CC) $(CFLAGS) -c game.c
|
||||||
|
|
||||||
graphicalGame.o: graphicalGame.c
|
graphicalGame.o: graphicalGame.c
|
||||||
$(CC) $(CFLAGS) -c graphicalGame.c
|
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
|
|||||||
@ -101,7 +101,37 @@ 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,
|
||||||
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
||||||
|
|
||||||
@ -134,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);
|
||||||
|
|||||||
@ -4,7 +4,7 @@ LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -f
|
|||||||
ARCH := $(shell uname -m)
|
ARCH := $(shell uname -m)
|
||||||
BINARIES = ./macos-$(ARCH)
|
BINARIES = ./macos-$(ARCH)
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -43,4 +43,4 @@ test: input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
|||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o wordsalad
|
rm -f *.o wordsalad $(TEST_BIN)
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
#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:
|
//TODO: Spiellogik implementieren:
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
@ -14,81 +13,11 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
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
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,32 +8,5 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
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;
|
|
||||||
}
|
}
|
||||||
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
#define WINDOW_SIZE 800 // Fenstergröße für das grafische Spiel
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -37,26 +36,11 @@ else
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
if (placedWords == wordCount)
|
// Start the game if successful
|
||||||
{
|
// error message if some words couldn't be placed
|
||||||
printf("All %u words successfully placed!\n\n", placedWords);
|
|
||||||
|
|
||||||
// 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
|
|
||||||
{
|
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
@ -67,5 +51,4 @@ else
|
|||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,9 +1,9 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -g -Wall -I$(raylib_folder)
|
CFLAGS = -g -Wall
|
||||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||||
BINARIES = ./windows
|
BINARIES = ./windows
|
||||||
|
|
||||||
raylib_folder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -12,15 +12,6 @@ unityfolder = ./unity
|
|||||||
wordsalad_initial:
|
wordsalad_initial:
|
||||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(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
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -37,7 +28,7 @@ game.o: game.c
|
|||||||
$(CC) -c $(CFLAGS) game.c
|
$(CC) -c $(CFLAGS) game.c
|
||||||
|
|
||||||
graphicalGame.o: graphicalGame.c
|
graphicalGame.o: graphicalGame.c
|
||||||
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
|
$(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user