2025-11-10 13:25:34 +01:00

167 lines
4.8 KiB
C

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
//#include <ctype.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));
int placedWords = 0;
unsigned int attemps = 0;
unsigned int check_direction = 0, check_overlap = 0;
int zeile = 0, spalte = 0, len = 0;
//Alle Felder mit ? befüllen
for(int m = 0; m < searchFieldLen; m++) //m Zeilen
{
for(int n = 0; n < searchFieldLen; n++) //n Spalte
{
salad[m][n] = '?';
}
}
//Prüfen ob Wort in Array geschrieben darf
for(int num_words = 0; num_words < wordCount; num_words++) //eingelesen Wörter zählen
{
len = strlen(words[num_words]); //Größe des Wortes ermitteln
while(attemps < MAX_RAND_TRIES_PER_WORD)
{
// zufällige Richtung auswählen
int direction = rand() % 2; // 0 = VERTIKAL, 1 = HORIZONTAL
// zufälliger Startpunkt, Startkoordinaten
if (direction == 0) // 0 = VERTIKAL
{
zeile = rand() % searchFieldLen;
spalte = rand() % searchFieldLen;
check_direction = 1;
}
if (direction == 1) // 1 = HORIZONTAL
{
zeile = rand() % searchFieldLen;
spalte = rand() % searchFieldLen;
check_direction = 1;
}
for(int i = 0; i < len; i++)
{
if (direction == 0) // 0 = VERTIKAL
{
//Prüfe ob Feld bereits belegt
if (salad[zeile+i][spalte] != '?')
{
check_overlap = 1; // 1 = Überlappung
break;
}
//Prüfe auf Groeße
if (spalte + len >= searchFieldLen-1 || check_overlap == 1)
{
attemps++;
}
else
{
for(int k; k < len; k++)
{
salad[zeile][spalte+k] = words[i][k];
placedWords++;
}
}
}
if (direction == 1) // 1 = HORZIZONTAL
{
//Prüfe ob Feld bereits belegt
if (salad[zeile][spalte+i] != '?')
{
check_overlap = 1; // 1 = Überlappung
break;
}
//Prüfe auf Groeße
if (zeile + len >= searchFieldLen-1 || check_overlap == 1)
{
attemps++;
}
else
{
for(int k; k < len; k++)
{
salad[zeile+k][spalte] = words[i][k];
placedWords++;
}
}
}
}
}
/*
//Wort in Wortsalat schreiben
if(check_direction == 1 && check_overlap == 0 && tries < MAX_RAND_TRIES_PER_WORD)
{
for(int i_set = 0; i_set < len; i_set++)
{
if (direction == 0) // 0 = VERTIKAL
{
salad[zeile + i_set][spalte] = words[num_words][i_set];
placedWords++;
}
if (direction == 1) // 1 = HORIZONTAL
{
salad[zeile][spalte + i_set] = words[num_words][i_set];
placedWords++;
}
}
}
}
}
/*
// fügt zufällige Buchstaben ein
for(int l = 0; l < searchFieldLen; l++){
for(int m = 0; m < searchFieldLen; m++ ){
if(isalpha(salad[l][m]) == 0 ){
// zufällige Buchstaben erzeugen
char alphabet [] = "abcdefghijklmnopqrstuvwxyz";
int laenge = strlen(alphabet);
int zufallszahl = rand()% laenge;
char zufallsbuchstabe = alphabet[zufallszahl];
// zufällige Buchstaben einfügen
salad[l][m] = zufallsbuchstabe;
}
}
}
*/
}
return placedWords; //platzierte Wörter zurückgeben
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
for(int p = 0; p < searchFieldLen; p++)
{
for(int q = 0; q < searchFieldLen; q++)
{
printf("%c", salad[p][q]);
}
printf("\n");
}
}