generated from freudenreichan/info2Praktikum-Wortsalat
game.c v1
This commit is contained in:
1 parent
adce61a9f6
commit
b71881192f
1 file changed
+93
-1
+93
-1
@@ -12,6 +12,98 @@
|
||||
|
||||
// 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)
|
||||
//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)
|
||||
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)
|
||||
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 die Wörter zu lange sind gibt es eine Fehlermeldung
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//3. Startpunkt für Wörter festlegen (Platzierung der Wörter)
|
||||
int plaziert = 0; //Wort plaziert?; 1 = fail
|
||||
srand(time(NULL));
|
||||
for (unsigned int versuch = 0; versuch < MAX_RAND_TRIES_PER_WORD; versuch++)
|
||||
{
|
||||
unsigned int x_position = srand() % searchFieldLen; //Startpunkt in x Richtung wählen
|
||||
unsigned int y_position = srand() % searchFieldLen; //Startpunkt in y Richtung wählen
|
||||
int richtung = srand() % 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 = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!plaziert)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//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' + srand() % 26;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//6. Endergebnis zurückgeben; (Rückgabewert)
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user