2025-10-31 09:28:41 +01:00

80 lines
2.6 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 */
//words und salad sind char arrays --> laengstes wort sind 100 zeichen
// 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));
for(int i = 0; i < MAX_NUMBER_OF_WORDS; i++) //geht Wortliste durch und fuellt die Woerter erstmal in salad
{
if(word[i][0] == NULL)
{
break;
}
int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount;
int value = (rand*rand) / rand;
if(rand < 0) //wenn rand eine negative Zahl ist, soll das Wort horizontal platziert werden
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) // geht salad durch
{
if(salad[j][i] == NULL) //sucht horizontal nach einer freien Zeile in salad
{
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) //kopiert die Zeichen in word in die Zeile in salad
{
salad[j][h] = word[value][h]; //value ist der Wert des zufaelligen Wortes, also der Platz im word Array
}
}
}
}
else //vertikal platzieren
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) // geht salad durch
{
if(salad[i][j] == NULL) //sucht vertikal nach einer freien Zeile in salad
{
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++) //kopiert die Zeichen in word in die Spalte in salad
{
salad[h][j] = word[value][h]; //value ist der Wert des zufaelligen Wortes, also der Platz im word Array
}
}
}
}
}
//jetzt ist die Wortliste leer oder es wurden alle Worte in Salad geschrieben.
//Restliche Plaetze mit random Buchstaben fuellen:
int num = rand()%(26 - 1 + 1) - 1;
char letter = 64 + num;
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
{
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
{
if(salad[i][j] == NULL)
{
salad[i][j] = letter;
}
}
}
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
}