This commit is contained in:
Philipp Messner 2025-11-11 10:27:57 +01:00
parent 2a921ddfa4
commit f2ee8341d3
10 changed files with 159 additions and 19 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
Start_Mac/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -3,21 +3,110 @@
#include <stdlib.h>
#include <string.h>
// Wie oft versucht wird, ein Wort zufällig zu platzieren
#define MAX_RAND_TRIES_PER_WORD 10
// Kennzeichen für leere Felder (wird später durch Buchstaben ersetzt)
#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 */
// 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)
{
// Richtungen für das Platzieren der Wörter
typedef enum { HORIZONTAL = 0, VERTICAL = 1 } Direction;
// Gibt einen zufälligen Großbuchstaben AZ zurück
static char randomLetter(void) {
return 'A' + (rand() % 26);
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
// Prüft, ob ein Wort an der gegebenen Position und Richtung ins Spielfeld passt
static int canPlace(const char field[][MAX_SEARCH_FIELD_LEN], unsigned int fieldSize,
const char *word, unsigned int row, unsigned int col, Direction dir) {
int length = strlen(word);
if (dir == HORIZONTAL) {
if (col + length > fieldSize) return 0; // würde rechts rausgehen
for (int i = 0; i < length; i++) {
char current = field[row][col + i];
if (current != EMPTY_CHAR && current != word[i])
return 0; // Kollision mit anderem Wort
}
} else { // VERTICAL
if (row + length > fieldSize) return 0; // würde unten rausgehen
for (int i = 0; i < length; i++) {
char current = field[row + i][col];
if (current != EMPTY_CHAR && current != word[i])
return 0;
}
}
return 1; // Platz ist frei
}
// Schreibt ein Wort tatsächlich ins Spielfeld
static void placeWord(char field[][MAX_SEARCH_FIELD_LEN], const char *word,
unsigned int row, unsigned int col, Direction dir) {
int length = strlen(word);
if (dir == HORIZONTAL) {
for (int i = 0; i < length; i++)
field[row][col + i] = word[i];
} else {
for (int i = 0; i < length; i++)
field[row + i][col] = word[i];
}
}
// Hauptfunktion: erstellt das Buchstabenfeld mit den versteckten Wörtern
int createWordSalad(char field[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int fieldSize, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
srand((unsigned)time(NULL)); // Zufallsstartwert setzen
// 1) Feld mit "leer" initialisieren
for (unsigned int r = 0; r < fieldSize; r++) {
for (unsigned int c = 0; c < fieldSize; c++) {
field[r][c] = EMPTY_CHAR;
}
}
// 2) Wörter zufällig platzieren
int placedWords = 0;
for (unsigned int w = 0; w < wordCount; w++) {
const char *word = words[w];
if (!word || word[0] == '\0') continue; // leere Zeilen überspringen
int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
Direction dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
unsigned int row = rand() % fieldSize;
unsigned int col = rand() % fieldSize;
if (canPlace(field, fieldSize, word, row, col, dir)) {
placeWord(field, word, row, col, dir);
placedWords++;
placed = 1;
}
}
}
// 3) Leere Felder mit Zufallsbuchstaben füllen
for (unsigned int r = 0; r < fieldSize; r++) {
for (unsigned int c = 0; c < fieldSize; c++) {
if (field[r][c] == EMPTY_CHAR)
field[r][c] = randomLetter();
}
}
return placedWords;
}
// Gibt das Spielfeld auf der Konsole aus
void showWordSalad(const char field[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int fieldSize)
{
for (unsigned int r = 0; r < fieldSize; r++) {
for (unsigned int c = 0; c < fieldSize; c++) {
printf("%c ", field[r][c]);
}
printf("\n");
}
}

View File

@ -2,11 +2,38 @@
#include <string.h>
#include <ctype.h>
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
unsigned int count = 0; // Anzahl übernommener Wörter
int j = 0; // aktuelle Wortlänge
int ch; // eingelesenes Zeichen
char buf[MAX_WORD_LEN]; // Puffer für ein Wort
while ((ch = fgetc(file)) != EOF) {
unsigned char c = (unsigned char)ch;
if (isalnum(c)) {
// Teil eines Wortes → Uppercase, sicher begrenzt
if (j < (MAX_WORD_LEN - 1)) {
buf[j++] = (char)toupper(c);
}
} else {
// Trennzeichen: Wort abschließen, wenn vorhanden
if (j > 0) {
buf[j] = '\0';
strncpy(words[count], buf, MAX_WORD_LEN);
count++;
j = 0;
if (count >= maxWordCount) break;
}
}
}
// Letztes Wort am Dateiende übernehmen
if (j > 0 && count < maxWordCount) {
buf[j] = '\0';
strncpy(words[count], buf, MAX_WORD_LEN);
count++;
}
return (int)count;
}

View File

@ -36,10 +36,28 @@ int main(int argc, char *argv[])
// 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
if (placedWords == wordCount) {
printf("[OK] %u/%u Wörter platziert.\n", placedWords, wordCount);
showWordSalad(wordSalad, SALAD_SIZE);
// Optional: Grafische Ausgabe starten, falls vorhanden.
// Aktivieren durch -DSTART_GRAPHICS beim Kompilieren
// und wenn in graphicalGame.h eine passende Funktion existiert.
#ifdef START_GRAPHICS
// Beispiel-Signatur bitte ggf. an deine API anpassen:
// void startGraphicalGame(const char field[][MAX_SEARCH_FIELD_LEN], unsigned int size,
// const char words[][MAX_WORD_LEN], unsigned int wordCount);
startGraphicalGame((const char (*)[MAX_SEARCH_FIELD_LEN])wordSalad,
SALAD_SIZE,
(const char (*)[MAX_WORD_LEN])words,
wordCount);
#endif
} else {
fprintf(stderr, "[FEHLER] Nur %u von %u Wörtern konnten platziert werden.\n",
placedWords, wordCount);
fprintf(stderr, "Tipp: Größe des Suchfeldes (SALAD_SIZE) erhöhen oder kürzere Wortliste verwenden.\n");
exitCode = EXIT_FAILURE;
}
}
else

View File

@ -1,5 +1,6 @@
CC = gcc
CFLAGS = -g -Wall
CFLAGS += -DSTART_GRAPHICS
LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -framework IOKit -framework Cocoa -framework CoreVideo
ARCH := $(shell uname -m)
BINARIES = ./macos-$(ARCH)
@ -29,7 +30,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
# --------------------------

BIN
Start_Mac/runTests Executable file

Binary file not shown.

BIN
Start_Mac/wordsalad_initial Executable file

Binary file not shown.

BIN
Start_Windows/.DS_Store vendored Normal file

Binary file not shown.

View File

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