forked from freudenreichan/info2Praktikum-Wortsalat
Notizen bei gam.c hinzufügen
This commit is contained in:
1 parent
8c747c61fa
commit
61193c20ab
1 file changed
+37
-25
+37
-25
@@ -7,23 +7,31 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
typedef enum
|
||||||
|
{
|
||||||
|
HORIZONTAL,
|
||||||
|
VERTICAL
|
||||||
|
} Direction;
|
||||||
|
|
||||||
// Versucht, ein einzelnes Wort an einer zufälligen Position zu platzieren; gibt 1 bei Erfolg zurück, sonst 0
|
// Versucht, ein einzelnes Wort an einer zufaelligen Position zu platzieren; gibt 1 bei Erfolg zurück, sonst 0
|
||||||
static int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
|
static int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word)
|
||||||
{
|
{
|
||||||
|
// Laenge des Wortes bestimmen
|
||||||
unsigned int wordLen = strlen(word);
|
unsigned int wordLen = strlen(word);
|
||||||
|
|
||||||
if(wordLen == 0 || wordLen > searchFieldLen)
|
// Leere Wörter oder Wörter die zu lang sind, werden uebersprungen (es wird 0 ausgegeben)
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for(int try = 0; try < MAX_RAND_TRIES_PER_WORD; try++)
|
// Wenn das Spielfeld voll ist, soll es nicht endlos weiterprobiert werden
|
||||||
|
for (int try = 0; try < MAX_RAND_TRIES_PER_WORD; try++)
|
||||||
{
|
{
|
||||||
|
// Es wird zufällig entschieden, ob das Wort waagerecht oder senkrecht geschrieben wird
|
||||||
Direction dir = rand() % 2;
|
Direction dir = rand() % 2;
|
||||||
unsigned int row, col;
|
unsigned int row, col;
|
||||||
|
|
||||||
// Startposition so wählen, dass das Wort ins Spielfeld passt
|
// Startposition so wählen, dass das Wort ins Spielfeld passt
|
||||||
if(dir == HORIZONTAL)
|
if (dir == HORIZONTAL)
|
||||||
{
|
{
|
||||||
row = rand() % searchFieldLen;
|
row = rand() % searchFieldLen;
|
||||||
col = rand() % (searchFieldLen - wordLen + 1);
|
col = rand() % (searchFieldLen - wordLen + 1);
|
||||||
@@ -34,21 +42,23 @@ static int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], uns
|
|||||||
col = rand() % searchFieldLen;
|
col = rand() % searchFieldLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prüfen, ob alle benötigten Felder noch leer sind
|
// Pruefen, ob alle benoetigten Felder noch leer sind
|
||||||
int isFree = 1;
|
int isFree = 1;
|
||||||
for(unsigned int i = 0; i < wordLen; i++)
|
|
||||||
|
for (unsigned int i = 0; i < wordLen; i++)
|
||||||
{
|
{
|
||||||
if(dir == HORIZONTAL && salad[row][col + i] != EMPTY_CHAR)
|
if (dir == HORIZONTAL && salad[row][col + i] != EMPTY_CHAR)
|
||||||
isFree = 0;
|
isFree = 0;
|
||||||
if(dir == VERTICAL && salad[row + i][col] != EMPTY_CHAR)
|
if (dir == VERTICAL && salad[row + i][col] != EMPTY_CHAR)
|
||||||
isFree = 0;
|
isFree = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isFree)
|
if (isFree)
|
||||||
{
|
{
|
||||||
for(unsigned int i = 0; i < wordLen; i++)
|
// Erst nach Pruefung das Wort vollständig eintragen
|
||||||
|
for (unsigned int i = 0; i < wordLen; i++)
|
||||||
{
|
{
|
||||||
if(dir == HORIZONTAL)
|
if (dir == HORIZONTAL)
|
||||||
salad[row][col + i] = toupper(word[i]);
|
salad[row][col + i] = toupper(word[i]);
|
||||||
else
|
else
|
||||||
salad[row + i][col] = toupper(word[i]);
|
salad[row + i][col] = toupper(word[i]);
|
||||||
@@ -57,36 +67,38 @@ static int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], uns
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wenn jeder Versuch fehlgeschloagen ist, wird 0 zurueckgegeben
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erstellt den Wortsalat: platziert die Wörter zufällig und füllt leere Felder auf
|
// Erstellt den Wortsalat: platziert die Woerter zufaellig und fuellt leere Felder auf
|
||||||
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 createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
static int isSeeded = 0;
|
static int isSeeded = 0; // Einmaliger Beginn des Zufallsgenerators gefordert
|
||||||
int placedWords = 0;
|
int placedWords = 0; // Anzahl erfogreicher Woerter
|
||||||
|
|
||||||
// Zufallszahlengenerator nur einmal initialisieren
|
// Zufallszahlengenerator nur einmal initialisieren
|
||||||
if(!isSeeded)
|
if (!isSeeded)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
isSeeded = 1;
|
isSeeded = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alle Felder als leer markieren
|
// Alle Felder als leer markieren
|
||||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
salad[i][j] = EMPTY_CHAR;
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
|
||||||
// Wörter platzieren
|
// Woerter platzieren, erfolgreiche Woerter zaehlen
|
||||||
for(unsigned int i = 0; i < wordCount; i++)
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
placedWords += placeWord(salad, searchFieldLen, words[i]);
|
placedWords += placeWord(salad, searchFieldLen, words[i]);
|
||||||
|
|
||||||
// Restliche Felder mit zufälligen Buchstaben füllen
|
// Restliche (leere) Felder mit zufaelligen Buchstaben fuellen
|
||||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
if(salad[i][j] == EMPTY_CHAR)
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
salad[i][j] = 'A' + rand() % 26;
|
salad[i][j] = 'A' + rand() % 26;
|
||||||
|
|
||||||
|
// Anzahl der erfolgreichen Woerter zurueckgeben
|
||||||
return placedWords;
|
return placedWords;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user