140 lines
5.4 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))+ 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 - erledigt
* Wenn ein Wort nicht zufällig platziert werden kann, muss es systemtatisch platziert werden! - erledigt*/
// 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 wordPlaced = 0, amountFailed = 0;
int wordlength = 0;
do
{
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
{//Horizontal
int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{
if(salad[startPosition1 + j][startPosition2] != 63)
allClear++;
}
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition1 + j][startPosition2] = words[i][j];
}
wordPlaced++;
wordamount++;
}
else//Notes the failed attempt to place the word
{
amountFailed++;
}
}
else
{//Vertical
int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{
if(salad[startPosition2][startPosition1 + j] != 63)
allClear++;
}
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[startPosition2][startPosition1 + j] = words[i][j];
}
wordPlaced++;
wordamount++;
}
else//Notes the failed attempt to place the word
{
amountFailed++;
}
}
}while((wordPlaced == 0)&&(amountFailed < MAX_RAND_TRIES_PER_WORD));
int index = 0;
while((wordPlaced == 0)&&(index < searchFieldLen))//Places Words that couln't be put in normally in the highest up horizontal place.
{
int allClear = 0;
for(int j = 0; j < wordlength; j++)//Checks, if there already is a word, where the new word should be placed.
{
if(salad[index + j][0] != 63)
allClear++;
}
if(allClear == 0)//Places the word
{
for(int j = 0; j < wordlength; j++)
{
salad[index + j][0] = words[i][j];
}
wordPlaced++;
wordamount++;
}
index++;
}
}
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!");
}
}