This commit is contained in:
Johannes Kritsch 2025-11-11 14:21:14 +01:00
parent eda27ce871
commit 1b3594fe4a
4 changed files with 134 additions and 8 deletions

View File

@ -13,12 +13,105 @@
// 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)
{ {
//hihui //Wort für Wort durchgehen
//tetetet //int position = 0;
srand(time(NULL));
int VerHor = 0;
int laenge = 0;
int maxPosition = 0;
int startPositionHor = 0;
int startPositionVer = 0;
int noFreigabe = 1;
int placedWords = 0;
int countTrys = 0;
//Jedes Wort einzeln durchgehen
for (int i = 0; i < wordCount; i++) {
laenge = strlen(words[i]);
//Vertikal oder Horizontal, Vertikal = 0; Horizontal = 1;
VerHor = rand() % 2;
//Maximale Startposition des Wortes bestimmen
maxPosition = searchFieldLen - laenge;
//Horizontal
if (VerHor) {
//Prüfen, ob die erforderlichen Felder frei sind, sonst erneute Positionsbestimmung
//Nach 10 vergeblichen Prüfungen wird abgebrochen
while (noFreigabe) {
countTrys++;
startPositionHor = rand() % (maxPosition + 1);
startPositionVer = rand() % searchFieldLen;
for (int j = 0; j < laenge; j++) {
if (salad[startPositionVer][startPositionHor + j] == EMPTY_CHAR) {
noFreigabe = 0;
}
else {
noFreigabe = 1;
break;
}
}
if (countTrys >= MAX_RAND_TRIES_PER_WORD)
break;
}
//Woerter in die leeren Felder eintragen
if (noFreigabe == 0) {
for (int j = 0; j < laenge; j++) {
salad[startPositionVer][startPositionHor + j] = words[i][j];
}
placedWords++;
countTrys = 0;
}
}
//Vertikal
else {
while (noFreigabe) {
countTrys++;
startPositionVer = rand() % (maxPosition + 1);
startPositionHor = rand() % searchFieldLen;
for (int j = 0; j < laenge; j++) {
if (salad[startPositionVer + j][startPositionHor] == EMPTY_CHAR) {
noFreigabe = 0;
}
else {
noFreigabe = 1;
break;
}
}
if (countTrys >= MAX_RAND_TRIES_PER_WORD)
break;
}
if (noFreigabe == 0) {
for (int j = 0; j < laenge; j++) {
salad[startPositionVer + j][startPositionHor] = words[i][j];
}
placedWords++;
countTrys = 0;
}
}
}
//Rest mit random Buchstaben auffüllen
for (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
if (salad[i][j] == EMPTY_CHAR)
salad[i][j] = ' ';// + (rand() % 26);
}
}
showWordSalad(salad, searchFieldLen);
return placedWords;
} }
// 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 (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
printf("%c", salad[i][j]);
}
printf("\n");
}
} }

View File

@ -8,5 +8,25 @@
// 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)
{ {
char zeile[MAX_LINE_LEN];
int anzahlWord = 0;
while (fgets(zeile, MAX_LINE_LEN, file)) {
char *teiler = " .,;";
char *token = strtok(zeile, teiler);
while (token != NULL && anzahlWord < maxWordCount) {
strncpy(words[anzahlWord], token, MAX_WORD_LEN - 1);
words[anzahlWord][MAX_WORD_LEN - 1] = '\0';
anzahlWord++;
token = strtok(NULL, teiler);
}
}
/*Alle Buchstaben zu Großbuchstaben umwandeln
for (int i = 0; i < anzahlWord; i++) {
int laenge = strlen(words[i]);
for (int j = 0; j < laenge; j++)
words[i][j] = toupper(words[i][j]);
}
*/
return anzahlWord;
} }

View File

@ -36,6 +36,16 @@ 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);
if(placedWords == wordCount) {
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
//showWordSalad(wordSalad, SALAD_SIZE);
else {
// Print error message if file couldn't be opened
fprintf(stderr, "Words couldn't be placed correctly...\n");
exitCode = EXIT_FAILURE;
}
// 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

View File

@ -1,5 +1,5 @@
CC = gcc CC = gcc
CFLAGS = -g -Wall -I$(raylibfolder) CFLAGS = -g -Wall -I$(raylib_folder)
LDFLAGS = -lopengl32 -lgdi32 -lwinmm LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows BINARIES = ./windows
@ -12,6 +12,9 @@ unityfolder = ./unity
wordsalad_initial: wordsalad_initial:
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS) $(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
wordsalad_myversion:
$(CC) -o wordsalad_myversion main.o game.o input.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
# -------------------------- # --------------------------
# Normales Spiel bauen # Normales Spiel bauen
@ -20,16 +23,16 @@ all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
$(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
main.o: main.c main.o: main.c
$(CC) -c $(CFLAGS) main.c $(CC) $(CFLAGS) -c main.c
input.o: input.c input.o: input.c
$(CC) -c $(CFLAGS)input.c $(CC) $(CFLAGS) -c input.c
game.o: game.c game.o: game.c
$(CC) -c $(CFLAGS) game.c $(CC) $(CFLAGS) -c game.c
graphicalGame.o: graphicalGame.c graphicalGame.o: graphicalGame.c
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c $(CC) $(CFLAGS) -I$(raylib_folder) -c graphicalGame.c -o graphicalGame.o
# -------------------------- # --------------------------
# Unit Tests # Unit Tests