forked from freudenreichan/info2Praktikum-Wortsalat
clean Code
This commit is contained in:
1 parent
add999961f
commit
9f17181f9e
1 file changed
+21
-33
+21
-33
@@ -1,12 +1,10 @@
|
|||||||
#include "game.h"
|
|
||||||
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_ATTEMPTS 100
|
#define MAX_ATTEMPTS 10
|
||||||
#define EMPTY_CHAR '\0'
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
@@ -21,15 +19,7 @@ int createWordSalad(
|
|||||||
const char words[][MAX_WORD_LEN],
|
const char words[][MAX_WORD_LEN],
|
||||||
unsigned int wordCount)
|
unsigned int wordCount)
|
||||||
{
|
{
|
||||||
/* Spielfeldgroesse pruefen */
|
|
||||||
if (searchFieldLen == 0 ||
|
|
||||||
searchFieldLen > MAX_SEARCH_FIELD_LEN)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int placedWords = 0;
|
int placedWords = 0;
|
||||||
|
|
||||||
/* Alle Felder initialisieren */
|
/* Alle Felder initialisieren */
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
@@ -39,16 +29,16 @@ int createWordSalad(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Richtungsvektoren */
|
// Richtungsvektoren
|
||||||
int dx[] = {1, 0};
|
int dx[] = {1, 0};
|
||||||
int dy[] = {0, 1};
|
int dy[] = {0, 1};
|
||||||
|
|
||||||
/* Alle Woerter durchlaufen */
|
// Alle Woerter durchlaufen
|
||||||
for (unsigned int w = 0; w < wordCount; w++)
|
for (unsigned int w = 0; w < wordCount; w++)
|
||||||
{
|
{
|
||||||
size_t wordLen = strlen(words[w]);
|
size_t wordLen = strlen(words[w]);
|
||||||
|
|
||||||
/* Leere oder zu lange Woerter ueberspringen */
|
//Leere oder zu lange Woerter ueberspringen
|
||||||
if (wordLen == 0 || wordLen > searchFieldLen)
|
if (wordLen == 0 || wordLen > searchFieldLen)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@@ -64,31 +54,30 @@ int createWordSalad(
|
|||||||
/* Zufallsrichtung bestimmen */
|
/* Zufallsrichtung bestimmen */
|
||||||
Direction dir = rand() % 2;
|
Direction dir = rand() % 2;
|
||||||
|
|
||||||
unsigned int row;
|
unsigned int Zeile;
|
||||||
unsigned int col;
|
unsigned int Spalte;
|
||||||
|
|
||||||
/* Zufallsposition so waehlen, dass das Wort hineinpasst */
|
/* Zufallsposition so waehlen, dass das Wort hineinpasst */
|
||||||
if (dir == HORIZONTAL)
|
if (dir == HORIZONTAL)
|
||||||
{
|
{
|
||||||
row = rand() % searchFieldLen;
|
Zeile = rand() % searchFieldLen;
|
||||||
col = rand() % (searchFieldLen - wordLen + 1);
|
Spalte = rand() % (searchFieldLen - wordLen + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
row = rand() % (searchFieldLen - wordLen + 1);
|
Zeile = rand() % (searchFieldLen - wordLen + 1);
|
||||||
col = rand() % searchFieldLen;
|
Spalte = rand() % searchFieldLen;
|
||||||
}
|
}
|
||||||
|
|
||||||
int collision = 0;
|
int collision = 0;
|
||||||
|
|
||||||
/* Pruefen, ob die Position geeignet ist */
|
// Pruefen, ob die Position geeignet ist (cillision mit anderen Wörtern)
|
||||||
for (size_t i = 0; i < wordLen; i++)
|
for (size_t i = 0; i < wordLen; i++)
|
||||||
{
|
{
|
||||||
unsigned int z = row + dy[dir] * i;
|
unsigned int z = Zeile + dy[dir] * i;
|
||||||
unsigned int s = col + dx[dir] * i;
|
unsigned int s = Spalte + dx[dir] * i;
|
||||||
|
|
||||||
char letter =
|
char letter = words[w][i];
|
||||||
(char)toupper((unsigned char)words[w][i]);
|
|
||||||
|
|
||||||
if (salad[z][s] != EMPTY_CHAR &&
|
if (salad[z][s] != EMPTY_CHAR &&
|
||||||
salad[z][s] != letter)
|
salad[z][s] != letter)
|
||||||
@@ -98,16 +87,15 @@ int createWordSalad(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Wenn keine Kollision besteht, Wort eintragen */
|
// Wenn keine collision besteht wort eintragen
|
||||||
if (!collision)
|
if (!collision)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < wordLen; i++)
|
for (size_t i = 0; i < wordLen; i++)
|
||||||
{
|
{
|
||||||
unsigned int z = row + dy[dir] * i;
|
unsigned int z = Zeile + dy[dir] * i;
|
||||||
unsigned int s = col + dx[dir] * i;
|
unsigned int s = Spalte + dx[dir] * i;
|
||||||
|
|
||||||
salad[z][s] =
|
salad[z][s] = words[w][i];
|
||||||
(char)toupper((unsigned char)words[w][i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
placed = 1;
|
placed = 1;
|
||||||
@@ -116,14 +104,14 @@ int createWordSalad(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Leere Felder mit zufaelligen Buchstaben fuellen */
|
// Leere Felder mit zufaelligen Buchstaben fuellen
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++)
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
{
|
{
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++)
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
{
|
{
|
||||||
if (salad[i][j] == EMPTY_CHAR)
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
{
|
{
|
||||||
salad[i][j] = (char)('A' + rand() % 26);
|
salad[i][j] ='A' + (rand() % 26);// 26 Buchstaben insgesammt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user