generated from freudenreichan/info2Praktikum-Wortsalat
99 lines
3.3 KiB
C
99 lines
3.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 */
|
|
|
|
// 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) {
|
|
int addedWords = 0;
|
|
int attempts = 0; //Anzahl an Versuchen ein Wort hinzuzufügen
|
|
int added = 0; //0 Wort nicht hinzugefügt , 1 Wort hinzugefügt
|
|
int space = 1; // 0 Wort zu lang, 1 Wort passt an diese Stelle
|
|
unsigned long long wordLength = 0;
|
|
|
|
for (int i = 0; i < searchFieldLen; i++) {
|
|
for (int j = 0; j < searchFieldLen; j++) {
|
|
salad[i][j] = EMPTY_CHAR; //Füllen des gesamten Arrays mit Nullen
|
|
}
|
|
}
|
|
for (int i=0; i < wordCount; i++) {
|
|
|
|
wordLength = strlen(words[i]);
|
|
|
|
while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) {
|
|
int direction = rand() % 2;// 0 Horizontal, 1 Vertikal
|
|
if (direction == 0) {
|
|
int collumn = rand() % searchFieldLen;
|
|
int row = rand() % searchFieldLen;
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
if (salad[row][collumn+j] != EMPTY_CHAR) {space = 0; break;}
|
|
//Prüfen ob das Wort ein bereits vorhandenes Wort überspeichern würde
|
|
}
|
|
if ((collumn + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
|
|
//Prüfen ob Wort an der Stelle (Zeile, Spalte) in das Array passt
|
|
else {for (int j = 0; j < wordLength; j++)
|
|
{
|
|
salad[row][collumn+j] = words[i][j]; //Einfügen des Wortes Buchstabe für Buchstabe
|
|
}
|
|
added=1;
|
|
addedWords++;
|
|
|
|
}
|
|
}
|
|
else if (direction == 1) {// Vertikal funktioniert analog zu horizontal
|
|
int collumn = rand() % searchFieldLen;
|
|
int row = rand() % searchFieldLen;
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
if (salad[row+j][collumn] != EMPTY_CHAR) {space = 0; break;}
|
|
}
|
|
if ((row + wordLength >= searchFieldLen-1) || (space == 0)) {attempts++;}
|
|
else {
|
|
for (int j = 0; j < wordLength; j++)
|
|
{
|
|
salad[row+j][collumn] = words[i][j];
|
|
}
|
|
added=1;
|
|
addedWords++;
|
|
}
|
|
}
|
|
space = 1;
|
|
}
|
|
attempts = 0;
|
|
added = 0;
|
|
|
|
}
|
|
for (int i = 0; i < searchFieldLen; i++) {
|
|
for (int j = 0; j < searchFieldLen; j++) {
|
|
if (salad[i][j] == EMPTY_CHAR)
|
|
{
|
|
salad[i][j] = rand()%('Z'-'A'+1)+'A';//Alle Felder, die noch nicht befüllt sind, werden zufällig befüllt
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
return addedWords;
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const 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++)
|
|
{
|
|
printf("%c", salad[i][j]);//Ausgabe des Arrays auf die Konsole
|
|
}
|
|
}
|
|
} |