Compare commits

..
2 Commits
Author SHA1 Message Date
zidanmo99094 e9a4bfdc64 change 2026-10-20 11:38:21 +02:00
zidanmo99094 1b841ae875 Startcode bearbeitet 2026-10-18 17:34:33 +02:00
5 changed files with 6 additions and 100 deletions

No files matched your search

+2 -67
View File
@@ -3,80 +3,15 @@
#include <stdlib.h>
#include <string.h>
#define MAX_RAND_TRIES_PER_WORD 1000
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
// TODO: Spiellogik implementieren:
//TODO: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
// 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)
{
// 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;
}
+1 -15
View File
@@ -2,22 +2,8 @@
#include <string.h>
#include <ctype.h>
// TODO:
// TODO:
// 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
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
+1 -10
View File
@@ -6,7 +6,6 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_WIDTH 800 //Breite Spielfenster 800 Pixel
int main(int argc, char *argv[])
{
@@ -37,18 +36,10 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO: erledigt
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// 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
-7
View File
@@ -22,13 +22,6 @@ codefolder = .
wordsalad_initial:
$(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
# --------------------------
+2 -1
View File
@@ -2,4 +2,5 @@ Yeti,Nessie Werwolf; Vampir
Monster
Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
Gespenst, Oger
Gespenst, Oger, Zombie, nnn
Werwolf