forked from freudenreichan/info2Praktikum-Wortsalat
Refactor game.c
This commit is contained in:
1 parent
e001da9bd3
commit
e1352870e0
1 file changed
+11
-17
+11
-17
@@ -7,9 +7,9 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
int placeWord(int len, int column, int row,
|
|
||||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
int placeWord(const char* word, int wordLength, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
int searchFieldLen, const char* word, int axis);
|
int searchFieldLen, int column, int row, 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(
|
int createWordSalad(
|
||||||
@@ -48,22 +48,17 @@ int createWordSalad(
|
|||||||
int row = rand() % searchFieldLen;
|
int row = rand() % searchFieldLen;
|
||||||
int column = rand() % searchFieldLen;
|
int column = rand() % searchFieldLen;
|
||||||
|
|
||||||
int success = placeWord(
|
int success = placeWord( words[w],wordlenght, salad, searchFieldLen, column, row, axis);
|
||||||
wordlenght, column, row, salad,
|
|
||||||
searchFieldLen, words[w], axis);
|
|
||||||
|
|
||||||
if (success)
|
if (success)
|
||||||
{
|
{
|
||||||
placedWords++;
|
placedWords++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Platzierung zum %d. Mal fehlgeschlagen!\n",
|
|
||||||
attempts + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Fill empty spaces
|
// Fill empty spaces
|
||||||
for (int r = 0; r < searchFieldLen; r++)
|
for (int r = 0; r < searchFieldLen; r++)
|
||||||
{
|
{
|
||||||
for (int c = 0; c < searchFieldLen; c++)
|
for (int c = 0; c < searchFieldLen; c++)
|
||||||
@@ -79,16 +74,15 @@ int createWordSalad(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int placeWord(int len, int column, int row,
|
int placeWord(const char* word, int wordLength, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
int searchFieldLen, int column, int row, int axis)
|
||||||
int searchFieldLen, const char* word, int axis)
|
|
||||||
{
|
{
|
||||||
// Available space check & word collision avoidance
|
// Available space check & word collision avoidance
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < wordLength; i++)
|
||||||
{
|
{
|
||||||
if (axis == 0) // Horizontal
|
if (axis == 0) // Horizontal
|
||||||
{
|
{
|
||||||
if (column + len > searchFieldLen ||
|
if (column + wordLength > searchFieldLen ||
|
||||||
salad[row][column + i] != EMPTY_CHAR)
|
salad[row][column + i] != EMPTY_CHAR)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@@ -96,7 +90,7 @@ int placeWord(int len, int column, int row,
|
|||||||
}
|
}
|
||||||
else // Vertical
|
else // Vertical
|
||||||
{
|
{
|
||||||
if (row + len > searchFieldLen ||
|
if (row + wordLength > searchFieldLen ||
|
||||||
salad[row + i][column] != EMPTY_CHAR)
|
salad[row + i][column] != EMPTY_CHAR)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@@ -105,7 +99,7 @@ int placeWord(int len, int column, int row,
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Write Word
|
//Write Word
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < wordLength; i++)
|
||||||
{
|
{
|
||||||
if (axis == 0) // Horizontal
|
if (axis == 0) // Horizontal
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user