108 lines
3.3 KiB
C
108 lines
3.3 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
|
|
|
|
//TODO: Spiellogik implementieren: | VERSION: Erfolgreicher Test 4 & 5 I
|
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
|
|
|
// 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)
|
|
{
|
|
//Random Zeit initialisieren
|
|
srand(time(NULL));
|
|
|
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
salad[i][j] = EMPTY_CHAR;
|
|
}
|
|
}
|
|
|
|
int placed = 0; //Anzahl der Erfolgreich platzierten Worte
|
|
|
|
//Platzieren
|
|
for (unsigned int w = 0; w < wordCount; w++)
|
|
{
|
|
const char *word = words[w];
|
|
unsigned int wordLen = strlen(word);
|
|
|
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++)
|
|
{
|
|
int row = rand() % searchFieldLen;
|
|
int col = rand() % searchFieldLen;
|
|
int direction = rand() % 2;
|
|
int canPlace = 1;
|
|
|
|
//Ob EIN Wort platziert werden kann (wird noch nicht platziert!)
|
|
for (unsigned int k = 0; k < wordLen; k++)
|
|
{
|
|
int r = row + (direction == 1 ? k : 0);
|
|
int c = col + (direction == 0 ? k : 0);
|
|
|
|
//Ränder des Spielfeldes beachten
|
|
if (r >= (int)searchFieldLen || c >= (int)searchFieldLen)
|
|
{
|
|
canPlace = 0;
|
|
break;
|
|
}
|
|
|
|
//Ist die Stelle nicht leer und passt Die stelle nicht zum passenden Buchstaben des Wortes
|
|
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
|
|
{
|
|
canPlace = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (canPlace) //Wenn geprüft wurde ob ein Wort erfolgreich platziert werden kann.
|
|
{
|
|
for (unsigned int k = 0; k < wordLen; k++)
|
|
{
|
|
int r = row + (direction == 1 ? k : 0);
|
|
int c = col + (direction == 0 ? k : 0);
|
|
if (r < (int)searchFieldLen && c < (int)searchFieldLen)
|
|
salad[r][c] = word[k];
|
|
}
|
|
placed++;
|
|
break; //Beim Nächsten Wort weiter machen
|
|
}
|
|
}
|
|
}
|
|
|
|
//Mit Zufälligen Buchstaben füllen
|
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
if (salad[i][j] == EMPTY_CHAR)
|
|
{
|
|
salad[i][j] = 'A' + (rand() % 26);
|
|
}
|
|
}
|
|
}
|
|
|
|
return placed;
|
|
}
|
|
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
|
{
|
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
|
{
|
|
printf("%c ", salad[i][j]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|