forked from freudenreichan/info2Praktikum-Wortsalat
Funktioniert
This commit is contained in:
1 parent
6c72054f42
commit
b0951bcb45
3 files changed
+34
-10
No files matched your search
+2
-2
@@ -50,8 +50,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
for(int j = 0; j < strlen(words[i]); j++){
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
salad[startpoint_x + j][startpoint_y] = words[i][j];
|
salad[startpoint_x + j][startpoint_y] = words[i][j];
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
wordsplaced += 1;
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
case 1: //X-Negativ
|
case 1: //X-Negativ
|
||||||
startpoint_x = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1);
|
startpoint_x = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
startpoint_y = rand() % searchFieldLen;
|
startpoint_y = rand() % searchFieldLen;
|
||||||
@@ -70,8 +70,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
|
|||||||
for(int j = 0; j < strlen(words[i]); j++){
|
for(int j = 0; j < strlen(words[i]); j++){
|
||||||
salad[startpoint_x - j][startpoint_y] = words[i][j];
|
salad[startpoint_x - j][startpoint_y] = words[i][j];
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
wordsplaced += 1;
|
wordsplaced += 1;
|
||||||
|
break;
|
||||||
case 2: //Y-Positiv
|
case 2: //Y-Positiv
|
||||||
startpoint_x = rand() % searchFieldLen;
|
startpoint_x = rand() % searchFieldLen;
|
||||||
startpoint_y = rand() % (searchFieldLen - strlen(words[i]) + 1);
|
startpoint_y = rand() % (searchFieldLen - strlen(words[i]) + 1);
|
||||||
|
|||||||
+29
-7
@@ -2,19 +2,41 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// 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
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
|
int wordCount = 0;
|
||||||
|
int charCount = 0;
|
||||||
|
int c;
|
||||||
|
|
||||||
|
while ((c = fgetc(file)) != EOF) {
|
||||||
|
|
||||||
unsigned int wordCount = 0;
|
if (c == ',' || c == ';' || c == '\n' || c == '\r') {
|
||||||
while(fgets(words[wordCount],MAX_WORD_LEN,file) != NULL && wordCount+1 < maxWordCount){
|
|
||||||
|
if (charCount > 0) {
|
||||||
|
words[wordCount][charCount] = '\0';
|
||||||
|
wordCount++;
|
||||||
|
charCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wordCount >= maxWordCount) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == ' ' || c == '\t') {
|
||||||
|
continue; // Leerzeichen und Tabs überspringen
|
||||||
|
}
|
||||||
|
else if (charCount < MAX_WORD_LEN - 1) {
|
||||||
|
words[wordCount][charCount++] = (char)toupper((unsigned char)c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (charCount > 0 && wordCount < maxWordCount) {
|
||||||
|
words[wordCount][charCount] = '\0';
|
||||||
wordCount++;
|
wordCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
return wordCount;
|
return wordCount;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+3
-1
@@ -43,10 +43,12 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if(placedWords==wordCount){
|
if(placedWords==wordCount){
|
||||||
//start game
|
//start game
|
||||||
startGame(wordSalad, SALAD_SIZE, words, placedWords, SALAD_SIZE);
|
startGame(wordSalad, SALAD_SIZE, words, placedWords, 1000);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("Error! Words coundn't be placed...\n");
|
printf("Error! Words coundn't be placed...\n");
|
||||||
|
printf("%d\n", placedWords);
|
||||||
|
printf("%d\n", wordCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user