forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
11
Commits
1725da2148
...
final
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
74b732a351 | ||
|
|
ed3b2e797c | ||
|
|
24d344b17e | ||
|
|
d505f80682 | ||
|
|
0e217a5e68 | ||
|
|
c5837d86ad | ||
|
|
e2df312438 | ||
|
|
f8deef43e1 | ||
|
|
a1a0b615a7 | ||
|
|
60bb394fb7 | ||
|
|
d7c7fe76f3 |
No files matched your search
@@ -0,0 +1,14 @@
|
|||||||
|
# Wortsalat Projekt
|
||||||
|
|
||||||
|
Ein GeneDas Programm liest eine Liste von Wörtern aus einer übergebenen Textdatei (z.B. `words.txt`), versteckt diese zufällig in einem Raster und füllt die restlichen leeren Felder mit zufälligen Buchstaben auf, sodass ein Wortsuchrätsel entsteht.
|
||||||
|
|
||||||
|
Code frisch bauen
|
||||||
|
make clean
|
||||||
|
make wordsalad_myversion
|
||||||
|
|
||||||
|
Das Spiel starten
|
||||||
|
./wordsalad_myversion words.txt
|
||||||
|
|
||||||
|
Uni-Tests ausführen
|
||||||
|
make test
|
||||||
|
./runTests
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "game.h"
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
|
||||||
|
|
||||||
const char words[][MAX_WORD_LEN] = {
|
|
||||||
"HALLO",
|
|
||||||
"WELT",
|
|
||||||
"AUTO",
|
|
||||||
"BIRNE",
|
|
||||||
"APFEL",
|
|
||||||
"BANANE",
|
|
||||||
"LANGESWORT",
|
|
||||||
"REITSPORT",
|
|
||||||
"APFELBAUM",
|
|
||||||
"DRACULA",
|
|
||||||
"LAPTOP",
|
|
||||||
"BILDSCHRIM",
|
|
||||||
"HAUS"
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
|
|
||||||
|
|
||||||
int placedWords = createWordSalad(salad, MAX_SEARCH_FIELD_LEN, words, wordCount);
|
|
||||||
|
|
||||||
printf("Placed words: %d\n", placedWords);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,64 @@
|
|||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// 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
|
||||||
|
|
||||||
|
// Liest Wörter aus einer Textdatei und speichert sie im 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)
|
||||||
{
|
{
|
||||||
|
// Prüft, ob die Eingaben gültig sind.
|
||||||
|
if (file == NULL || words == NULL || maxWordCount == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int count = 0;
|
||||||
|
char currentWord[MAX_WORD_LEN];
|
||||||
|
size_t length = 0;
|
||||||
|
int tooLong = 0;
|
||||||
|
int ch;
|
||||||
|
|
||||||
|
while (count < maxWordCount && (ch = fgetc(file)) != EOF) {
|
||||||
|
// fgetc(file) liest jeweils ein Zeichen aus der Datei.
|
||||||
|
|
||||||
|
if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) {
|
||||||
|
// isspace() erkennt Leerraumzeichen wie Leerzeichen und Zeilenumbrüche. Kommas und Semikolons werden separat geprüft.
|
||||||
|
|
||||||
|
// Speichert das Wort, wenn es nicht leer und nicht zu lang ist.
|
||||||
|
if (length > 0 && !tooLong) {
|
||||||
|
currentWord[length] = '\0';
|
||||||
|
strncpy(words[count], currentWord, MAX_WORD_LEN);
|
||||||
|
// strncpy() kopiert das fertige Wort mit einer festgelegten maximalen Länge in das Zielarray.
|
||||||
|
count++; // Erhöht die Anzahl der gespeicherten Wörter.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setzt die Variablen für das nächste Wort zurück.
|
||||||
|
length = 0;
|
||||||
|
tooLong = 0;
|
||||||
|
}
|
||||||
|
else if (!tooLong) {
|
||||||
|
|
||||||
|
// Prüft, ob noch Platz für das Zeichen und '\0' vorhanden ist.
|
||||||
|
if (length < MAX_WORD_LEN - 1) {
|
||||||
|
// toupper() wandelt Kleinbuchstaben in Großbuchstaben um.
|
||||||
|
// MAX_WORD_LEN - 1 Lässt Platz für '\0', das das Ende eines C-Strings kennzeichnet und verhindert, dass über den Wortpuffer hinausgeschrieben wird.
|
||||||
|
currentWord[length] = (char)toupper((unsigned char)ch);
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Überspringt das Wort, wenn es zu lang ist.
|
||||||
|
tooLong = 1;
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speichert das letzte Wort, falls die Datei ohne Trennzeichen endet.
|
||||||
|
if (count < maxWordCount && length > 0 && !tooLong) {
|
||||||
|
currentWord[length] = '\0';
|
||||||
|
strncpy(words[count], currentWord, MAX_WORD_LEN);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gibt die Anzahl der gespeicherten Wörter zurück.
|
||||||
|
return (int)count;
|
||||||
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -43,8 +43,8 @@ graphicalGame.o: $(codefolder)/graphicalGame.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# mein Spiel bauen
|
# mein Spiel bauen
|
||||||
#---------------------------
|
#---------------------------
|
||||||
wordsalad_myversion: main.o
|
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
Reference in new issue
Block a user