generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
No commits in common. "main" and "main" have entirely different histories.
@ -3,110 +3,21 @@
|
||||
#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
|
||||
|
||||
// Richtungen für das Platzieren der Wörter
|
||||
typedef enum { HORIZONTAL = 0, VERTICAL = 1 } Direction;
|
||||
//TODO: Spiellogik implementieren:
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
// Gibt einen zufälligen Großbuchstaben A–Z zurück
|
||||
static char randomLetter(void) {
|
||||
return 'A' + (rand() % 26);
|
||||
}
|
||||
|
||||
// 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)
|
||||
// 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)
|
||||
{
|
||||
srand((unsigned)time(NULL)); // Zufallsstartwert setzen
|
||||
|
||||
// 1) Feld "leer"
|
||||
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)
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
for (unsigned int r = 0; r < fieldSize; r++) {
|
||||
for (unsigned int c = 0; c < fieldSize; c++) {
|
||||
printf("%c ", field[r][c]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@ -2,38 +2,11 @@
|
||||
#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;
|
||||
}
|
||||
Binary file not shown.
@ -5,7 +5,7 @@
|
||||
#include "graphicalGame.h"
|
||||
|
||||
#define MAX_NUMBER_OF_WORDS 100
|
||||
#define SALAD_SIZE 18
|
||||
#define SALAD_SIZE 20
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -36,26 +36,10 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
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
|
||||
startGame((const char (*)[MAX_SEARCH_FIELD_LEN])wordSalad,
|
||||
SALAD_SIZE,
|
||||
words,
|
||||
wordCount,
|
||||
1000);
|
||||
#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;
|
||||
}
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
Binary file not shown.
@ -1,48 +1,43 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
BINARIES = ./windows
|
||||
|
||||
raylib_folder = ./raylib
|
||||
unityfolder = ./unity
|
||||
BINARIES = ./windows
|
||||
|
||||
CFLAGS = -g -Wall -I$(raylib_folder) -DSTART_GRAPHICS
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
# initiales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_initial: $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a
|
||||
$(CC) -o $@ $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
all: wordsalad
|
||||
|
||||
wordsalad: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o $@ main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
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) $(CFLAGS) -c main.c -o main.o
|
||||
$(CC) -c $(CFLAGS) main.c
|
||||
|
||||
input.o: input.c
|
||||
$(CC) $(CFLAGS) -c input.c -o input.o
|
||||
$(CC) -c $(CFLAGS)input.c
|
||||
|
||||
game.o: game.c
|
||||
$(CC) $(CFLAGS) -c game.c -o game.o
|
||||
$(CC) -c $(CFLAGS) game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) $(CFLAGS) -c graphicalGame.c -o graphicalGame.o
|
||||
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
TEST_BIN = runTests
|
||||
|
||||
unit_tests.o: unit_tests.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -c unit_tests.c -o unit_tests.o
|
||||
|
||||
test: input.o game.o unit_tests.o $(BINARIES)/libunity.a
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.o $(BINARIES)/libunity.a
|
||||
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
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user