final
This commit is contained in:
parent
3e0fe5f977
commit
25f7fb4d39
@ -14,98 +14,121 @@
|
|||||||
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)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
unsigned int placedWordsCount = 0;
|
||||||
|
// salad = spielfeld (100*100)
|
||||||
|
// searchFieldLen = salad Size = 20
|
||||||
|
// words = array mit den zu plazierenden wörtern
|
||||||
|
// anzahl an zu plazierenden wörtern
|
||||||
|
const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
const int anzahlBuchstaben = 26;
|
||||||
|
|
||||||
const char buchstaben[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int placedWords = 0;
|
typedef enum
|
||||||
|
{
|
||||||
|
VERTIKAL,
|
||||||
|
HORIZONTAL
|
||||||
|
} Richtung;
|
||||||
|
|
||||||
for (int i = 0; i < wordCount; i++)
|
for (int i = 0; i < wordCount; i++)
|
||||||
{
|
{
|
||||||
|
Richtung vertikal_horizontal = rand() % 2; // 1 = vertikal //0 = horizontal
|
||||||
int vertikal = rand() % 2;
|
int leange = strlen(words[i]);
|
||||||
|
int positionX = 0;
|
||||||
size_t länge = strlen(words[i]);
|
int positionY = 0;
|
||||||
int positionX;
|
int voll = 0;
|
||||||
int positionY;
|
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
int belegt;
|
|
||||||
|
|
||||||
while (tries <= MAX_RAND_TRIES_PER_WORD)
|
if (vertikal_horizontal == VERTIKAL)
|
||||||
{
|
{
|
||||||
if (vertikal)
|
|
||||||
|
tries = 0;
|
||||||
|
do
|
||||||
{
|
{
|
||||||
|
voll = 0;
|
||||||
positionX = rand() % (searchFieldLen);
|
positionX = rand() % (searchFieldLen);
|
||||||
positionY = rand() % (searchFieldLen - länge);
|
positionY = rand() % (searchFieldLen - leange);
|
||||||
|
for (int j = positionY; j < positionY + leange; j++)
|
||||||
for (int y = positionY; y < (länge + positionY); y++)
|
{ // überprüfung ob alle positionen 0 sind
|
||||||
{
|
if (salad[j][positionX] != EMPTY_CHAR)
|
||||||
if (salad[y][positionX] != '0')
|
|
||||||
{
|
{
|
||||||
tries++;
|
voll = 1;
|
||||||
belegt = 1;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tries++;
|
||||||
if(belegt){
|
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||||
continue;
|
if (voll == 0)
|
||||||
belegt = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int y = positionY, j = 0; y < (länge + positionY); y++, j++)
|
|
||||||
{
|
|
||||||
salad[y][positionX] = words[i][j];
|
|
||||||
}
|
|
||||||
placedWords++;
|
|
||||||
break; // Wort wurde hinzugefügt, while Schleife wird verlassen
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
positionX = rand() % (searchFieldLen - länge);
|
for (int k = positionY, l = 0; k < positionY + leange; k++, l++)
|
||||||
positionY = rand() % (searchFieldLen);
|
{ // buchstaben holen und setzen
|
||||||
|
salad[k][positionX] = words[i][l];
|
||||||
|
}
|
||||||
|
placedWordsCount++;
|
||||||
|
}
|
||||||
|
else if (voll == 1)
|
||||||
|
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||||
|
}
|
||||||
|
else if (vertikal_horizontal == HORIZONTAL)
|
||||||
|
{
|
||||||
|
|
||||||
for (int x = positionX; x < (länge + positionX); x++)
|
tries = 0;
|
||||||
{
|
do
|
||||||
if (salad[positionY][x] != '0')
|
{
|
||||||
|
voll = 0;
|
||||||
|
positionY = rand() % (searchFieldLen);
|
||||||
|
positionX = rand() % (searchFieldLen - leange);
|
||||||
|
for (int j = positionX; j < positionX + leange; j++)
|
||||||
|
{ // überprüfung ob alle positionen 0 sind
|
||||||
|
if (salad[positionY][j] != EMPTY_CHAR)
|
||||||
{
|
{
|
||||||
tries++;
|
voll = 1;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tries++;
|
||||||
if(belegt){
|
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
|
||||||
continue;
|
if (voll == 0)
|
||||||
belegt = 0;
|
{
|
||||||
|
for (int k = positionX, l = 0; k < positionX + leange; k++, l++)
|
||||||
|
{ // buchstaben holen und setzen
|
||||||
|
salad[positionY][k] = words[i][l];
|
||||||
}
|
}
|
||||||
|
placedWordsCount++;
|
||||||
|
}
|
||||||
|
else if (voll == 1)
|
||||||
|
printf("Das Wort '%s' konnte nicht plaziert werden", words[i]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal);
|
||||||
|
}
|
||||||
|
|
||||||
for (int x = positionX, j = 0; x < (länge + positionX); x++, j++)
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
salad[positionY][x] = words[i][j];
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
}
|
{
|
||||||
placedWords++;
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
break; // Wort wurde hinzugefügt, while Schleife wird verlassen
|
{
|
||||||
|
salad[i][j] = buchstaben[rand() % anzahlBuchstaben];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i< searchFieldLen;i++){
|
return placedWordsCount;
|
||||||
for(int j = 0; j< searchFieldLen;j++){
|
|
||||||
if(salad[i][j] == '0'){
|
|
||||||
salad[i][j] = buchstaben[rand() % 26];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return placedWords;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
for(int i = 0; i< searchFieldLen;i++){
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
for(int j = 0; j< searchFieldLen;j++){
|
{
|
||||||
printf("%c",salad[i][j]);
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
printf(" %c", salad[i][j]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -31,6 +31,11 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//hier müsste man den token string in großbuchstaben formatieren
|
||||||
|
for (int i = 0; i < strlen(token); i++)
|
||||||
|
{
|
||||||
|
token[i] = toupper(token[i]);
|
||||||
|
}
|
||||||
strncpy(words[count], token, MAX_WORD_LEN-1);
|
strncpy(words[count], token, MAX_WORD_LEN-1);
|
||||||
words[count][MAX_WORD_LEN-1] = '\0';
|
words[count][MAX_WORD_LEN-1] = '\0';
|
||||||
|
|
||||||
@ -43,4 +48,14 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 00M 01a 02x
|
||||||
|
// 10W 11a 12l 13t 14e 15r
|
||||||
|
// 20T 21o 22b 23i 24a 25s
|
||||||
|
|
||||||
|
// array[3][2]
|
||||||
|
// 00 01
|
||||||
|
// 10 11
|
||||||
|
// 20 21
|
||||||
|
|||||||
Binary file not shown.
@ -29,14 +29,6 @@ int main(int argc, char *argv[])
|
|||||||
unsigned int placedWords = 0;
|
unsigned int placedWords = 0;
|
||||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||||
|
|
||||||
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
|
||||||
{
|
|
||||||
wordSalad[i][j] = '0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|||||||
Binary file not shown.
BIN
Start_Windows/runTests.exe
Normal file
BIN
Start_Windows/runTests.exe
Normal file
Binary file not shown.
1
Start_Windows/testwords_delims.txt
Normal file
1
Start_Windows/testwords_delims.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hund,Katze; Maus
|
||||||
0
Start_Windows/testwords_empty.txt
Normal file
0
Start_Windows/testwords_empty.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
3
Start_Windows/testwords_simple.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Apfel
|
||||||
|
Banane
|
||||||
|
Kiwi
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user