113 lines
2.9 KiB
C
113 lines
2.9 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_RAND_TRIES_PER_WORD 10
|
|
#define EMPTY_CHAR 0
|
|
|
|
// TODO: Spiellogik implementieren:
|
|
/* * 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)
|
|
{
|
|
srand(time(NULL));
|
|
|
|
const char buchstaben[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
int placedWords = 0;
|
|
|
|
for (int i = 0; i < wordCount; i++)
|
|
{
|
|
|
|
int vertikal = rand() % 2;
|
|
|
|
size_t länge = strlen(words[i]);
|
|
int positionX;
|
|
int positionY;
|
|
int tries = 0;
|
|
int belegt;
|
|
|
|
while (tries <= MAX_RAND_TRIES_PER_WORD)
|
|
{
|
|
if (vertikal)
|
|
{
|
|
positionX = rand() % (searchFieldLen);
|
|
positionY = rand() % (searchFieldLen - länge);
|
|
|
|
for (int y = positionY; y < (länge + positionY); y++)
|
|
{
|
|
if (salad[y][positionX] != '0')
|
|
{
|
|
tries++;
|
|
belegt = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(belegt){
|
|
continue;
|
|
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);
|
|
positionY = rand() % (searchFieldLen);
|
|
|
|
for (int x = positionX; x < (länge + positionX); x++)
|
|
{
|
|
if (salad[positionY][x] != '0')
|
|
{
|
|
tries++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(belegt){
|
|
continue;
|
|
belegt = 0;
|
|
}
|
|
|
|
for (int x = positionX, j = 0; x < (länge + positionX); x++, j++)
|
|
{
|
|
salad[positionY][x] = words[i][j];
|
|
}
|
|
placedWords++;
|
|
break; // Wort wurde hinzugefügt, while Schleife wird verlassen
|
|
}
|
|
}
|
|
}
|
|
|
|
for(int i = 0; i< searchFieldLen;i++){
|
|
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
|
|
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 j = 0; j< searchFieldLen;j++){
|
|
printf("%c",salad[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|