generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
4
Commits
main
..
aed3cce12e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aed3cce12e | ||
|
|
208e35e934 | ||
|
|
36c61ff8e7 | ||
|
|
42739dc5cc |
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@@ -13,7 +14,44 @@
|
||||
// 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)
|
||||
{
|
||||
// Initialisiere Salad mit EMPTY_CHAR
|
||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// Platziere Wörter (einfache horizontale Platzierung für Tests)
|
||||
unsigned int placedCount = 0;
|
||||
for(unsigned int w = 0; w < wordCount && w < searchFieldLen; w++)
|
||||
{
|
||||
unsigned int wordLen = strlen(words[w]);
|
||||
if(wordLen <= searchFieldLen)
|
||||
{
|
||||
// Platziere Wort horizontal in Zeile w
|
||||
for(unsigned int j = 0; j < wordLen; j++)
|
||||
{
|
||||
salad[w][j] = words[w][j];
|
||||
}
|
||||
placedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// Fülle restliche Felder mit zufälligen Buchstaben
|
||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if(salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedCount;
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
|
||||
Binary file not shown.
@@ -4,9 +4,46 @@
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
char* readWord(FILE *file)
|
||||
{
|
||||
char word[MAX_WORD_LEN]; // Puffer für Wort
|
||||
int index = 0; // Position im Puffer
|
||||
int c; // Variable für jedes gelesene Zeichen
|
||||
|
||||
// Whitespace und Delimiters überspringen
|
||||
while((c = fgetc(file)) != EOF && (isspace(c) || c == ',' || c == ';' || c == '.'));
|
||||
|
||||
// Buchstaben einlesen bis nächstes whitespace/Delimiter/EOF
|
||||
while(c != EOF && !isspace(c) && c != ',' && c != ';' && c != '.' && index < MAX_WORD_LEN - 1) //-1 wegen Nullterminator
|
||||
{
|
||||
word[index++] = (char)toupper(c); // Konvertiere zu Großbuchstaben
|
||||
c = fgetc(file);
|
||||
}
|
||||
word[index] = '\0'; // Nullterminator (= Ende String)
|
||||
|
||||
// Leere überspringen
|
||||
if(index == 0)
|
||||
return NULL;
|
||||
|
||||
return strdup(word); // Rückgabe string, dynamisch allokiert da Zeiger auf lokalen Puffer zurückgegeben
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
unsigned int count = 0;
|
||||
|
||||
while(count < maxWordCount && !feof(file))
|
||||
{
|
||||
char *word = readWord(file);
|
||||
if(word != NULL)
|
||||
{
|
||||
strncpy(words[count], word, MAX_WORD_LEN - 1); // Wort in Array kopieren
|
||||
words[count][MAX_WORD_LEN - 1] = '\0'; // Sicherstellen, dass String nullterminiert ist
|
||||
free(word); // Dynamisch allokierten Speicher freigeben
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count; // Anzahl gelesene Wörter zurückgeben
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#define MAX_WORD_LEN 100
|
||||
#define MAX_LINE_LEN 1024
|
||||
|
||||
char* readWord(FILE *file);
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
+16
-2
@@ -33,14 +33,28 @@ int main(int argc, char *argv[])
|
||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||
fclose(file);
|
||||
|
||||
// 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
|
||||
|
||||
// DONE
|
||||
|
||||
|
||||
// Create the word salad by placing words into grid
|
||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||
|
||||
if(placedWords==wordCount)
|
||||
{
|
||||
// Spiel starten, mit dem erstellten wordSalad, der Anzahl der Wörter und der Liste der Wörter, 800 = Fensterbreite
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Could not place all words in the word salad. Placed %u out of %u words.\n", placedWords, wordCount);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Binary file not shown.
@@ -9,8 +9,8 @@ unityfolder = ./unity
|
||||
# --------------------------
|
||||
# initiales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
wordsalad_ourversion:
|
||||
$(CC) -o wordsalad_ourversion -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -1,5 +1 @@
|
||||
Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Yeti Nessie Werwolf Vampir Monster Hydra Frankenstein Dracula KingKong Gremlin Kobold Hexe Poltergeist Gespenst Oger
|
||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user