forked from freudenreichan/info2Praktikum-Wortsalat
115 lines
4.1 KiB
C
115 lines
4.1 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)
|
|
{
|
|
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;
|
|
}
|