tests ok, programm noch aufräumen!

This commit is contained in:
Kristin 2025-10-30 21:20:06 +01:00
parent cb9a3ef159
commit c5c2824a97
3 changed files with 180 additions and 16 deletions

View File

@ -1,23 +1,145 @@
#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#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)
{
int createWordSalad(
char salad[MAX_SEARCH_FIELD_LEN]
[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) {
srand(time(NULL));
enum Richtung { HORIZONTAL, VERTIKAL };
// wipe field
// array auf emptychar
for (int x = 0; x < searchFieldLen; x++) {
for (int y = 0; y < searchFieldLen; y++) {
salad[y][x] = EMPTY_CHAR;
}
}
int placedWords = 0; // variable platzierte Wörter
// place words
// for schleife mit wortzahl = 0 bis wordCount
for (int wortNummer = 0; wortNummer < wordCount; wortNummer++) {
// strlen mit Wortlänge
int wortLen = strlen(words[wortNummer]);
int platziert = 0;
int versuch = 0;
// rand % 2 vertikal oder horizontal
// rand % searchFieldLen um Ort zu generieren
// Länge überprüfen und überprüfen, ob wort sich überschneidet ist nicht
// randomtries
while (versuch < MAX_RAND_TRIES_PER_WORD && platziert == 0) {
enum Richtung r = (rand() % 2) ? HORIZONTAL : VERTIKAL;
int x = rand() % searchFieldLen;
int y = rand() % searchFieldLen;
int fits = 1;
if (r == HORIZONTAL) {
if ((y + wortLen) > (searchFieldLen)) {
fits = 0;
}
if (fits) {
for (int i = 0; i < wortLen; i++) {
char var = salad[y + i][x];
if (var != EMPTY_CHAR) {
fits = 0;
}
}
}
if (fits != 0) {
for (int i = 0; i < wortLen; i++) {
salad[y + i][x] = words[wortNummer][i];
}
platziert = 1;
placedWords++;
}
}
else if (r == VERTIKAL) {
if ((x + wortLen) > searchFieldLen) {
fits = 0;
}
if (fits) {
for (int i = 0; i < wortLen; i++) {
char var = salad[y][x + i];
if (var != EMPTY_CHAR) {
fits = 0;
}
}
}
if (fits != 0) {
for (int i = 0; i < wortLen; i++) {
salad[y][x + i] = words[wortNummer][i];
}
platziert = 1;
placedWords++;
}
}
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() % ('Z' - 'A' + 1) + 'A';
}
}
}
return placedWords;
// emptychar rand() % ('Z' - 'A' + 1) + 'A'
}
// 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]);
}
}
}

View File

@ -1,12 +1,54 @@
#include "input.h"
#include <string.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.
// 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
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) {
perror("File couldn't be opened");
return 0;
}
// zunächst fehlerhaften String einlesen und in anderem Array
// zwischenspeichern
char line[MAX_LINE_LEN];
char *token;
int wordCount = 0;
while (fgets(line, sizeof(line), file) !=
NULL && // während mit fgets alle Zeichen aus dem file eingelesen
// werden
wordCount < maxWordCount) {
token = strtok(line, " ;,.\n"); // Erstes Wort mit strtok aus dem
// fehlerhaften String herauslösen
while (token != NULL &&
wordCount < maxWordCount) { // while strtok nicht am Ende ist und
// noch Wörter in words passen
for (int i = 0; token[i] != '\0'; i++) {
token[i] = toupper((unsigned char)token[i]);
}
strncpy(words[wordCount], token,
sizeof(words[wordCount]) -
1); // mit strcpy das aktuelle Wort in words kopieren
words[wordCount][sizeof(words[wordCount]) - 1] =
'\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des
// Worts setzen
wordCount++; // Nächstes Wort
token = strtok(NULL, " ;,.\n"); // Nächstes Token
}
}
return wordCount; // Anzahl der eingelesenen Wörter
}

View File

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -g -Wall -I$(raylibfolder)
CFLAGS = -g -Wall
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
BINARIES = ./windows
@ -15,7 +15,7 @@ wordsalad_initial:
#---------------------------
# eigenes Target bauen
#---------------------------
wordsalad_myversion:
wordsalad_myversion: main.o graphicalGame.o
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
@ -29,13 +29,13 @@ main.o: main.c
$(CC) -c $(CFLAGS) main.c
input.o: input.c
$(CC) -c $(CFLAGS)input.c
$(CC) -c $(CFLAGS) input.c
game.o: game.c
$(CC) -c $(CFLAGS) game.c
graphicalGame.o: graphicalGame.c
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
$(CC) -I$(raylib_folder) $(CFLAGS) -c graphicalGame.c
# --------------------------
# Unit Tests
@ -49,4 +49,4 @@ test: input.o game.o unit_tests.c
# Clean
# --------------------------
clean:
del /f *.o *.exe
rm -f *.o *.exe