forked from freudenreichan/info2Praktikum-Wortsalat
game.c bearbeitet
This commit is contained in:
1 parent
84e854fdc2
commit
f1fdbb5424
1 file changed
+71
-1
+71
-1
@@ -11,10 +11,13 @@
|
|||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
|
int clearField();
|
||||||
|
int placeWords();
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
/*srand(time(NULL));
|
||||||
char bustaben[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
|
char bustaben[] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
|
||||||
char zufall = bustaben[rand() % 26];
|
char zufall = bustaben[rand() % 26];
|
||||||
|
|
||||||
@@ -24,5 +27,72 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0; */
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
clearField[ROWS, COLS];
|
||||||
|
placeWords();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int clearField() {
|
||||||
|
for (int r=0; r < ROWS; r++) {
|
||||||
|
for (int c =0; c < COLS; c++) {
|
||||||
|
grid[r][c] = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int placeWords() {
|
||||||
|
//Wörter plazieren
|
||||||
|
for (unsigned int w = 0; w< wordCount; w++) {
|
||||||
|
int wordLen = strlen(words[w]);
|
||||||
|
int placed = 0;
|
||||||
|
while (!placed) {
|
||||||
|
int direction = rand() % 2;
|
||||||
|
int row;
|
||||||
|
int col;
|
||||||
|
int fits = 1;
|
||||||
|
if (direction == 0) {
|
||||||
|
row = rand() % ROWS;
|
||||||
|
col = rand() % (COLS - wordLen +1);
|
||||||
|
for (int i = 0; i< wordLen; i++) {
|
||||||
|
if (grid[row][col +i] != '.') {
|
||||||
|
fits = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fits) {
|
||||||
|
for (int i=0; i < wordLen; i++) {
|
||||||
|
grid[row][col +i] = words[w][i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
row = rand() % (ROWS - wordLen +1);
|
||||||
|
col = rand() % COLS;
|
||||||
|
for (int i = 0; i< wordLen; i++) {
|
||||||
|
if (grid[row+i][col] != '.') {
|
||||||
|
fits = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fits) {
|
||||||
|
for (int i = 0; i<worldLen; i++) {
|
||||||
|
grid[row+i][col] = words[w][i];
|
||||||
|
}
|
||||||
|
placed =1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Rest mit Zufallszahlen füllen
|
||||||
|
for (int r=0; r < ROWS; r++) {
|
||||||
|
for (int c=0; c < COLS; c++) {
|
||||||
|
if (grid[r][c] == '.') {
|
||||||
|
grid[r][c]='A' + rand() % 26;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user