forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
10
Commits
84e854fdc2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5d6a5d1b3 | ||
|
|
a0bd61a872 | ||
|
|
556a83c9af | ||
|
|
d2357c9040 | ||
|
|
69d0c0ee4d | ||
|
|
075a2ad50c | ||
|
|
b0b70e371d | ||
|
|
7df641beb3 | ||
|
|
88575ba8bf | ||
|
|
f1fdbb5424 |
No files matched your search
+71
-9
@@ -11,18 +11,80 @@
|
||||
/* * 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)
|
||||
{
|
||||
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 placedWords = 0;
|
||||
|
||||
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 zufall = bustaben[rand() % 26];
|
||||
|
||||
printf("Zufaelliger Bustabe: %c\n", zufall);
|
||||
|
||||
const char words[15][MAX_WORD_LEN] = "Yeti,Nessie,Werwolf,Vampir,Monster,Hydra,Frankenstein,Dracula,KingKong,Gremlin,Kobold,Hexe,Poltergeist,Gespenst,Oge";
|
||||
|
||||
|
||||
for (unsigned int r=0; r < searchFieldLen; r++) {
|
||||
for (unsigned int c =0; c <searchFieldLen; c++) {
|
||||
salad[r][c] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
//Wörter plazieren
|
||||
for (unsigned int w = 0; w< wordCount; w++) {
|
||||
int wordLen = strlen(words[w]);
|
||||
if (wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int tries = 0;
|
||||
int placed = 0;
|
||||
while (!placed && tries < MAX_RAND_TRIES_PER_WORD) {
|
||||
int direction = rand() % 2;
|
||||
int row;
|
||||
int col;
|
||||
int fits = 1;
|
||||
if (direction == 0) {
|
||||
row = rand() % searchFieldLen;
|
||||
col = rand() % (searchFieldLen - wordLen +1);
|
||||
for (int i = 0; i< wordLen; i++) {
|
||||
if (salad[row][col +i] != EMPTY_CHAR) {
|
||||
fits = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fits) {
|
||||
for (int i=0; i < wordLen; i++) {
|
||||
salad[row][col +i] = words[w][i];
|
||||
}
|
||||
placed = 1;
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
else {
|
||||
row = rand() % (searchFieldLen - wordLen +1);
|
||||
col = rand() % searchFieldLen;
|
||||
for (int i = 0; i< wordLen; i++) {
|
||||
if (salad[row+i][col] != EMPTY_CHAR) {
|
||||
fits = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (fits) {
|
||||
for (int i = 0; i<wordLen; i++) {
|
||||
salad[row+i][col] = words[w][i];
|
||||
}
|
||||
placed = 1;
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
tries++;
|
||||
}
|
||||
}
|
||||
//Rest mit Zufallsbustaben füllen
|
||||
for (unsigned int r=0; r < searchFieldLen; r++) {
|
||||
for (int c=0; c < searchFieldLen; c++) {
|
||||
if (salad[r][c] == EMPTY_CHAR) {
|
||||
salad[r][c]='A' + rand() % 26;
|
||||
}
|
||||
}
|
||||
}
|
||||
return placedWords;
|
||||
}
|
||||
+5
-1
@@ -40,7 +40,11 @@ int main(int argc, char *argv[])
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
if (placedWords == wordCount){
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||
}else {
|
||||
fprintf(stderr, "Could not open file %s for reading ...\n");
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user