Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d9fbdf925 | ||
|
|
2de180513e | ||
|
|
5832baf300 |
No files matched your search
+89
-1
@@ -10,8 +10,96 @@
|
|||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
enum PlaceDirection {
|
||||||
|
HORIZONTAL,
|
||||||
|
VERTICAL
|
||||||
|
};
|
||||||
|
|
||||||
|
bool canPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word, unsigned int wordLength, unsigned int row, unsigned int col, PlaceDirection dir)
|
||||||
|
{
|
||||||
|
if (dir == HORIZONTAL) // HORIZONTAL
|
||||||
|
{
|
||||||
|
if (col + wordLength > searchFieldLen) return false;
|
||||||
|
for (unsigned int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
char current = salad[row][col + i];
|
||||||
|
if (current != EMPTY_CHAR && current != word[i]) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // VERTICAL
|
||||||
|
{
|
||||||
|
if (row + wordLength > searchFieldLen) return false;
|
||||||
|
for (unsigned int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
char current = salad[row + i][col];
|
||||||
|
if (current != EMPTY_CHAR && current != word[i]) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write word in the grid
|
||||||
|
void writeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char *word, unsigned int wordLength, unsigned int row, unsigned int col, PlaceDirection dir)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < wordLength; i++)
|
||||||
|
{
|
||||||
|
if (dir == HORIZONTAL)
|
||||||
|
{
|
||||||
|
salad[row][col + i] = word[i];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
salad[row + i][col] = word[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// try to place a word with a return value
|
||||||
|
bool placeWordAttempt(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word, unsigned int wordLength, PlaceDirection dir)
|
||||||
|
{
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
|
||||||
|
{
|
||||||
|
unsigned int row, col;
|
||||||
|
if (dir == HORIZONTAL)
|
||||||
|
{
|
||||||
|
row = rand() % searchFieldLen;
|
||||||
|
col = rand() % (searchFieldLen - wordLength + 1);
|
||||||
|
}
|
||||||
|
else // VERTICAL
|
||||||
|
{
|
||||||
|
row = rand() % (searchFieldLen - wordLength + 1);
|
||||||
|
col = rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
if (canPlaceWord(salad, searchFieldLen, word, wordLength, row, col, dir))
|
||||||
|
{
|
||||||
|
writeWord(salad, word, wordLength, row, col, dir);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // could not be placed after MAX_RAND_TRIES_PER_WORD tries
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
|
// place words randomly in salad array
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
|
{
|
||||||
|
const char *word = words[i];
|
||||||
|
unsigned int len = strlen(word);
|
||||||
|
PlaceDirection dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
|
||||||
|
|
||||||
|
placeWordAttempt(salad, searchFieldLen, word, len, dir);
|
||||||
|
}
|
||||||
|
// fill up empty fields
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; r++)
|
||||||
|
{
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; c++)
|
||||||
|
{
|
||||||
|
if (salad[r][c] == EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
salad[r][c] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -40,7 +40,13 @@ int main(int argc, char *argv[])
|
|||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// error message if some words couldn't be placed
|
||||||
|
if (placedWords != wordCount)
|
||||||
|
{
|
||||||
|
printf("Error: Some words couldn't be placed!");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 500);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -52,3 +58,6 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
hallii halloo probee
|
||||||
Reference in new issue
Block a user