Alle Wörter werden erfolgreich in den Wortsalat gepackt und der Salat kann über die Console ausgegeben werden. Es müssen nur noch alle leeren '#' Felder mit zufälligen Buchstaben gefüllt werden. Aber das wird am ende Gemacht wenn alles passt um einfacher zu schauen was klappt
This commit is contained in:
parent
cf584df1b6
commit
207d93b317
@ -2,6 +2,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
@ -28,26 +29,28 @@ int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], shor
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
for(short i = searchFieldLen; i>0; i--)
|
for(short i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
for(short j = searchFieldLen; j>0; j--)
|
for(short j = 0; j < searchFieldLen; j++)
|
||||||
{
|
{
|
||||||
salad[i][j]= "#";
|
salad[i][j] = '#';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
srand(time(NULL));
|
||||||
for(short i = wordCount;i>0;i--)
|
for(short i = wordCount;i>0;i--)
|
||||||
{
|
{
|
||||||
size_t platzbedarf = strlen(words[i-1]);
|
size_t platzbedarf = strlen(words[i-1]);
|
||||||
if (platzbedarf >= searchFieldLen)
|
if (platzbedarf > searchFieldLen)
|
||||||
{
|
{
|
||||||
printf("%s konnte nicht eingefuegt werden da es groeßer als das Feld ist",words[wordCount-1]);
|
printf("%s konnte nicht eingefuegt werden da es groesser als das Feld ist\n",words[i-1]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
srand(time(NULL));
|
|
||||||
short x,y,waagrecht;
|
short x,y,waagrecht;
|
||||||
|
int zuoft = enoughTries(searchFieldLen,0.99);
|
||||||
|
int versuche = 0;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
versuche++;
|
||||||
short max = searchFieldLen - platzbedarf;
|
short max = searchFieldLen - platzbedarf;
|
||||||
short position1 = rand() % (max +1);
|
short position1 = rand() % (max +1);
|
||||||
short position2 = rand() % (searchFieldLen);
|
short position2 = rand() % (searchFieldLen);
|
||||||
@ -56,7 +59,18 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
y = (waagrecht) ? position2:position1;
|
y = (waagrecht) ? position2:position1;
|
||||||
|
|
||||||
|
|
||||||
}while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1);
|
}while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1 && versuche<=zuoft);
|
||||||
|
if(versuche > zuoft)
|
||||||
|
{
|
||||||
|
printf("%s passt nicht mehr ins Gitter\n",words[i-1]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for(size_t k = 0 ; k < strlen(words[i-1]); k++ )
|
||||||
|
{
|
||||||
|
short xoffset = (waagrecht) ? k : 0;
|
||||||
|
short yoffset = (waagrecht) ? 0 : k;
|
||||||
|
salad[x+xoffset][y+yoffset]=words[i-1][k];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -75,3 +89,10 @@ void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int enoughTries(double feldgroesse, double sicherheit) {
|
||||||
|
double moeglichkeiten = feldgroesse * feldgroesse; // Anzahl der Möglichkeiten
|
||||||
|
double c = -log(-log(sicherheit)); // Zusatzterm aus Zielwahrscheinlichkeit
|
||||||
|
double versuche = moeglichkeiten * (log(moeglichkeiten) + c); // Coupon-Collector-Formel
|
||||||
|
return (int)ceil(versuche);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,8 +4,8 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
#define MAX_SEARCH_FIELD_LEN 100
|
#define MAX_SEARCH_FIELD_LEN 100
|
||||||
|
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[]);
|
||||||
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);
|
||||||
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);
|
||||||
|
int enoughTries(double feldgroesse, double sicherheit);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -9,6 +9,23 @@
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
/*char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
char words[MAX_NUMBER_OF_WORDS][100] =
|
||||||
|
{
|
||||||
|
"TESTN",
|
||||||
|
"KATZE",
|
||||||
|
"ENTE",
|
||||||
|
"BEERE",
|
||||||
|
"FEE",
|
||||||
|
"KLEE",
|
||||||
|
"Feheler",
|
||||||
|
"SEE",
|
||||||
|
};
|
||||||
|
int wordCount = 8;
|
||||||
|
createWordSalad(salad,5, words,wordCount);
|
||||||
|
showWordSalad(salad,5);
|
||||||
|
|
||||||
|
*/
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// Check if the correct number of arguments is provided
|
||||||
@ -51,4 +68,5 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user