This commit is contained in:
yannick 2025-10-30 15:17:40 +01:00
parent 1f3236c18b
commit 5e77016df3
2 changed files with 75 additions and 5 deletions

View File

@ -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
//abcdefghijklmnopqrstuvwxyz
//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
@ -15,11 +14,81 @@
// 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");
}
} }

View File

@ -1,9 +1,9 @@
CC = gcc CC = gcc
CFLAGS = -g -Wall CFLAGS = -g -Wall -I$(raylib_folder)
LDFLAGS = -lopengl32 -lgdi32 -lwinmm LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows BINARIES = ./windows
raylibfolder = ./raylib raylib_folder = ./raylib
unityfolder = ./unity unityfolder = ./unity
# -------------------------- # --------------------------
@ -12,6 +12,7 @@ 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)
# -------------------------- # --------------------------
# Normales Spiel bauen # Normales Spiel bauen
# -------------------------- # --------------------------
@ -28,7 +29,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$(raylibfolder) -c $(CFLAGS) graphicalGame.c $(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests