generated from freudenreichan/info2Praktikum-Wortsalat
102 lines
3.6 KiB
C
102 lines
3.6 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
|
|
|
|
|
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
//salad = Array das als Tabelle dient; searchFieldLEN = tatsächliche Länge des Spielfelds; words = Liste der möglichen Suchwörter; wordCount = Anzahl der Wörter in words
|
|
{
|
|
//1. Spielfeld salad leeren; (Leeres Feld erstellen)
|
|
srand(time(NULL));
|
|
for (unsigned int breite = 0; breite < searchFieldLen; breite++)
|
|
{
|
|
for (unsigned int hoehe = 0; hoehe < searchFieldLen; hoehe++)
|
|
{
|
|
salad[hoehe][breite] = EMPTY_CHAR;
|
|
}
|
|
}
|
|
//2. Wortliste word durchgehen; (Wortlängen bestimmen)
|
|
int plaziert = 0; //Wort plaziert?;
|
|
for (unsigned i = 0; i < wordCount; i++)
|
|
{
|
|
unsigned int wortlaenge = strlen(words[i]); //Strlen gibt die länge der Zeichenkette an
|
|
if (wortlaenge == 0) //Leere Listenplätze werden Übersprungen
|
|
{
|
|
continue;
|
|
}
|
|
if (wortlaenge > searchFieldLen) //Falls das Wort zu lange ist Überspringe
|
|
{
|
|
continue;;
|
|
}
|
|
|
|
//3. Startpunkt für Wörter festlegen (Platzierung der Wörter)
|
|
for (unsigned int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++)
|
|
{
|
|
unsigned int x_position = rand() % searchFieldLen; //Startpunkt in x Richtung wählen
|
|
unsigned int y_position = rand() % searchFieldLen; //Startpunkt in y Richtung wählen
|
|
int richtung = rand() % 2; //Richtung festlegen; 0 = waagerecht; 1 = senkrecht
|
|
if (richtung == 0 && wortlaenge > searchFieldLen - x_position) //Waagerecht Platz prüfen
|
|
{
|
|
continue;
|
|
}
|
|
if (richtung == 1 && wortlaenge > searchFieldLen - y_position) //Senkrecht Platz prüfen
|
|
{
|
|
continue;
|
|
}
|
|
int check = 0; //Wort passt an die ausgewählte Position?; 1 = fail
|
|
for (unsigned int j = 0; j < wortlaenge; j++) //Felder auf leere prüfen
|
|
{
|
|
unsigned int x_positionrest = x_position;
|
|
unsigned int y_positionrest = y_position;
|
|
if (richtung == 0)
|
|
{
|
|
x_positionrest += j;
|
|
}
|
|
else
|
|
{
|
|
y_positionrest += j;
|
|
}
|
|
if (salad[y_positionrest][x_positionrest] != EMPTY_CHAR)
|
|
{
|
|
check = 1;
|
|
break;
|
|
}
|
|
}
|
|
//4. Eingabe der Wörter; (Wörter eintragen)
|
|
if (!check)
|
|
{
|
|
for (unsigned int j = 0; j < wortlaenge; j++)
|
|
{
|
|
if(richtung == 0)
|
|
{
|
|
salad[y_position][x_position + j] = words[i][j];
|
|
}
|
|
else
|
|
{
|
|
salad[y_position + j][x_position] = words[i][j];
|
|
}
|
|
}
|
|
plaziert++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//5. Freie Felder mit Zufallsbuchstaben befüllen; (leere Felder Füllen)
|
|
for (unsigned int x_position = 0; x_position < searchFieldLen; x_position++)
|
|
{
|
|
for (unsigned int y_position = 0; y_position < searchFieldLen; y_position++)
|
|
{
|
|
if (salad[y_position][x_position] == EMPTY_CHAR)
|
|
{
|
|
salad[y_position][x_position] = 'A' + rand() % 26;
|
|
}
|
|
}
|
|
}
|
|
//6. Endergebnis zurückgeben; (Rückgabewert)
|
|
return plaziert;
|
|
}
|