111 lines
3.5 KiB
C
111 lines
3.5 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 */
|
|
|
|
void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
if (salad[i][j] == '0') {
|
|
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Creates the word salad by placing words randomly and filling empty spaces
|
|
// - 2D array salad -> fertiger Wortsalat
|
|
// - searchFieldLen -> Dimension der Salatschüssel
|
|
// - 2D array words -> gegebene Wörter
|
|
// - wordCount -> anzahl an Wörtern
|
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
{
|
|
int placedWords = 0;
|
|
|
|
for (int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
salad[i][j] = '0';
|
|
}
|
|
}
|
|
for (int i = 0; i < wordCount; i++)
|
|
{
|
|
int wordLength = strlen(words[i]);
|
|
int xCoordinat = rand() % (searchFieldLen - wordLength + 1);
|
|
int yCoordinat = rand() % searchFieldLen;
|
|
if (rand() % 2) // erzeugt Zufallszahl 0 oder 1: 0 -> Horizontal; 1 -> Vertical
|
|
{
|
|
for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++)
|
|
{
|
|
int fits = 1;
|
|
for (int j = xCoordinat; j < wordLength + xCoordinat; j++)
|
|
{
|
|
if(salad[yCoordinat][j] != '0')
|
|
{
|
|
fits = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
|
|
{
|
|
continue;
|
|
}
|
|
else //Wort platzieren
|
|
{
|
|
for (int j = xCoordinat; j < wordLength + xCoordinat; j++)
|
|
{
|
|
salad[yCoordinat][j] = words[i][j - xCoordinat];
|
|
}
|
|
placedWords += 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++)
|
|
{
|
|
int fits = 1;
|
|
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
|
|
{
|
|
if(salad[j][yCoordinat] != '0')
|
|
{
|
|
fits = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt
|
|
{
|
|
continue;
|
|
}
|
|
else //Wort platzieren
|
|
{
|
|
for (int j = yCoordinat; j < wordLength + yCoordinat; j++)
|
|
{
|
|
salad[j][yCoordinat] = words[i][j - yCoordinat];
|
|
}
|
|
placedWords += 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
|
|
}
|