forked from freudenreichan/info2Praktikum-Wortsalat
game.c hinzugefuegt, Spiellogik implementiert
This commit is contained in:
1 parent
b3d425aeb8
commit
ee9b83a82f
2 files changed
+97
No files matched your search
@@ -13,5 +13,102 @@
|
||||
// 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)
|
||||
{
|
||||
srand(time(NULL));
|
||||
/***********************************************************************************
|
||||
Salat initialisieren
|
||||
***********************************************************************************/
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
salad[i][j] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************
|
||||
Wörter im Feld platzieren
|
||||
***********************************************************************************/
|
||||
int placed_counter = 0;
|
||||
int x_pos, y_pos;
|
||||
int richtung; //horizontal: 0, vertikal: 1
|
||||
|
||||
for(int i = 0;i < wordCount; i++) //Schleife um nacheinander durch alle Wörter zu gehen
|
||||
{
|
||||
int is_valid = 0;
|
||||
int counter = 0;
|
||||
int laenge = strlen(words[i]);
|
||||
|
||||
richtung = rand() % 2; //Richtung zufällig bestimmen
|
||||
|
||||
if(richtung == 0) //Richtung: horizontal
|
||||
{
|
||||
while(is_valid == 0 && counter < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
counter++;
|
||||
is_valid = 1;
|
||||
y_pos = rand() % (MAX_SEARCH_FIELD_LEN ); //y-Koordinate bei horizontal egal
|
||||
x_pos = rand() % (MAX_SEARCH_FIELD_LEN - laenge + 1); //x-Koordinate bei horizontal nur in dem Bereich, in den das Wort auch reinpasst
|
||||
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu prüfen
|
||||
{
|
||||
if(salad[x_pos + j][y_pos] != '\0') //überprüfen ob Feld schon voll ist
|
||||
{
|
||||
is_valid = 0;
|
||||
}
|
||||
}
|
||||
if(is_valid == 1)
|
||||
{
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu setzen
|
||||
{
|
||||
salad[x_pos + j][y_pos] = words[i][j]; //horizontal: y-Koordinate fest, x-Koordinate erhöht sich pro Durchlauf um 1
|
||||
}
|
||||
placed_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(richtung == 1) //Richtung: vertikal
|
||||
{
|
||||
while(is_valid == 0 && counter < MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
counter++;
|
||||
is_valid = 1;
|
||||
x_pos = rand() % (MAX_SEARCH_FIELD_LEN); //x-Koordinate bei vertikal egal
|
||||
y_pos = rand() % (MAX_SEARCH_FIELD_LEN - laenge + 1); //y-Koordinate bei vertikal nur in dem Bereich, in den das Wort auch reinpasst
|
||||
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu prüfen
|
||||
{
|
||||
if(salad[x_pos][y_pos + j] != '\0') //überprüfen ob Feld schon voll ist
|
||||
{
|
||||
is_valid = 0;
|
||||
}
|
||||
}
|
||||
if(is_valid == 1)
|
||||
{
|
||||
for(int j = 0; j < laenge; j++) //Schleife um Buchstaben ab Startposition zu setzen
|
||||
{
|
||||
salad[x_pos][y_pos + j] = words[i][j]; //vertikal: x-Koordinate fest, y-Koordinate erhöht sich pro Durchlauf um 1
|
||||
}
|
||||
placed_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************************
|
||||
übrige leere Felder mit zufälligen Buchstaben füllen
|
||||
***********************************************************************************/
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
if(salad[i][j] == '\0')
|
||||
{
|
||||
salad[i][j] = 'A' + rand() % 26;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placed_counter;
|
||||
}
|
||||
Reference in new issue
Block a user