test wird bestanden
This commit is contained in:
parent
ad88cbbba6
commit
df2d64c137
@ -24,8 +24,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// showWordSalad(salad, 20);
|
||||||
showWordSalad(salad, 20);
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
int wordsPlaced = 0;
|
int wordsPlaced = 0;
|
||||||
@ -33,116 +32,114 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
// loop through all words
|
// loop through all words
|
||||||
for (int n = 0; n < wordCount; n++)
|
for (int n = 0; n < wordCount; n++)
|
||||||
{
|
{
|
||||||
int checkSquare, letterPlaced, retries = 0;
|
int checkSquare, retries = 0;
|
||||||
int wordLength = strlen(words[n]);
|
int wordLength = strlen(words[n]);
|
||||||
|
|
||||||
|
if(wordLength > searchFieldLen) {
|
||||||
|
printf("Fehler");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// get random coordinates
|
// get random coordinates
|
||||||
int rndX = rand() % (searchFieldLen - wordLength + 1);
|
int rndX = rand() % (searchFieldLen - wordLength + 1);
|
||||||
int rndY = rand() % (searchFieldLen - wordLength + 1);
|
int rndY = rand() % (searchFieldLen - wordLength + 1);
|
||||||
// rand() % 2
|
|
||||||
if (1)
|
if (rand() % 2)
|
||||||
{
|
{
|
||||||
while (letterPlaced < wordLength)
|
// checks if word fits into gamefield
|
||||||
|
while (checkSquare != wordLength && retries < 10)
|
||||||
{
|
{
|
||||||
// checks if word fits into gamefield
|
checkSquare = 0;
|
||||||
while (checkSquare != wordLength && retries < 10)
|
// checks if squares to paste into is already taken
|
||||||
|
for (int x = rndX; x < (rndX + wordLength); x++)
|
||||||
{
|
{
|
||||||
checkSquare = 0;
|
if (salad[rndY][x] == '\0')
|
||||||
// checks if squares to paste into is already taken
|
checkSquare++;
|
||||||
for (int x = rndX; x < (rndX + wordLength); x++)
|
|
||||||
{
|
|
||||||
if (salad[rndY][x] == '\0')
|
|
||||||
checkSquare++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// new coords when doesnt fit
|
|
||||||
if (checkSquare != wordLength)
|
|
||||||
{
|
|
||||||
rndX = rand() % (searchFieldLen - wordLength + 1);
|
|
||||||
rndY = rand() % (searchFieldLen - wordLength + 1);
|
|
||||||
|
|
||||||
// retries++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (retries >= 10)
|
// new coords when doesnt fit
|
||||||
|
if (checkSquare != wordLength)
|
||||||
{
|
{
|
||||||
printf("Word %d couldn't be placed", n);
|
rndX = rand() % (searchFieldLen - wordLength + 1);
|
||||||
break;
|
rndY = rand() % (searchFieldLen - wordLength + 1);
|
||||||
}
|
|
||||||
printf("%d %d",n, checkSquare);
|
retries++;
|
||||||
// pastes word into the line
|
|
||||||
if (checkSquare == wordLength)
|
|
||||||
{
|
|
||||||
for (int x = 0; x < wordLength; x++)
|
|
||||||
{
|
|
||||||
salad[rndY][rndX + x] = words[n][x];
|
|
||||||
letterPlaced++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wordsPlaced++;
|
|
||||||
|
|
||||||
|
if (retries >= 10)
|
||||||
|
{
|
||||||
|
printf("Word %d couldn't be placed", n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pastes word into the line
|
||||||
|
if (checkSquare == wordLength)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < wordLength; x++)
|
||||||
|
{
|
||||||
|
salad[rndY][rndX + x] = words[n][x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wordsPlaced++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (letterPlaced < wordLength)
|
|
||||||
|
// checks if word fits into gamefield
|
||||||
|
while (checkSquare != wordLength && retries < 10)
|
||||||
{
|
{
|
||||||
// checks if word fits into gamefield
|
checkSquare = 0;
|
||||||
while (checkSquare != wordLength && retries < 10)
|
// checks if squares to paste into is already taken
|
||||||
|
for (int y = rndY; y < (rndY + wordLength); y++)
|
||||||
{
|
{
|
||||||
checkSquare = 0;
|
if (salad[y][rndX] == '\0')
|
||||||
// checks if squares to paste into is already taken
|
checkSquare++;
|
||||||
for (int y = rndY; y < (rndY + wordLength); y++)
|
|
||||||
{
|
|
||||||
if (salad[y][rndX] == '\0')
|
|
||||||
checkSquare++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// new coords when doesnt fit
|
|
||||||
if (checkSquare != wordLength)
|
|
||||||
{
|
|
||||||
rndX = rand() % (searchFieldLen - wordLength + 1);
|
|
||||||
rndY = rand() % (searchFieldLen - wordLength + 1);
|
|
||||||
|
|
||||||
retries++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (retries >= 10)
|
// new coords when doesnt fit
|
||||||
|
if (checkSquare != wordLength)
|
||||||
{
|
{
|
||||||
printf("Word %u couldn't be placed", n);
|
rndX = rand() % (searchFieldLen - wordLength + 1);
|
||||||
break;
|
rndY = rand() % (searchFieldLen - wordLength + 1);
|
||||||
}
|
|
||||||
|
|
||||||
// pastes word into the line
|
retries++;
|
||||||
if (checkSquare == wordLength)
|
|
||||||
{
|
|
||||||
for (int y = 0; y < wordLength; y++)
|
|
||||||
{
|
|
||||||
salad[y + rndY][rndX] = words[n][y];
|
|
||||||
letterPlaced++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (retries >= 10)
|
||||||
|
{
|
||||||
|
printf("Word %d couldn't be placed", n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pastes word into the line
|
||||||
|
if (checkSquare == wordLength)
|
||||||
|
{
|
||||||
|
for (int y = 0; y < wordLength; y++)
|
||||||
|
{
|
||||||
|
salad[y + rndY][rndX] = words[n][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wordsPlaced++;
|
wordsPlaced++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fill voids
|
// fill voids
|
||||||
for(int x = 0; x < searchFieldLen; x++)
|
for (int x = 0; x < searchFieldLen; x++)
|
||||||
{
|
{
|
||||||
for(int y = 0; y < searchFieldLen; y++)
|
for (int y = 0; y < searchFieldLen; y++)
|
||||||
{
|
{
|
||||||
if(salad[y][x] == '\0')
|
if (salad[y][x] == '\0')
|
||||||
{
|
{
|
||||||
// random number from 65 (A) to 90 (Z) with ASCII
|
// random number from 65 (A) to 90 (Z) with ASCII
|
||||||
// salad[y][x] = (char) (rand() % (25 + 1) + 65);
|
salad[y][x] = rand() % (25 + 1) + 65;
|
||||||
salad[y][x] = 120;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 15;
|
return wordsPlaced;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
@ -159,16 +156,3 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
printf(" %c\n", salad[i][searchFieldLen]);
|
printf(" %c\n", salad[i][searchFieldLen]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear Wordsalad
|
|
||||||
void clearWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
||||||
{
|
|
||||||
int i, j = 0;
|
|
||||||
for (i = 0; i < searchFieldLen; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j < searchFieldLen; j++)
|
|
||||||
{
|
|
||||||
salad[i][j] = '\0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -11,12 +11,10 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
char puffer[MAX_LINE_LEN];
|
char puffer[MAX_LINE_LEN];
|
||||||
char *teiler = " ,;.\n";
|
char *teiler = " ,;.\n";
|
||||||
char *token;
|
char *token;
|
||||||
file = fopen("words.txt", "r");
|
|
||||||
|
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
printf("\nNot able to open file.\n");
|
printf("\nNot able to open file.\n");
|
||||||
fclose(file);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +41,5 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user