generated from freudenreichan/info2Praktikum-Wortsalat
Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/eckardtni105737/Info2PrWortsalat
This commit is contained in:
commit
221e0e7168
4 files changed
+64
-8
No files matched your search
+40
-2
@@ -2,11 +2,49 @@
|
||||
#include <string.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 a single word from file and store it in buffer.
|
||||
Returns the length of the word, or 0 if no more words are available (EOF).
|
||||
*/
|
||||
static int readSingleWord(FILE *file, char buffer[MAX_WORD_LEN])
|
||||
{
|
||||
int len = 0;
|
||||
int c;
|
||||
|
||||
//clear leading seperators
|
||||
while((c = fgetc(file)) != EOF && (c == ',' || c == ';' || c == ' ' || c == '\n')) {}
|
||||
|
||||
//check for empty file
|
||||
if(c == EOF)
|
||||
return 0;
|
||||
|
||||
|
||||
//read word
|
||||
do
|
||||
{
|
||||
if(len < MAX_WORD_LEN - 1) // -1 to leave space for '\0'
|
||||
{
|
||||
buffer[len] = toupper((unsigned char)c);
|
||||
len++;
|
||||
}
|
||||
c = fgetc(file);
|
||||
}
|
||||
while(c != EOF && !(c == ',' || c == ';' || c == ' ' || c == '\n'));
|
||||
|
||||
//append string end-delimiter
|
||||
buffer[len] = '\0'; // '\0' marks the end of a string, we need to apend it for strcpy
|
||||
return len;
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char buffer[MAX_WORD_LEN];
|
||||
unsigned int wordCount = 0;
|
||||
int len;
|
||||
|
||||
while(wordCount < maxWordCount && (len = readSingleWord(file, buffer)) > 0)
|
||||
strcpy(words[wordCount++], buffer);
|
||||
|
||||
return wordCount;
|
||||
}
|
||||
+10
-5
@@ -35,11 +35,16 @@ int main(int argc, char *argv[])
|
||||
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// Start the game if successful
|
||||
// error message if some words couldn't be placed
|
||||
|
||||
if (wordCount == placedWords)
|
||||
{
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Not all words could be placed in the grid.\nOnly %d of %d could be placed", placedWords, wordCount);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
+11
-1
@@ -22,6 +22,16 @@ codefolder = .
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
# --------------------------
|
||||
# Zwischenstände:
|
||||
# --------------------------
|
||||
# Only main
|
||||
wordsalad_mymain: main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_mymain main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
# Main + Input
|
||||
wordsalad_myinput: main.o input.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o wordsalad_myinput main.o input.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
@@ -55,5 +65,5 @@ clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
del /f *.o *.exe
|
||||
else
|
||||
rm -f *.o wordsalad $(TEST_BIN)
|
||||
rm -f *.o wordsalad wordsalad_mymain wordsalad_myinput $(TEST_BIN)
|
||||
endif
|
||||
Reference in new issue
Block a user