Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
22996a3363
@ -6,14 +6,91 @@
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
//TODO: Spiellogik implementieren:
|
||||
//TODO: Spiellogik implementieren: (erledigt von Sara)
|
||||
/* * 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)
|
||||
{
|
||||
// Zufall initialisieren
|
||||
srand(time(NULL));
|
||||
|
||||
// 1. Spielfeld leeren
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Wörter platzieren
|
||||
for (unsigned int w = 0; w < wordCount; w++)
|
||||
{
|
||||
const char *word = words[w];
|
||||
unsigned int wordLen = strlen(word);
|
||||
|
||||
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
|
||||
{
|
||||
// zufällige Startposition
|
||||
int row = rand() % searchFieldLen;
|
||||
int col = rand() % searchFieldLen;
|
||||
|
||||
// zufällige Richtung: 0 = horizontal, 1 = vertikal
|
||||
int direction = rand() % 2;
|
||||
|
||||
int canPlace = 1;
|
||||
|
||||
// Nur prüfen, ob Buchstaben passen (gleiche erlaubt, leer erlaubt)
|
||||
for (unsigned int k = 0; k < wordLen; k++)
|
||||
{
|
||||
int r = row + (direction == 1 ? k : 0);
|
||||
int c = col + (direction == 0 ? k : 0);
|
||||
|
||||
// Wenn außerhalb des Feldes -> abbrechen
|
||||
if (r >= (int)searchFieldLen || c >= (int)searchFieldLen)
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
// Wenn belegt mit anderem Buchstaben -> abbrechen
|
||||
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
|
||||
{
|
||||
canPlace = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn möglich, das Wort setzen
|
||||
if (canPlace)
|
||||
{
|
||||
for (unsigned int k = 0; k < wordLen; k++)
|
||||
{
|
||||
int r = row + (direction == 1 ? k : 0);
|
||||
int c = col + (direction == 0 ? k : 0);
|
||||
if (r < (int)searchFieldLen && c < (int)searchFieldLen)
|
||||
salad[r][c] = word[k];
|
||||
}
|
||||
break; // Wort erfolgreich platziert
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Leere Felder mit Zufallsbuchstaben füllen
|
||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if (salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
|
||||
@ -36,7 +36,7 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// TODO: (erledigt von Niklas)
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
@ -36,11 +36,24 @@ int main(int argc, char *argv[])
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// TODO: (erledigt von Niklas)
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
if (placedWords == wordCount)
|
||||
{
|
||||
printf("All words have been placed successfully.\n");
|
||||
// Start graphical game
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Error: Only %u out of %u words could be placed.\n", placedWords, wordCount);
|
||||
fprintf(stderr, "Try reducing the number or length of words.\n");
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user