100 lines
3.2 KiB
C
100 lines
3.2 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 row, col, placedWords=0;
|
|
|
|
//Gesamtes Feld auf 0 setzen
|
|
for (row=0; row < searchFieldLen; row++) {
|
|
for (col=0; col < searchFieldLen; col++) {
|
|
salad [row][col] = EMPTY_CHAR;
|
|
}
|
|
}
|
|
|
|
//Wortlänge wird für jedes Wort erfasst
|
|
for (int w=0; w< wordCount; w++) {
|
|
const char *word = words[w];
|
|
int wordLen = strlen(word);
|
|
if (wordLen == 0 || wordLen > searchFieldLen) {
|
|
continue;
|
|
}
|
|
|
|
int placed = 0;
|
|
|
|
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
|
|
int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
|
|
|
|
row = 0;
|
|
col = 0;
|
|
|
|
if (dir == 0) { //horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
|
|
row = rand () % searchFieldLen;
|
|
col = rand () % (searchFieldLen - wordLen + 1);
|
|
} else { //vertikal
|
|
row = rand () % (searchFieldLen - wordLen + 1);
|
|
col = rand () % searchFieldLen;
|
|
}
|
|
|
|
int placeable = 1;
|
|
for (int i = 0; i < wordLen; i++) { //Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
|
|
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
|
|
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR)) {
|
|
placeable = 0;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
if (placeable) { //wenn es keine Überschneidungen gibt, wird Wort platziert
|
|
for (int i=0; i<wordLen; i++) {
|
|
if (dir == 0) {
|
|
salad [row][col+i] = word[i];
|
|
} else {
|
|
salad [row+i][col] = word[i];
|
|
}
|
|
}
|
|
placed = 1;
|
|
placedWords++;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
|
|
for (row=0; row < searchFieldLen; row++) {
|
|
for (col=0; col < searchFieldLen; col++) {
|
|
if (salad[row][col] == EMPTY_CHAR) {
|
|
salad [row][col] = 'A' + (rand()% 26);
|
|
}
|
|
}
|
|
}
|
|
//return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
|
|
return placedWords;
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
int row, col;
|
|
|
|
//Ausgabe des gesamten Feldes
|
|
for (row=0; row < searchFieldLen; row++) {
|
|
for (col=0; col < searchFieldLen; col++) {
|
|
printf("%c ", salad[row][col]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|