Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8555ca8fb5 | ||
|
|
b7089376a1 | ||
|
|
a0523f0ad3 | ||
|
|
e3658be8a3 | ||
|
|
573bc294a3 | ||
|
|
c69143e2e4 | ||
|
|
53388be6b1 | ||
|
|
ba4be788da | ||
|
|
acb24d4314 |
@ -1,23 +1,162 @@
|
||||
#include "game.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "game.h" // Enthält Definitionen wie MAX_SEARCH_FIELD_LEN und MAX_WORD_LEN
|
||||
#include <time.h> // Für Zeitfunktionen, z. B. zur Initialisierung von Zufallszahlen
|
||||
#include <stdlib.h> // Für rand(), malloc(), etc.
|
||||
#include <string.h> // Für Stringfunktionen wie strnlen()
|
||||
#include <ctype.h> // Für toupper(), um Buchstaben in Großbuchstaben umzuwandeln
|
||||
#include <stdio.h> // Für printf(), um das Spielfeld auszugeben
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
#define MAX_RAND_TRIES_PER_WORD 10 // Maximale Anzahl zufälliger Platzierungsversuche pro Wort
|
||||
#define EMPTY_CHAR 0 // Kennzeichnung für ein leeres Feld im Spielfeld
|
||||
|
||||
//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)
|
||||
// Prüft, ob ein Wort an Position (x,y) in gegebener Richtung ins Spielfeld passt
|
||||
// dir = 0 → horizontal; dir = 1 → vertikal
|
||||
static int canPlaceAt(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int searchFieldLen,
|
||||
unsigned int x, unsigned int y,
|
||||
const char *word, unsigned int wordLen,
|
||||
int dir)
|
||||
{
|
||||
if (dir == 0) { // horizontal
|
||||
// Prüfen, ob das Wort über den rechten Rand hinausgeht
|
||||
if (x + wordLen > searchFieldLen) return 0;
|
||||
|
||||
// Prüfen, ob jedes Zeichen entweder leer ist oder bereits passt
|
||||
for (unsigned int k = 0; k < wordLen; ++k) {
|
||||
char c = (char)toupper((unsigned char)word[k]); // Zeichen in Großbuchstaben
|
||||
char cell = salad[y][x + k]; // Aktuelles Feld
|
||||
if (cell != EMPTY_CHAR && cell != c) return 0; // Konflikt → nicht platzierbar
|
||||
}
|
||||
} else { // vertikal
|
||||
// Prüfen, ob das Wort über den unteren Rand hinausgeht
|
||||
if (y + wordLen > searchFieldLen) return 0;
|
||||
|
||||
for (unsigned int k = 0; k < wordLen; ++k) {
|
||||
char c = (char)toupper((unsigned char)word[k]);
|
||||
char cell = salad[y + k][x];
|
||||
if (cell != EMPTY_CHAR && cell != c) return 0;
|
||||
}
|
||||
}
|
||||
return 1; // Wort kann platziert werden
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
// Trägt ein Wort an Position (x,y) in gegebener Richtung ins Spielfeld ein
|
||||
static void placeAt(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int x, unsigned int y,
|
||||
const char *word, unsigned int wordLen,
|
||||
int dir)
|
||||
{
|
||||
|
||||
if (dir == 0) { // horizontal
|
||||
for (unsigned int k = 0; k < wordLen; ++k) {
|
||||
salad[y][x + k] = (char)toupper((unsigned char)word[k]);
|
||||
}
|
||||
} else { // vertikal
|
||||
for (unsigned int k = 0; k < wordLen; ++k) {
|
||||
salad[y + k][x] = (char)toupper((unsigned char)word[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Erstellt das Wortsuchfeld, platziert Wörter und füllt leere Felder mit Zufallsbuchstaben
|
||||
// Rückgabewert: Anzahl der erfolgreich platzierten Wörter
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int searchFieldLen,
|
||||
const char words[][MAX_WORD_LEN],
|
||||
unsigned int wordCount)
|
||||
{
|
||||
// Ungültige Feldgröße → nichts tun
|
||||
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialisiere das Spielfeld mit EMPTY_CHAR
|
||||
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
salad[r][c] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
int placedCount = 0; // Zähler für erfolgreich platzierte Wörter
|
||||
|
||||
// Versuche, jedes Wort zu platzieren
|
||||
for (unsigned int i = 0; i < wordCount; ++i) {
|
||||
const char *word = words[i];
|
||||
if (!word) continue;
|
||||
|
||||
size_t rawLen = strnlen(word, MAX_WORD_LEN); // Wortlänge ermitteln
|
||||
if (rawLen == 0) continue;
|
||||
|
||||
unsigned int wordLen = (unsigned int)rawLen;
|
||||
|
||||
// Wenn das Wort zu lang ist, überspringen
|
||||
if (wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int placed = 0; // Flag, ob das Wort platziert wurde
|
||||
|
||||
// Zufällige Platzierungsversuche
|
||||
for (int t = 0; t < MAX_RAND_TRIES_PER_WORD && !placed; ++t) {
|
||||
int dir = rand() % 2; // Richtung zufällig wählen: 0 = horizontal, 1 = vertikal
|
||||
|
||||
// Berechnung der maximalen Startpositionen
|
||||
unsigned int maxX = (dir == 0) ? (searchFieldLen - wordLen) : (searchFieldLen - 1);
|
||||
unsigned int maxY = (dir == 1) ? (searchFieldLen - wordLen) : (searchFieldLen - 1);
|
||||
|
||||
// Zufällige Startposition innerhalb der Grenzen
|
||||
unsigned int x = (maxX > 0) ? (unsigned int)(rand() % (maxX + 1)) : 0;
|
||||
unsigned int y = (maxY > 0) ? (unsigned int)(rand() % (maxY + 1)) : 0;
|
||||
|
||||
// Prüfen und ggf. platzieren
|
||||
if (canPlaceAt(salad, searchFieldLen, x, y, word, wordLen, dir)) {
|
||||
placeAt(salad, x, y, word, wordLen, dir);
|
||||
placed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: systematische Suche, falls Zufall nicht funktioniert hat
|
||||
if (!placed) {
|
||||
for (int dir = 0; dir < 2 && !placed; ++dir) {
|
||||
unsigned int limitX = (dir == 0) ? (searchFieldLen - wordLen + 1) : searchFieldLen;
|
||||
unsigned int limitY = (dir == 1) ? (searchFieldLen - wordLen + 1) : searchFieldLen;
|
||||
|
||||
for (unsigned int y = 0; y < limitY && !placed; ++y) {
|
||||
for (unsigned int x = 0; x < limitX && !placed; ++x) {
|
||||
if (canPlaceAt(salad, searchFieldLen, x, y, word, wordLen, dir)) {
|
||||
placeAt(salad, x, y, word, wordLen, dir);
|
||||
placed = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (placed) {
|
||||
placedCount++; // Erfolgreich platziertes Wort zählen
|
||||
}
|
||||
// Nicht platzierbare Wörter werden ignoriert
|
||||
}
|
||||
|
||||
// Fülle alle verbleibenden Felder mit zufälligen Buchstaben
|
||||
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
if (salad[r][c] == EMPTY_CHAR) {
|
||||
salad[r][c] = (char)('A' + (rand() % 26)); // Zufallsbuchstabe A–Z
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedCount; // Anzahl der platzierten Wörter zurückgeben
|
||||
}
|
||||
|
||||
// Gibt das fertige Wortsuchfeld auf der Konsole aus
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||
unsigned int searchFieldLen)
|
||||
{
|
||||
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
char ch = salad[r][c]; // Zeichen aus dem Feld
|
||||
printf("%c ", ch); // Ausgabe mit Leerzeichen
|
||||
}
|
||||
printf("\n"); // Zeilenumbruch nach jeder Reihe
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
@ -5,8 +6,35 @@
|
||||
// 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;
|
||||
// größerer Zeilenpuffer, falls mehrere Wörter/Delimiter in einer Zeile stehen
|
||||
char line[4 * MAX_WORD_LEN];
|
||||
const char *DELIMS = " ,;:.-_!?\t\r\n()[]{}\"'/\\";
|
||||
|
||||
while (count < maxWordCount && fgets(line, sizeof(line), file)) {
|
||||
// Zeilenende entfernen
|
||||
line[strcspn(line, "\r\n")] = '\0';
|
||||
|
||||
}
|
||||
// Tokenisieren nach Delimitern (auch wenn keine Spaces dazwischen sind)
|
||||
for (char *tok = strtok(line, DELIMS);
|
||||
tok && count < maxWordCount;
|
||||
tok = strtok(NULL, DELIMS))
|
||||
{
|
||||
// Uppercase (ASCII-sicher)
|
||||
for (char *p = tok; *p; ++p)
|
||||
*p = (char)toupper((unsigned char)*p);
|
||||
|
||||
// Sicher kopieren + nullterminieren
|
||||
strncpy(words[count], tok, MAX_WORD_LEN - 1);
|
||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
//Test Commit Änderung
|
||||
//Gittest2
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "input.h"
|
||||
@ -38,8 +41,18 @@ int main(int argc, char *argv[])
|
||||
|
||||
// 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)
|
||||
{
|
||||
// Start the graphical game
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Error message if some words couldn't be placed
|
||||
fprintf(stderr, "Only %u out of %u words could be placed in the word salad.\n", placedWords, wordCount);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
@ -9,14 +9,14 @@ unityfolder = ./unity
|
||||
# --------------------------
|
||||
# initiales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
#wordsalad_myversion:
|
||||
# $(CC) -o wordsalad_myversion $(BINARIES)/libwordsalad.a $(BINARIES)
|
||||
#
|
||||
# --------------------------
|
||||
# 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)
|
||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
main.o: main.c
|
||||
$(CC) -c $(CFLAGS) main.c
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user