forked from freudenreichan/info2Praktikum-Wortsalat
small changes, works now
This commit is contained in:
1 parent
4f43ad6429
commit
e001da9bd3
1 file changed
+75
-63
+75
-63
@@ -7,91 +7,60 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
int placeWord(int len, int column, int row,
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
int searchFieldLen, const char* word, int axis);
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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 createWordSalad(
|
||||||
|
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
|
unsigned int searchFieldLen,
|
||||||
|
const char words[][MAX_WORD_LEN],
|
||||||
|
unsigned int wordCount)
|
||||||
{
|
{
|
||||||
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN)
|
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN)
|
||||||
{return 0;}
|
return 0;
|
||||||
|
|
||||||
int placedWOrds = 0;
|
int placedWords = 0;
|
||||||
srand((unsigned int)time(NULL));
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
// Spielfeld leeren:
|
// Spielfeld leeren:
|
||||||
for (int r = 0; r < searchFieldLen; r++ )
|
for (int x = 0; x < searchFieldLen; x++)
|
||||||
{
|
for (int y = 0; y < searchFieldLen; y++)
|
||||||
for (int c = 0; c < searchFieldLen; c++) {
|
salad[x][y] = EMPTY_CHAR;
|
||||||
salad[r][c] = EMPTY_CHAR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wörter platzieren
|
// Wörter platzieren
|
||||||
for (int w = 0; w < wordCount; w++)
|
for (int w = 0; w < wordCount; w++)
|
||||||
{
|
{
|
||||||
int len = strlen(words[w]); //Wortlänge festgelegt
|
int wordlenght = strlen(words[w]); // Wortlänge festgelegt
|
||||||
|
|
||||||
for (int atempts = 0; atempts < MAX_RAND_TRIES_PER_WORD; atempts++)
|
if (wordlenght == 0 || wordlenght > searchFieldLen)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int attempts = 0;
|
||||||
|
attempts < MAX_RAND_TRIES_PER_WORD;
|
||||||
|
attempts++)
|
||||||
{
|
{
|
||||||
// Muss in for-Schleife sein damit immer neu erzeugt wird
|
// Muss in for-Schleife sein damit immer neu erzeugt wird
|
||||||
int axis = rand() % 2;
|
int axis = rand() % 2;
|
||||||
int row = rand() % searchFieldLen;
|
int row = rand() % searchFieldLen;
|
||||||
int column = rand() % searchFieldLen;
|
int column = rand() % searchFieldLen;
|
||||||
int error = 0;
|
|
||||||
|
|
||||||
|
int success = placeWord(
|
||||||
|
wordlenght, column, row, salad,
|
||||||
|
searchFieldLen, words[w], axis);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
|
||||||
if (axis == 0) //Vertical~~~~~~~~~~~
|
|
||||||
{
|
{
|
||||||
//Available Space Check & Wordcollision Avoidance//////
|
placedWords++;
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (len + row > searchFieldLen || salad[row+i][column] != EMPTY_CHAR){
|
|
||||||
error = 1;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Platzierung zum %d. Mal fehlgeschlagen!\n",
|
||||||
|
attempts + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Write Word
|
|
||||||
if (error == 0){
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
salad[row+i][column] = words[w][i];
|
|
||||||
}
|
|
||||||
placedWOrds++;
|
|
||||||
break;
|
|
||||||
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}else if (axis == 1){ //Horizontal~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
//Available Space Check & Wordcollision Avoidance
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (len + column > searchFieldLen || salad[row][column+i] != EMPTY_CHAR){
|
|
||||||
error = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Write Word
|
|
||||||
if (error == 0){
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
salad[row][column+i] = words[w][i];
|
|
||||||
}
|
|
||||||
placedWOrds++;
|
|
||||||
break;
|
|
||||||
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Fill empty spaces
|
//Fill empty spaces
|
||||||
@@ -105,5 +74,48 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return placedWOrds;
|
printf("%d placed" ,placedWords);
|
||||||
|
return placedWords;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int placeWord(int len, int column, int row,
|
||||||
|
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
|
int searchFieldLen, const char* word, int axis)
|
||||||
|
{
|
||||||
|
// Available space check & word collision avoidance
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (axis == 0) // Horizontal
|
||||||
|
{
|
||||||
|
if (column + len > searchFieldLen ||
|
||||||
|
salad[row][column + i] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Vertical
|
||||||
|
{
|
||||||
|
if (row + len > searchFieldLen ||
|
||||||
|
salad[row + i][column] != EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Write Word
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (axis == 0) // Horizontal
|
||||||
|
{
|
||||||
|
salad[row][column + i] = word[i];
|
||||||
|
}
|
||||||
|
else // Vertical
|
||||||
|
{
|
||||||
|
salad[row + i][column] = word[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user