2025-11-03 10:50:43 +01:00

171 lines
5.3 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));
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)
{
}
// Decides wether to print horizontally or vertically
// returns 1, when horizontal
int printHorizontal(unsigned int wordCount)
{
int rand = rand() % (wordCount - (-wordCount) + 1) - wordCount;
if(rand > 0)
{
return 1;
}
return 0;
}
// checks wether after all words are filled, empty spaces are left
// returns 1, when empty spaces are left
int emptyPlaces(unsigned int wordCount)
{
if(wordCount < MAX_SEARCH_FIELD_LEN)
{
return 1;
}
return 0;
}
// fills salad array with max. words from word array either horizontally or vertically
void fillSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
for(int i= 0; i< searchFieldLen; i++) //for every word in salad, it is determined wether hor or ver
{
if(printHorizontal(wordCount)) //horizontal
{
for(int j= 0; j < searchFieldLen; j++) //sucht freie Zeile in salad
{
if(salad[j][0] == NULL)
{
int numWord = whichWord(words, wordCount); //determines, which word will be written in salad
fillWordinRow(salad, searchFieldLen, words, wordCount, numWord, j); // fills word horizontally in salad and deletes the word afterwards
}
}
}
else{ //vertical, immer von oben nach unten
for(int j = 0; j< searchFieldLen; j++)
{
if(salad[0][j] == NULL)
{
int numWord = whichWord(words, wordCount);
fillWordinColumn(salad, searchFieldLen, words, wordCount, numWord, j);
}
}
}
}
}
// Determines, which word from words is filled into salad
int whichWord(const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
while(true){
int numWord = rand()% (wordCount + 1);
if(words[numWord][0] != NULL)
{
return numWord; //return random row, which has not been used yet
}
}
}
//Fills word in salad and deletes row of words of the used word
void fillWordinRow(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int row)
{
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++)
{
for(int g = 0; g < MAX_WORD_LEN; g++)
{
salad[row][h] = words[numWord][g];
words[numWord][g] = NULL;
}
}
}
//Fills word in salad and deletes row of words of the used word, vertical from top to bottom
void fillWordinColumn(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount, int numWord, int column)
{
for(int h = 0; h< MAX_SEARCH_FIELD_LEN; h++)
{
for(int g = 0; g < MAX_WORD_LEN; g++)
{
salad[h][column] = word[numWord][g];
words[numWord][g] = NULL;
}
}
}
// if emptyPlaces, fill these with random letters
void fillRandom()
{
}