This commit is contained in:
engelpa103619 committed 2026-10-26 12:32:21 +01:00
1 parent 93db739464
commit 2e24fdcec3
6 files changed
+163 -2

No files matched your search

+108
View File
@@ -6,12 +6,120 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
typedef enum{
HORIZONTAL,
VERTICAL
} Richtung;
//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
// Buchstabengitter Koordinaten Seitenlänge die Wörter, der Buchstabe Anzahl der 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)
{
for(int i = 0; i < searchFieldLen; i++)
{
for(int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
int placeWords = 0;
for(int i = 0; i < wordCount; i++)
{
int Spalte;
int Zeile;
int WORD_LEN = strlen(words[i]); //Buchstaben zählen
int platziert = 0;
for(int try = 0; try < MAX_RAND_TRIES_PER_WORD; try++) // Max 10 Versuche pro Wort
{
Richtung dir = rand() % 2;
int passt = 1;
if(dir == HORIZONTAL)
{
Spalte = rand() % searchFieldLen;
Zeile = rand() % searchFieldLen;
if(Spalte + WORD_LEN <= searchFieldLen) // Prüft auf Border
{
for(int k = 0; k < WORD_LEN; k++)
{
if(salad[Zeile][Spalte+k] != EMPTY_CHAR) // Koalisionsprüfung
{
passt = 0; //Kollision gefunden
break;
}
}
if(passt == 1)
{
for(int s = 0; s < WORD_LEN; s++)
{
salad[Zeile][Spalte + s] = words[i][s]; // Das Wort eintragen in einer Zeile
}
platziert = 1;
break; // Das Wort kann eingetragen werden, keine Kollision
}
}
}
else // Vertikal
{
Spalte = rand() % searchFieldLen;
Zeile = rand() % searchFieldLen;
if(Zeile + WORD_LEN <= searchFieldLen)
{
int passt = 1;
for(int k = 0; k < WORD_LEN; k++)
{
if(salad[Zeile+k][Spalte] != EMPTY_CHAR)
{
passt = 0; //Kollision gefunden
break;
}
}
if(passt == 1)
{
for(int z = 0; z < WORD_LEN; z++)
{
salad[Zeile + z][Spalte] = words[i][z]; // // Das Wort eintragen in einer Spalte
}
platziert = 1;
break; // Das Wort kann eingetragen werden, keine Kollision
}
}
}
}
if(platziert == 1)
{
placeWords++;
}
}
for(int i = 0; i < searchFieldLen; i++)
{
for(int j = 0; j < searchFieldLen; j++)
{
if(salad[i][j] == EMPTY_CHAR) // Nur wenn Feld noch frei ist, dann mit random Buchstaben belegen
{
salad[i][j] = 'A' + (rand() % 26);
}
}
}
return placeWords;
}
+1
View File
@@ -7,4 +7,5 @@
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
#endif
+37
View File
@@ -4,9 +4,46 @@
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
int leseEinWort(FILE *file, char zielWort[])
{
int c;
int i = 0;
while((c = fgetc(file)) != EOF) // liest aus Textdatei und überprüft ob End Of File ist
{
if(isalpha(c) != 0)
{
if(i < MAX_WORD_LEN - 1) // -1 wegen '\0'
{
zielWort[i] = (char)toupper(c); // Für Großbuchstaben
i++;
}
}
else if(i > 0)
{
zielWort[i] = '\0';
return 1;
}
}
if(i > 0) // Datei endet direkt nach ein WOrt ohne folgendes Leerzeichen oder so
{
zielWort[i] = '\0';
return 1;
}
return 0;
}
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int anzahl = 0;
while((leseEinWort(file, words[anzahl])) == 1 && anzahl < maxWordCount)
{
anzahl++;
}
return anzahl;
}
+13
View File
@@ -3,12 +3,16 @@
#include "input.h"
#include "game.h"
#include "graphicalGame.h"
#include <time.h>
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
int main(int argc, char *argv[])
{
srand(time(NULL));
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
@@ -40,6 +44,15 @@ int main(int argc, char *argv[])
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if(placedWords == wordCount) // Wenn alle Wörter reinpassten, rufe das grafische Fenster auf
{
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
else
{
fprintf(stderr, "Could not place all words\n");
exitCode = EXIT_FAILURE;
}
}
else
+2 -1
View File
@@ -21,7 +21,8 @@ codefolder = .
# --------------------------
wordsalad_initial:
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
wordsalad_myversion: main.o game.o graphicalGame.o
$(CC) $(CFLAGS) -o wordsalad_myversion main.o game.o graphicalGame.o -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
# --------------------------
# Normales Spiel bauen
# --------------------------