nach Kontrolle Fehler: Test nicht bestanden: Wordsalad too Small und all Wordsplaced

This commit is contained in:
Tobias Busch 2025-11-04 18:25:01 +01:00
parent 81d0e35517
commit 7c9460eb99
5 changed files with 76 additions and 52 deletions

View File

@ -6,107 +6,131 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: // TODO: Spiellogik implementieren:
/* * 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 // 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)
{ {
srand(time(NULL)); srand(time(NULL));
unsigned int placedWordsCount = 0; unsigned int placedWordsCount = 0;
//salad = spielfeld (100*100) // salad = spielfeld (100*100)
//searchFieldLen = salad Size = 20 // searchFieldLen = salad Size = 20
//words = array mit den zu plazierenden wörtern // words = array mit den zu plazierenden wörtern
//anzahl an zu plazierenden wörtern // anzahl an zu plazierenden wörtern
const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char buchstaben[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int anzahlBuchstaben = 26; const int anzahlBuchstaben = 26;
for (int i = 0; i < wordCount; i++) { int positionX = 0;
typedef enum {VERTIKAL, HORIZONTAL} Richtung; int positionY = 0;
Richtung vertikal_horizontal = rand() % 2; //1 = vertikal //0 = horizontal int voll = 0;
int leange = strlen(words[i]); int tries = 0;
int positionX = 0; int leange = 0;
int positionY = 0;
int voll = 0;
int tries = 0;
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++) typedef enum
{
VERTIKAL,
HORIZONTAL
} Richtung;
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{ {
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) salad[i][j] = EMPTY_CHAR;
{
salad[i][j] = EMPTY_CHAR;
}
} }
}
if (vertikal_horizontal == VERTIKAL) { for (int i = 0; i < wordCount; i++)
{
tries = 0; Richtung vertikal_horizontal = rand() % 2; // 0 = vertikal //1 = horizontal
do { leange = strlen(words[i]);
tries = 0;
if (vertikal_horizontal == VERTIKAL)
{
do
{
voll = 0; voll = 0;
positionX = rand() % (searchFieldLen); positionX = rand() % (searchFieldLen);
positionY = rand() % (searchFieldLen-leange); positionY = rand() % (searchFieldLen - leange);
for(int j = positionY; j < positionY+leange ; j++) { //überprüfung ob alle positionen 0 sind for (int j = positionY; j < positionY + leange; j++)
if(salad[j][positionX] != EMPTY_CHAR) { { // überprüfung ob alle positionen 0 sind
if (salad[j][positionX] != EMPTY_CHAR)
{
voll = 1; voll = 1;
} }
} }
tries++; tries++;
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD); } while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
if (voll == 0) { if (voll == 0)
for(int k = positionY, l = 0; k < positionY+leange; k++, l++) { //buchstaben holen und setzen {
for (int k = positionY, l = 0; k < positionY + leange; k++, l++)
{ // buchstaben holen und setzen
salad[k][positionX] = words[i][l]; salad[k][positionX] = words[i][l];
} }
placedWordsCount++; placedWordsCount++;
} else if (voll == 1) }
else if (voll == 1)
printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]); printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]);
}
} else if (vertikal_horizontal == HORIZONTAL) { else if (vertikal_horizontal == HORIZONTAL) //bessser als funktion, weil fast doppelt
{
tries = 0; do
do { {
voll = 0; voll = 0;
positionY = rand() % (searchFieldLen); positionY = rand() % (searchFieldLen);
positionX = rand() % (searchFieldLen-leange); positionX = rand() % (searchFieldLen - leange);
for(int j = positionX; j < positionX+leange ; j++) { //überprüfung ob alle positionen 0 sind for (int j = positionX; j < positionX + leange; j++)
if(salad[positionY][j] != EMPTY_CHAR) { { // überprüfung ob alle positionen 0 sind
if (salad[positionY][j] != EMPTY_CHAR)
{
voll = 1; voll = 1;
} }
} }
tries++; tries++;
} while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD); } while (voll == 1 && tries <= MAX_RAND_TRIES_PER_WORD);
if (voll == 0) { if (voll == 0)
for(int k = positionX, l = 0; k < positionX+leange; k++, l++) { //buchstaben holen und setzen {
for (int k = positionX, l = 0; k < positionX + leange; k++, l++)
{ // buchstaben holen und setzen
salad[positionY][k] = words[i][l]; salad[positionY][k] = words[i][l];
} }
placedWordsCount++; placedWordsCount++;
} else if (voll == 1) }
printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]); else if (voll == 1)
printf("Das Wort '%s' konnte nicht plaziert werden\n", words[i]); //besser als perror
} else }
else
printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal); printf("Fehler bei Vertikal Horizontal Wert: %d", vertikal_horizontal);
} }
for(int i = 0; i < searchFieldLen; i++) { for (int i = 0; i < searchFieldLen; i++)
for(int j = 0; j < searchFieldLen; j++) { {
if(salad[i][j] == EMPTY_CHAR) { for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR)
{
salad[i][j] = buchstaben[rand() % anzahlBuchstaben]; salad[i][j] = buchstaben[rand() % anzahlBuchstaben];
} }
} }
} }
return placedWordsCount; return placedWordsCount;
} }
// 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++) { {
for (int j = 0; j < searchFieldLen; j++)
{
printf(" %c", salad[i][j]); printf(" %c", salad[i][j]);
} }
printf("\n"); printf("\n");
} }
} }

Binary file not shown.

View File

@ -11,14 +11,14 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
unsigned int count = 0; unsigned int count = 0;
char line[MAX_LINE_LEN]; char line[MAX_LINE_LEN];
char *teiler = " ,;\n\t"; const char teiler[] = " ,;\n\t"; //literale
char *token; char *token = NULL;
while(fgets(line, sizeof(line), file)){ while(fgets(line, sizeof(line), file)){
//Yeti,Nessie Werwolf; Vampir //Yeti,Nessie Werwolf; Vampir
// token = Yeti // token = Yeti
token = strtok(line, teiler); token = strtok(line, teiler); //besser wäre: strpbrk
while (token != NULL) while (token != NULL)
{ {

Binary file not shown.

Binary file not shown.