Compare commits

..
3 Commits
4 changed files with 146 additions and 2 deletions

No files matched your search

+68
View File
@@ -10,6 +10,74 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
//Erstellt den Wortsalat
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 anzahlPlatziert = 0;
//1. Feld leeren
for (unsigned int zeile = 0; zeile < searchFieldLen; zeile++)
for (unsigned int spalte = 0; spalte < searchFieldLen; spalte++)
salad[zeile][spalte] = EMPTY_CHAR;
//2. Wörter platzieren
for (unsigned int w = 0; w < wordCount; w++)
{
unsigned int laenge = strlen(words[w]);
if (laenge > searchFieldLen)
continue; //Wort zu lang
for (int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++)
{
int waagrecht = rand() % 2; //1 = horizontal, 0 = vertikal
unsigned int zeile, spalte;
if (waagrecht)
{
zeile = rand() % searchFieldLen;
spalte = rand() % (searchFieldLen - laenge + 1);
}
else if (!waagrecht)
{
zeile = rand() % (searchFieldLen - laenge + 1);
spalte = rand() % searchFieldLen;
}
//Prüfen, ob Platz frei ist
int passt = 1;
for (unsigned int i = 0; i < laenge; i++)
{
char feld = waagrecht ? salad[zeile][spalte + i] : salad[zeile + i][spalte];
if (feld != EMPTY_CHAR && feld != words[w][i])
passt = 0;
}
//Wort eintragen
if (passt)
{
for (unsigned int i = 0; i < laenge; i++)
{
if (waagrecht)
salad[zeile][spalte + i] = words[w][i];
else
salad[zeile + i][spalte] = words[w][i];
}
anzahlPlatziert++;
break;
}
}
}
//3. Leere Felder mit Zufallsbuchstaben füllen
for (unsigned int zeile = 0; zeile < searchFieldLen; zeile++)
for (unsigned int spalte = 0; spalte < searchFieldLen; spalte++)
if (salad[zeile][spalte] == EMPTY_CHAR)
salad[zeile][spalte] = 'A' + rand() % 26;
return anzahlPlatziert;
}
// 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)
{
+61 -2
View File
@@ -4,9 +4,68 @@
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
static char *liesWort(FILE *file, char *puffer, unsigned int pufferGroesse) {
int laenge = 0;
if (file == NULL || puffer == NULL || pufferGroesse == 0) {
return NULL;
}
if (fgets(puffer, (int)pufferGroesse, file) == NULL) {
return NULL;
}
laenge = (int)strlen(puffer);
if (laenge > 0 && puffer[laenge - 1] == '\n') {
puffer[laenge - 1] = '\0';
laenge--;
}
if (laenge > 0 && puffer[laenge - 1] == '\r') {
puffer[laenge - 1] = '\0';
laenge--;
}
return puffer;
}
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount){
int anzahlWoerter = 0;
int i = 0;
char zeilenPuffer[MAX_LINE_LEN];
char *wort = NULL;
const char *trennzeichen = " ,;\t\r\n";
if (file == NULL || words == NULL || maxWordCount == 0) {
return 0;
}
while (anzahlWoerter < (int)maxWordCount && liesWort(file, zeilenPuffer, sizeof(zeilenPuffer)) != NULL) {
wort = strtok(zeilenPuffer, trennzeichen);
while (wort != NULL && anzahlWoerter < (int)maxWordCount) {
while (*wort == ' ' || *wort == '\t') {
wort++;
}
if (*wort == '\0') {
wort = strtok(NULL, trennzeichen);
continue;
}
for (i = 0; wort[i] != '\0'; i++) {
wort[i] = (char)toupper((unsigned char)wort[i]);
}
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN - 1);
words[anzahlWoerter][MAX_WORD_LEN - 1] = '\0';
anzahlWoerter++;
wort = strtok(NULL, trennzeichen);
}
}
return anzahlWoerter;
}
+9
View File
@@ -41,6 +41,15 @@ int main(int argc, char *argv[])
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords == wordCount) {
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
else {
//Fehlermeldung
fprintf(stderr, "Error: Could not place all words (%u of %u placed).\n", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
else
{
+8
View File
@@ -22,6 +22,14 @@ codefolder = .
wordsalad_initial:
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
# --------------------------
# eigenes Spiel bauen
# --------------------------
wordsalad_myversion: main.o
$(CC) -o wordsalad_myversion main.o -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
# --------------------------
# Normales Spiel bauen
# --------------------------