Author SHA1 Message Date
Simon 364dad3ac8 game.c: createWordSalad fertiggestellt (vertikal + Auffuellen) 2026-11-04 20:04:54 +01:00
hopfenu103561 d21c6beda0 game.c 2026-10-29 11:09:16 +01:00
2 changed files with 77 additions and 12 deletions

No files matched your search

+73 -1
View File
@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define MAX_RAND_TRIES_PER_WORD 1000
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
@@ -13,5 +13,77 @@
// 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(time(NULL));
int i,j;
int Woertergesetzt = 0;
for (i=0 ; i<MAX_SEARCH_FIELD_LEN;i++)
{
for (j=0 ; j<MAX_SEARCH_FIELD_LEN;j++)
{
salad[i][j] = EMPTY_CHAR;
}
}
for (unsigned int k = 0 ; k < wordCount ; k++)
{
int richtigplatziert = 0;
unsigned int laenge = strlen(words[k]);
for (int f = 0 ; f < MAX_RAND_TRIES_PER_WORD && !richtigplatziert ; f++)
{
unsigned int ausrichtung, zeile, spalte;
ausrichtung = rand() % 2;
zeile = rand() % (searchFieldLen);
spalte = rand() % (searchFieldLen);
if (ausrichtung == 0) // horizontal
{
if (spalte + laenge <= searchFieldLen)
{
int moeglichePosition = 1;
for (unsigned int i1 = 0; i1 < laenge; i1++)
{
if (salad[zeile][spalte + i1] != EMPTY_CHAR)
moeglichePosition = 0;
}
if (moeglichePosition)
{
for (unsigned int i1 = 0; i1 < laenge; i1++)
salad[zeile][spalte + i1] = words[k][i1];
richtigplatziert = 1;
Woertergesetzt++;
}
}
}
else // vertikal
{
if (zeile + laenge <= searchFieldLen)
{
int moeglichePosition = 1;
for (unsigned int i1 = 0; i1 < laenge; i1++)
{
if (salad[zeile + i1][spalte] != EMPTY_CHAR)
moeglichePosition = 0;
}
if (moeglichePosition)
{
for (unsigned int i1 = 0; i1 < laenge; i1++)
salad[zeile + i1][spalte] = words[k][i1];
richtigplatziert = 1;
Woertergesetzt++;
}
}
}
}
}
// restliche Felder mit zufaelligen Buchstaben fuellen
for (i = 0; i < (int)searchFieldLen; i++)
{
for (j = 0; j < (int)searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR)
salad[i][j] = 'A' + rand() % 26;
}
}
return Woertergesetzt;
}
+4 -11
View File
@@ -36,17 +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 (wordCount == placedWords)
{
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
else
{
fprintf(stderr, "Could only place %u of %u words!\n", placedWords, wordCount);
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