87 lines
3.2 KiB
C

#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
#define RANDOMNUMBER(minimum, maximum) ((rand() % ((maximum)-(minimum)+1))+cd minimum) //Macro to generate a Number between minimum and maximum (both are possibly rolled)
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - erledigt
* restliche Felder mit zufälligen Buchstaben füllen - erldeigt
* Rückgabewert ist Anzahl der platzierten Wörter - erledigt
* Es muss gecheckt werden, ob ein anderes platziertes Word fertig ist
* Wenn ein Wort nicht zufällig platziert werden kann, muss es systemtatisch platziert werden!*/
// 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));//sets the seed of the random function to the current time in order to guarantee unique results
for(int i = 0; i < searchFieldLen; i++)//Clears the array to simplify the process of findings empty slots
{
for(int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = 63;
}
}
int wordamount = 0;
for(int i = 0; i < wordCount; i++)
{
int wordlength = 0;
while(words[i][wordlength] != '0')//gets the length of the current word
wordlength++;
int startPosition1 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN - wordlength);//gets a random starting position for the word
int startPosition2 = RANDOMNUMBER(0, MAX_SEARCH_FIELD_LEN);//gest a second coordinate for the starting position
if(RANDOMNUMBER(0,1) < 1)//Flips a coin whether the number is vertical or horizontal
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition1 + j][startPosition2] = words[i][j]
}
}
else
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition2][startPosition1 + j] = words[i][j]
}
}
wordamount++;
}
for(int i = 0; i < searchFieldLen; i++)//Randomly fills the rest of the array
{
for(int j = 0; j < searchFieldLen; j++)
{
if((salad[i][j] == 63))//checks if there is already a letter of a word placed in here
{
salad[i][j] = RANDOMNUMBER(65, 90);//fills "empty" chars in word salad with a random upper case letter
}
}
}
return wordamount;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
if(searchFieldLen <= MAX_SEARCH_FIELD_LEN)
{
for(int i = 0; i < searchFieldLen; i++)
{
for(int j = 0; j < searchFieldLen; j++)
{
putchar(salad[i][j]);//Prints a letter within the salad array
}
putchar(10);//Prints a Line Feed, thus ensuring the correct formatting of the program
}
}
else
{
puts("FEHLER! Buchstabenfeld uebersteigt Maximalzahl der Buchstaben!");
}
}