5 Commits
Author SHA1 Message Date
Kristin c288f6debf nutzlose Variable gelöscht 2025-11-04 10:33:36 +01:00
Kristin cbc48c3362 mit Kommentaren, aufgeräumt 2025-11-02 17:24:44 +01:00
Kristin 8adb201876 angepasstes makefile 2025-11-02 16:56:58 +01:00
Kristin c5c2824a97 tests ok, programm noch aufräumen! 2025-10-30 21:20:06 +01:00
Kristin cb9a3ef159 richtiges repo 2025-10-24 17:08:35 +02:00
14 changed files with 171 additions and 33 deletions
+112 -10
View File
@@ -1,23 +1,125 @@
#include "game.h" #include "game.h"
#include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: int createWordSalad(
/* * 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 char salad[MAX_SEARCH_FIELD_LEN]
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) [MAX_SEARCH_FIELD_LEN], // salad ist Spielfeld 2D Array mit 20*20
{ // Buchstaben
unsigned int searchFieldLen, const char words[][MAX_WORD_LEN],
unsigned int wordCount) {
enum Richtung { HORIZONTAL, VERTIKAL };
for (int x = 0; x < searchFieldLen; x++) {
for (int y = 0; y < searchFieldLen; y++) {
salad[y][x] = EMPTY_CHAR; // Spielfeld auf EmptyChar setzen
}
}
unsigned int placedWords = 0; // variable platzierte Wörter
// Wörter platzieren
// for schleife mit wortNummer = 0 bis wordCount - 1 (insgesamt wordCount)
for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) {
// strlen mit Wortlänge
int wortLen = strlen(words[wortNummer]);
int platziert = 0; // variable, um platziertes Wort zu signalisieren
int versuch = 0; // veruche bis max rand tries
while (versuch < MAX_RAND_TRIES_PER_WORD &&
platziert == 0) { // wort platziert -> Schleife wird verlassen oder
// konnte nach 10 Versuchen nicht platziert werden
enum Richtung r =
(rand() % 2) ? HORIZONTAL : VERTIKAL; // mit rand Richtung
int x = rand() % searchFieldLen; // mit rand Koordinaten bestimmen
int y = rand() % searchFieldLen;
int wortFrei = 1; // Wort passt ins Feld und überschneidet sich nicht
if (r == VERTIKAL) {
if ((y + wortLen) > (searchFieldLen)) { // Wortlänge größer als Feld
wortFrei = 0;
}
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
char var = salad[y + i][x];
if (var != EMPTY_CHAR) {
wortFrei = 0; // Wort überschneidet sich mit bereits platziertem
// Wort
}
}
if (wortFrei != 0) {
for (int i = 0; i < wortLen; i++) {
salad[y + i][x] = words[wortNummer][i]; // Wort platzieren
}
placedWords++;
platziert = 1; // schleife wird verlassen
}
} }
// Prints the word salad to console else if (r == HORIZONTAL) {
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
if ((x + wortLen) > searchFieldLen) {
wortFrei = 0;
}
for (int i = 0; (i < wortLen) && (wortFrei != 0); i++) {
char var = salad[y][x + i];
if (var != EMPTY_CHAR) {
wortFrei = 0;
}
}
if (wortFrei != 0) {
for (int i = 0; i < wortLen; i++) {
salad[y][x + i] = words[wortNummer][i];
}
placedWords++;
platziert = 1;
}
}
versuch++;
}
}
for (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = rand() % (90 - 65 + 1) + 65;
}
}
}
return placedWords;
}
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]);
}
}
} }
Binary file not shown.
Binary file not shown.
+38 -6
View File
@@ -1,12 +1,44 @@
#include "input.h" #include "input.h"
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <string.h>
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// 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) {
if (file == NULL) {
printf("File couldn't be opened...");
return 0;
}
char line[MAX_LINE_LEN]; // fehlerhaften String zwischenspeichern
unsigned int wordCount = 0; // eingelesene Wörter
while ((fgets(line, sizeof(line), file) != NULL) &&
(wordCount < maxWordCount)) { // gesamte Wörterliste einlesen
// Token initialisieren
char *token = strtok(
line, " ,;.\n"); // Trennen der Wörter bei den angegebenen Zeichen
while (token && (wordCount < maxWordCount)) {
if (*token == '\0') {
token = strtok(NULL, " ,;.\n");
continue;
}
for (int i = 0; token[i] != '\0'; i++) {
token[i] = toupper(token[i]); // Alles in Großbuchstaben
}
strcpy(words[wordCount], token); // einzelnes Wort in words array
// speichern
wordCount++; // Nächstes Wort
token = strtok(NULL, " ,;.\n"); // Nächster Token
}
}
return wordCount; // Anzahl der eingelesenen Wörter
} }
Binary file not shown.
+7 -4
View File
@@ -3,11 +3,13 @@
#include "input.h" #include "input.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20 #define SALAD_SIZE 20
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srand(time(NULL));
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
@@ -37,19 +39,20 @@ int main(int argc, char *argv[]) {
// 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
printf("Placed Words: %d\n", placedWords);
printf("Word Count:%d\n", wordCount);
if (placedWords == wordCount) { if (placedWords == wordCount) {
startGame(wordSalad, SALAD_SIZE, words, wordCount, startGame(wordSalad, SALAD_SIZE, words, placedWords, 1024);
MAX_SEARCH_FIELD_LEN);
} }
else else
{ {
// Print error message if words couldn't be placed // Print error message if words couldn't be placed
// evtl noch richtigen error code einfügen printf("Placed %d words of %d.", placedWords, wordCount);
fprintf(stderr, "Could not place words...\n");
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
} else { } else {
Binary file not shown.
+9 -9
View File
@@ -1,5 +1,5 @@
CC = gcc CC = gcc
CFLAGS = -g -Wall -I$(raylibfolder) CFLAGS = -g -Wall
LDFLAGS = -lopengl32 -lgdi32 -lwinmm LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows BINARIES = ./windows
@@ -15,18 +15,18 @@ wordsalad_initial:
#--------------------------- #---------------------------
# eigenes Target bauen # eigenes Target bauen
#--------------------------- #---------------------------
wordsalad_myversion: main.o graphicalGame.o input.o game.o
$(CC) main.o graphicalGame.o input.o game.o -o wordsalad_myversion $(BINARIES)/libraylib.a $(LDFLAGS)
wordsalad_myversion:
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
# -------------------------- # --------------------------
# Normales Spiel bauen # Normales Spiel bauen
# -------------------------- # --------------------------
all: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a all: $(BINARIES)/libwordsalad.a 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)
$(BINARIES)/libwordsalad.a: main.o input.o game.o graphicalGame.o
ar rcs $(BINARIES)/libwordsalad.a main.o input.o game.o graphicalGame.o
main.o: main.c main.o: main.c
$(CC) -c $(CFLAGS) main.c $(CC) -c $(CFLAGS) main.c
@@ -37,7 +37,7 @@ game.o: game.c
$(CC) -c $(CFLAGS) game.c $(CC) -c $(CFLAGS) game.c
graphicalGame.o: graphicalGame.c graphicalGame.o: graphicalGame.c
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c $(CC) -I$(raylib_folder) $(CFLAGS) -c graphicalGame.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
@@ -51,4 +51,4 @@ test: input.o game.o unit_tests.c
# Clean # Clean
# -------------------------- # --------------------------
clean: clean:
del /f *.o *.exe rm -f *.o *.exe $(BINARIES)/libwordsalad.a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+3 -2
View File
@@ -1,5 +1,6 @@
Yeti,Nessie Werwolf; Vampir Yeti,Nessie Werwolf; Vampir
Monster Monster
Hydra;Frankenstein Hydra;Frankenstein
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist Dracula;KingKong,xxxxxxxx
Gespenst, Oger Max, Tobi, Kristin
Binary file not shown.