generated from freudenreichan/info2Praktikum-Wortsalat
Implementierung von game.c #2
+117
-6
@@ -6,12 +6,123 @@
|
||||
#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
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||
// 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)
|
||||
{
|
||||
unsigned int placedWords = 0;
|
||||
|
||||
// Check the dimensions
|
||||
if (searchFieldLen == 0 ||
|
||||
searchFieldLen > MAX_SEARCH_FIELD_LEN)
|
||||
return 0;
|
||||
|
||||
// Initialize the field with empty cells
|
||||
for (unsigned int row = 0; row < searchFieldLen; row++)
|
||||
{
|
||||
for (unsigned int col = 0; col < searchFieldLen; col++)
|
||||
{
|
||||
salad[row][col] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// Go through all words
|
||||
for (unsigned int w = 0; w < wordCount; w++)
|
||||
{
|
||||
// Determine the length of the word
|
||||
unsigned int len = 0;
|
||||
|
||||
while (len < MAX_WORD_LEN &&
|
||||
words[w][len] != '\0')
|
||||
{
|
||||
len++;
|
||||
}
|
||||
|
||||
// Skip invalid or too long words
|
||||
if (len == 0 ||
|
||||
len == MAX_WORD_LEN ||
|
||||
len > searchFieldLen)
|
||||
continue;
|
||||
|
||||
// Try up to 10 random positions
|
||||
for (unsigned int attempt = 0;
|
||||
attempt < MAX_RAND_TRIES_PER_WORD;
|
||||
attempt++)
|
||||
{
|
||||
// 0 = horizontal, 1 = vertical
|
||||
int vertical = rand() % 2;
|
||||
|
||||
// Generate a valid starting position
|
||||
unsigned int row =
|
||||
(unsigned int)(rand() %
|
||||
(searchFieldLen -
|
||||
(vertical ? len : 1) + 1));
|
||||
|
||||
unsigned int col =
|
||||
(unsigned int)(rand() %
|
||||
(searchFieldLen -
|
||||
(vertical ? 1 : len) + 1));
|
||||
|
||||
int fits = 1;
|
||||
|
||||
// Check if the word fits
|
||||
for (unsigned int i = 0; i < len; i++)
|
||||
{
|
||||
unsigned int r =
|
||||
row + (vertical ? i : 0);
|
||||
|
||||
unsigned int c =
|
||||
col + (vertical ? 0 : i);
|
||||
|
||||
if (salad[r][c] != EMPTY_CHAR &&
|
||||
salad[r][c] != words[w][i])
|
||||
{
|
||||
fits = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Try another position if necessary
|
||||
if (!fits)
|
||||
continue;
|
||||
|
||||
// Place the word
|
||||
for (unsigned int i = 0; i < len; i++)
|
||||
{
|
||||
unsigned int r =
|
||||
row + (vertical ? i : 0);
|
||||
|
||||
unsigned int c =
|
||||
col + (vertical ? 0 : i);
|
||||
|
||||
salad[r][c] = words[w][i];
|
||||
}
|
||||
|
||||
placedWords++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill remaining cells with random letters
|
||||
for (unsigned int row = 0; row < searchFieldLen; row++)
|
||||
{
|
||||
for (unsigned int col = 0;
|
||||
col < searchFieldLen;
|
||||
col++)
|
||||
{
|
||||
if (salad[row][col] == EMPTY_CHAR)
|
||||
{
|
||||
salad[row][col] =
|
||||
(char)('A' + rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (int)placedWords;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user