zweiter Versuch

This commit is contained in:
Adrian Schulze committed 2026-10-30 10:00:04 +01:00
1 parent 4d7ccdf9ff
commit bd39d8ad5e
2 files changed
+60 -20

No files matched your search

+52 -18
View File
@@ -15,6 +15,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_
void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge); void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge);
void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]); void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]);
void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField); void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField);
unsigned int search(char salad[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction);
// Creates the word salad by placing words randomly and filling empty spaces // 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)
@@ -30,8 +31,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
else else
sizeOfField = searchFieldLen; sizeOfField = searchFieldLen;
initializeField(sizeOfField, salad); initializeField(sizeOfField, salad);
// random
srand(time(NULL));
// for all words put them in // for all words put them in
for (unsigned int i = 0; i < wordCount; i++){ for (unsigned int i = 0; i < wordCount; i++){
if (findAPlace(sizeOfField,words,salad,i) == 1) { if (findAPlace(sizeOfField,words,salad,i) == 1) {
@@ -59,10 +58,6 @@ unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Num
return max; return max;
} }
// unsigned int free(unsigned int richtung, unsigned int laenge, unsigned int x, unsigned int y, char spielfeld [][MAX_SEARCH_FIELD_LEN], unsigned int groesse)
// {
// }
void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]) void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN])
{ {
for (unsigned int j = 0; j < groesse; j++){ for (unsigned int j = 0; j < groesse; j++){
@@ -82,7 +77,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_
direction = rand() % 2; // choose 0 = X oder 1 = Y - direction direction = rand() % 2; // choose 0 = X oder 1 = Y - direction
length = strlen(words[position]); length = strlen(words[position]);
maxLineColumn = (sizeOfField + 1) - length; maxLineColumn = (sizeOfField - length) + 1;
for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++) for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++)
{ {
@@ -96,7 +91,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_
} }
if(direction == 1){ if(direction == 1){
unsigned int a = xNumber; unsigned int a = xNumber;
while (a < (xNumber + length) && salad[a][yNumber] == 0) while (a < (xNumber + length) && salad[a][yNumber] == EMPTY_CHAR)
a++; a++;
if (a == xNumber+length) { if (a == xNumber+length) {
putInTheWord(words, position, salad, xNumber, yNumber, direction, length); putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
@@ -105,18 +100,19 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_
} }
else{ else{
unsigned int a = yNumber; unsigned int a = yNumber;
while (a < (yNumber + length) && salad[xNumber][a] == 0) while (a < (yNumber + length) && salad[xNumber][a] == EMPTY_CHAR)
a++; a++;
if (a == yNumber+length) { if (a == yNumber+length) {
putInTheWord(words, position, salad, xNumber, yNumber, direction, length); putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
return 1; return 1;
} }
} }
// if (j == (MAX_RAND_TRIES_PER_WORD -1)){ if (j == (MAX_RAND_TRIES_PER_WORD -1)){
// if (search() == 1) if (search(salad,length, sizeOfField, &xNumber, &yNumber, &direction) == 1) {
// putInTheWord(words, position, salad, xNumber, yNumber, direction, length); putInTheWord(words, position, salad, xNumber, yNumber, direction, length);
// return 1; return 1;
// } }
}
} }
return 0; return 0;
} }
@@ -139,13 +135,51 @@ void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField)
{ {
for (unsigned int j = 0; j < sizeOfField; j++) for (unsigned int j = 0; j < sizeOfField; j++)
{ {
if (field[i][j] == 0) { if (field[i][j] == EMPTY_CHAR) {
char random = 'A' + rand() % 26; char random = 'A' + rand() % 26;
field[i][j] = random; field[i][j] = random;
} }
printf("%1c ", field[i][j]);
} }
printf("\n");
} }
}
unsigned int search(char field[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction)
{
unsigned int freeLength = 0;
for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any line
{
freeLength = 0;
for (unsigned int j = 0; j < sizeOfField; j++)
{
if (field[i][j] == EMPTY_CHAR)
freeLength += 1;
else
freeLength = 0;
if (freeLength >= length){
*x = i;
*y = j+1-length;
*direction = 0;
return 1;
}
}
}
for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any column
{
freeLength = 0;
for (unsigned int j = 0; j < sizeOfField; j++)
{
if (field[j][i] == EMPTY_CHAR)
freeLength += 1;
else
freeLength = 0;
if (freeLength == length){
*x = j+1-length;
*y = i;
*direction = 1;
return 1;
}
}
}
return 0;
} }
+8 -2
View File
@@ -10,7 +10,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
srand(time(NULL));
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
{ {
@@ -35,7 +35,13 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid // Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
if (placedWords == wordCount) {
startGame(wordSalad, SALAD_SIZE, words, wordCount, 1024);
}
else {
printf("Could only %u of %u words. ", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed
// Start the game if successful // Start the game if successful