generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
2 Commits
538528e5ec
...
76ecf700bb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76ecf700bb | ||
|
|
db3fffe3f5 |
@ -32,12 +32,14 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
}
|
||||
|
||||
int placedWrdsAmnt = 0;
|
||||
|
||||
//wörter platzieren, 1 schleife = 1 wort
|
||||
for(int w = 0; w < wordCount; w++){
|
||||
const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile)
|
||||
int len = strlen(word);
|
||||
|
||||
if(len > searchFieldLen) // wenn wort zu lang, nimm nächstes wort
|
||||
if(len > searchFieldLen ) // wenn wort zu lang, nimm nächstes wort
|
||||
continue;
|
||||
|
||||
int placed = 0;
|
||||
@ -78,6 +80,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
}
|
||||
placed = 1;
|
||||
}
|
||||
placedWrdsAmnt++;
|
||||
}
|
||||
//leere felder mit random buchstaben füllen
|
||||
for(int y = 0; y < searchFieldLen; y++){
|
||||
@ -86,7 +89,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
||||
salad[y][x] = (rand() % 26) + 'A';
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
return placedWrdsAmnt;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
|
||||
BIN
Start_Mac/game.o
BIN
Start_Mac/game.o
Binary file not shown.
Binary file not shown.
@ -7,23 +7,32 @@
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int anzahlWoerter = 0;
|
||||
char zeile[500];
|
||||
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) //words[zeilen also wörter][spalten also anzahl]
|
||||
{
|
||||
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
|
||||
int anzahlWoerter = 0;
|
||||
|
||||
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ // eine zeile
|
||||
char *wort = strtok(zeile, ",; \n");
|
||||
|
||||
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
||||
if(strlen(wort) == 0){
|
||||
wort = strtok(NULL, ",; \n");
|
||||
continue;
|
||||
}
|
||||
|
||||
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN -1);
|
||||
words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0';
|
||||
|
||||
for(int i = 0; words[anzahlWoerter][i]; i++){
|
||||
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //jedes wort das eingelesen wurde wird komplett gross geschrieben, unsigned damit toupper kein neg. arg bekommt (zb umlaute)
|
||||
}
|
||||
|
||||
anzahlWoerter++;
|
||||
wort = strtok(NULL, ",; \n");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
return anzahlWoerter;
|
||||
}
|
||||
Binary file not shown.
@ -42,7 +42,7 @@ int main(int argc, char *argv[])
|
||||
// error message if some words couldn't be placed
|
||||
int windowWidth = 880;
|
||||
|
||||
if(placedWords == 1){
|
||||
if(placedWords == wordCount){
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
|
||||
}else{
|
||||
printf("Fehler: Nicht alle Wörter konnten platziert werden.\n");
|
||||
|
||||
BIN
Start_Mac/main.o
BIN
Start_Mac/main.o
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user