Files
Info2-Aufgaben/Startcode/game.c
T

77 lines
1.9 KiB
C

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//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
// Erstellt den Wortsalat.
// Die Wörter werden zufällig horizontal oder vertikal eingesetzt.
// Freie Felder werden anschließend mit zufälligen Buchstaben gefüllt.
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
/ 5. Vertikale Platzierung prüfen
else {
if (row + wordLen > searchFieldLen) {
continue;
}
for (size_t j = 0; j < wordLen; j++) {
if (salad[row + j][col] != EMPTY_CHAR &&
salad[row + j][col] != words[i][j]) {
canPlace = 0;
break;
}
}
}
// Platzierung nicht möglich
if (!canPlace) {
continue;
}
// 6. Wort tatsächlich platzieren
for (size_t j = 0; j < wordLen; j++) {
if (direction == 0) {
salad[row][col + j] = words[i][j];
}
else {
salad[row + j][col] = words[i][j];
}
}
// Erfolgreich platziertes Wort zählen
placedWords++;
// Nächstes Wort bearbeiten
break;
}
}
// 7. 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;
}
}
}
// 8. Anzahl erfolgreich platzierter Wörter zurückgeben
return (int)placedWords;
}