generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
6
Commits
main
..
15f5dfbd40
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15f5dfbd40 | ||
|
|
aed3cce12e | ||
|
|
b8074e6751 | ||
|
|
208e35e934 | ||
|
|
36c61ff8e7 | ||
|
|
42739dc5cc |
No files matched your search
+85
-1
@@ -13,11 +13,95 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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)
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
|
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start with empty field
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||||
|
salad[r][c] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Platzieren der Wörter
|
||||||
|
for (unsigned int w = 0; w < wordCount; ++w) {
|
||||||
|
const char *word = words[w];
|
||||||
|
size_t wordLen = strlen(word);
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
for (int attempts = 0; attempts < MAX_RAND_TRIES_PER_WORD && !placed; ++attempts) {
|
||||||
|
int vertical = rand() % 2; // 0=horizontal, 1=vertical
|
||||||
|
unsigned int row, col;
|
||||||
|
|
||||||
|
if (vertical) {
|
||||||
|
row = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
col = rand() % searchFieldLen;
|
||||||
|
} else {
|
||||||
|
row = rand() % searchFieldLen;
|
||||||
|
col = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if placeable
|
||||||
|
int ok = 1;
|
||||||
|
for (size_t i = 0; i < wordLen; ++i) {
|
||||||
|
unsigned int r = row + (vertical ? i : 0);
|
||||||
|
unsigned int c = col + (vertical ? 0 : i);
|
||||||
|
char current = salad[r][c];
|
||||||
|
if (current != EMPTY_CHAR && current != word[i]) {
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!ok) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set letters
|
||||||
|
for (size_t i = 0; i < wordLen; ++i) {
|
||||||
|
unsigned int r = row + (vertical ? i : 0);
|
||||||
|
unsigned int c = col + (vertical ? 0 : i);
|
||||||
|
salad[r][c] = word[i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!placed) {
|
||||||
|
// Ein Wort konnte nicht platziert werden, Abbruch möglich
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rest mit zufälligen Buchstaben füllen
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||||
|
if (salad[r][c] == EMPTY_CHAR) {
|
||||||
|
salad[r][c] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
|
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||||
|
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||||
|
char ch = salad[r][c];
|
||||||
|
if (ch == EMPTY_CHAR) {
|
||||||
|
ch = '.';
|
||||||
|
}
|
||||||
|
putchar(ch);
|
||||||
|
if (c + 1 < searchFieldLen) {
|
||||||
|
putchar(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Binary file not shown.
@@ -4,9 +4,46 @@
|
|||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// 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.
|
||||||
|
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
|
// 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)
|
||||||
{
|
{
|
||||||
|
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_WORD_LEN 100
|
||||||
#define MAX_LINE_LEN 1024
|
#define MAX_LINE_LEN 1024
|
||||||
|
|
||||||
|
char* readWord(FILE *file);
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Binary file not shown.
+19
-5
@@ -31,16 +31,30 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
// Create the word salad by placing words into grid
|
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
// 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
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Binary file not shown.
@@ -9,8 +9,8 @@ unityfolder = ./unity
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# initiales Spiel bauen
|
# initiales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
wordsalad_initial:
|
wordsalad_ourversion:
|
||||||
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_ourversion -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
@@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
|||||||
Monster
|
Monster
|
||||||
Hydra;Frankenstein
|
Hydra;Frankenstein
|
||||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
Gespenst, Oger
|
Gespenst, Oger
|
||||||
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user