generated from freudenreichan/info2Praktikum-Wortsalat
129 lines
3.5 KiB
C
129 lines
3.5 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#define MAX_RAND_TRIES_PER_WORD 100
|
|
#define EMPTY_CHAR 0
|
|
|
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
|
|
|
// 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 wordlength;
|
|
int direction;
|
|
int horizontal;
|
|
int vertical;
|
|
int possible_coordinate = 0;
|
|
int place_word = 0;
|
|
|
|
// Initialisierung Zufallszahl
|
|
srand(time(NULL));
|
|
|
|
// Generierung des Wortfeldes: Alle Felder auf "Leer" setzen
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
salad[i][j] = '\0';
|
|
|
|
}
|
|
}
|
|
|
|
for (int w = 0; w < wordCount; w++)
|
|
{
|
|
wordlength = strlen(words[w]);
|
|
possible_coordinate = 0;
|
|
|
|
// Versuch bis zu MAX_RAND_TRIES_PER_WORD eine Position zu finden
|
|
for (int c = 0; c < MAX_RAND_TRIES_PER_WORD && possible_coordinate == 0; c++)
|
|
{
|
|
possible_coordinate = 1;
|
|
|
|
direction = rand() % 2;
|
|
|
|
if (direction == 0) // Vertikale Wortpositionierung
|
|
{
|
|
vertical = rand() % (searchFieldLen - wordlength + 1);
|
|
horizontal = rand() % searchFieldLen;
|
|
|
|
for (int i = 0; i < wordlength; i++)
|
|
{
|
|
char current = salad[vertical + i][horizontal];
|
|
// Feld muss leer oder mit gleichem Buchstaben belegt sein
|
|
if (current != '\0' && current != words[w][i])
|
|
{
|
|
possible_coordinate = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else // Horizontale Wortpositionierung
|
|
{
|
|
vertical = rand() % searchFieldLen;
|
|
horizontal = rand() % (searchFieldLen - wordlength + 1);
|
|
|
|
for (int i = 0; i < wordlength; i++)
|
|
{
|
|
char current = salad[vertical][horizontal + i];
|
|
if (current != '\0' && current != words[w][i])
|
|
{
|
|
possible_coordinate = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (possible_coordinate == 0)
|
|
return place_word; // Rückgabe: Anzahl der positionierten Wörter
|
|
|
|
// Platzierung des Wortes
|
|
if (direction == 0) // Vertikal Wortpositionierung
|
|
{
|
|
for (int i = 0; i < wordlength; i++)
|
|
{
|
|
salad[vertical + i][horizontal] = words[w][i];
|
|
}
|
|
}
|
|
else // Horizontal Wortpositionierung
|
|
{
|
|
for (int i = 0; i < wordlength; i++)
|
|
{
|
|
salad[vertical][horizontal + i] = words[w][i];
|
|
}
|
|
}
|
|
place_word = w + 1;
|
|
}
|
|
|
|
// Auffüllen der leeren Felder
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
if (salad[i][j] == '\0')
|
|
{
|
|
salad[i][j] = 'A' + rand() % 26;
|
|
}
|
|
}
|
|
}
|
|
return place_word;
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
printf("---Wortsalat---\n");
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
printf("%c", salad[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|