Compare commits
14
Commits
977f80eb24
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c5710f2f1 | ||
|
|
def3034ede | ||
|
|
fb4211872e | ||
|
|
70c6635991 | ||
|
|
213103dad0 | ||
|
|
c3e875f493 | ||
|
|
fbd986c66f | ||
|
|
2100c8da2c | ||
|
|
1ae5e39309 | ||
|
|
a297e25d94 | ||
|
|
df5c3e2ed0 | ||
|
|
49e4f22a22 | ||
|
|
f1fad264c8 | ||
|
|
450697aac9 |
No files matched your search
+66
-1
@@ -3,7 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 1000
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
// TODO: Spiellogik implementieren:
|
// TODO: Spiellogik implementieren:
|
||||||
@@ -13,5 +13,70 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
// 1. Spielfeld vollständig mit EMPTY_CHAR initialisieren
|
||||||
|
for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
|
||||||
|
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
|
||||||
|
salad[r][c] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int placedWordsCount = 0;
|
||||||
|
|
||||||
|
// 2. Jedes Wort aus der Wörterliste platzieren
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++) {
|
||||||
|
if (words[i] == NULL) continue;
|
||||||
|
|
||||||
|
int len = (int)strlen(words[i]);
|
||||||
|
if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue;
|
||||||
|
|
||||||
|
// Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) {
|
||||||
|
// 0 = Horizontal, 1 = Vertikal
|
||||||
|
int dir = rand() % 2;
|
||||||
|
|
||||||
|
int row, col;
|
||||||
|
if (dir == 0) { // Horizontal
|
||||||
|
row = rand() % MAX_SEARCH_FIELD_LEN;
|
||||||
|
col = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
|
||||||
|
} else { // Vertikal
|
||||||
|
row = rand() % (MAX_SEARCH_FIELD_LEN - len + 1);
|
||||||
|
col = rand() % MAX_SEARCH_FIELD_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prüfen, ob das Wort platziert werden kann (Kollisionsprüfung)
|
||||||
|
int canPlace = 1;
|
||||||
|
for (int k = 0; k < len; k++) {
|
||||||
|
int r = (dir == 1) ? (row + k) : row;
|
||||||
|
int c = (dir == 0) ? (col + k) : col;
|
||||||
|
|
||||||
|
// Feld muss noch leer sein oder exakt den gleichen Buchstaben enthalten
|
||||||
|
if (salad[r][c] != EMPTY_CHAR && salad[r][c] != words[i][k]) {
|
||||||
|
canPlace = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wenn die Position gültig ist, Buchstaben in das Spielfeld schreiben
|
||||||
|
if (canPlace) {
|
||||||
|
for (int k = 0; k < len; k++) {
|
||||||
|
int r = (dir == 1) ? (row + k) : row;
|
||||||
|
int c = (dir == 0) ? (col + k) : col;
|
||||||
|
salad[r][c] = words[i][k];
|
||||||
|
}
|
||||||
|
placedWordsCount++;
|
||||||
|
break; // Nächstes Wort verarbeiten
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Verbleibende leere Felder (EMPTY_CHAR) mit Zufallsbuchstaben 'A'-'Z' auffüllen
|
||||||
|
for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) {
|
||||||
|
for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) {
|
||||||
|
if (salad[r][c] == EMPTY_CHAR) {
|
||||||
|
salad[r][c] = 'A' + (rand() % 26);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return placedWordsCount;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,20 @@
|
|||||||
|
|
||||||
// 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* readSingleWord(FILE *file, char *destination, int maxLen)
|
||||||
|
{
|
||||||
|
char buffer[256]; // Zwischenspeicher zum einlesen
|
||||||
|
if (fgets(buffer, sizeof(buffer), file) != NULL)
|
||||||
|
{
|
||||||
|
buffer[strcspn(buffer, "\r\n")] = '\0'; // Entfernt Umbrüche
|
||||||
|
|
||||||
|
strncpy(destination, buffer, maxLen - 1);
|
||||||
|
destination[maxLen - 1] = '\0'; // Zwingendes Ende des C-Strings setzen, verhindert Pufferüberlauf
|
||||||
|
|
||||||
|
return destination; //Rückgabe bei Erfolg
|
||||||
|
}
|
||||||
|
return NULL; //Rückgabe nur bei Dateiende
|
||||||
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
|
|||||||
+10
-1
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
#define WINDOW_WIDTH 800 //Breite Spielfenster 800 Pixel
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -36,10 +37,18 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
// TODO: erledigt
|
||||||
// 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
|
||||||
|
if (placedWords == wordCount) //wenn alle Wörter erfolgreicht plaziert sind
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_WIDTH); //Startet das Spiel
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Fehler: Es konnten nicht alle Wörter plaziert werden\n"); //Gibt Fehlermeldung aus
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ codefolder = .
|
|||||||
wordsalad_initial:
|
wordsalad_initial:
|
||||||
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_initial -L. $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# meine Version
|
||||||
|
# --------------------------
|
||||||
|
wordsalad_myversion: main.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o graphicalGame.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
+1
-1
@@ -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, Maus
|
Gespenst, Oger
|
||||||
Reference in new issue
Block a user