Compare commits
32
Commits
3c89c51854
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6b98b53fc | ||
|
|
bd2e6f4e41 | ||
|
|
0a87d89da3 | ||
|
|
fe1cb95cc0 | ||
|
|
078e326e4d | ||
|
|
491a85debd | ||
|
|
b8a6308821 | ||
|
|
9d8a760f08 | ||
|
|
817ddc0e35 | ||
|
|
09039d961a | ||
|
|
0c086b9607 | ||
|
|
b88cf94072 | ||
|
|
5162e9890d | ||
|
|
fbef3806db | ||
|
|
8844495411 | ||
|
|
288693c76f | ||
|
|
b1d7cbc74b | ||
|
|
207d93b317 | ||
|
|
cf584df1b6 | ||
|
|
c5501c1bd3 | ||
|
|
3c68ced8ab | ||
|
|
eb15c18771 | ||
|
|
02b4cb3b9b | ||
|
|
83de6ef49e | ||
|
|
69a78a0776 | ||
|
|
32a6133d18 | ||
|
|
39e8380916 | ||
|
|
ffaed665b9 | ||
|
|
e20bf82c19 | ||
|
|
1ed03a3444 | ||
|
|
6b435cf82d | ||
|
|
92c0ab2c28 |
@@ -1,9 +1,9 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lGL -lX11 -lm
|
||||
BINARIES = ./linux
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -28,7 +28,7 @@ game.o: game.c
|
||||
$(CC) $(CFLAGS) -c game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) $(CFLAGS) -c graphicalGame.c
|
||||
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
|
||||
@@ -101,7 +101,37 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
||||
|
||||
@@ -134,9 +164,10 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -f
|
||||
ARCH := $(shell uname -m)
|
||||
BINARIES = ./macos-$(ARCH)
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -43,4 +43,4 @@ test: input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
rm -f *.o wordsalad
|
||||
rm -f *.o wordsalad $(TEST_BIN)
|
||||
|
||||
@@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
@@ -135,6 +164,7 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
+91
-1
@@ -2,6 +2,8 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@@ -10,14 +12,102 @@
|
||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||
|
||||
|
||||
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[])
|
||||
{
|
||||
for(size_t i = 0 ; i < strlen(wort); i++ )
|
||||
{
|
||||
short xoffset = (richtung) ? i : 0;
|
||||
short yoffset = (richtung) ? 0 : i;
|
||||
if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i]))
|
||||
{
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
unsigned int placedCount = 0;
|
||||
|
||||
for(short i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(short j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = '#';
|
||||
}
|
||||
}
|
||||
srand(time(NULL));
|
||||
for(short i = wordCount;i>0;i--)
|
||||
{
|
||||
size_t platzbedarf = strlen(words[i-1]);
|
||||
if (platzbedarf > searchFieldLen)
|
||||
{
|
||||
printf("%s konnte nicht eingefuegt werden da es groesser als das Feld ist\n",words[i-1]);
|
||||
continue;
|
||||
}
|
||||
short x,y,waagrecht;
|
||||
int zuoft = enoughTries(searchFieldLen,0.99);
|
||||
int versuche = 0;
|
||||
do
|
||||
{
|
||||
versuche++;
|
||||
short max = searchFieldLen - platzbedarf;
|
||||
short position1 = rand() % (max +1);
|
||||
short position2 = rand() % (searchFieldLen);
|
||||
waagrecht = rand()%2;
|
||||
x = (waagrecht) ? position1:position2;
|
||||
y = (waagrecht) ? position2:position1;
|
||||
|
||||
|
||||
}while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1 && versuche<=zuoft);
|
||||
if(versuche > zuoft)
|
||||
{
|
||||
printf("%s passt nicht mehr ins Gitter\n",words[i-1]);
|
||||
continue;
|
||||
}
|
||||
for(size_t k = 0 ; k < strlen(words[i-1]); k++ )
|
||||
{
|
||||
short xoffset = (waagrecht) ? k : 0;
|
||||
short yoffset = (waagrecht) ? 0 : k;
|
||||
salad[x+xoffset][y+yoffset]=words[i-1][k];
|
||||
}
|
||||
|
||||
placedCount++;
|
||||
|
||||
|
||||
}
|
||||
for(short i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(short j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if(salad[i][j] == '#')
|
||||
{
|
||||
salad[i][j]='A' + (rand()%26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedCount;
|
||||
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
|
||||
for (unsigned int y = 0; y < searchFieldLen; y++) {
|
||||
for (unsigned int x = 0; x < searchFieldLen; x++) {
|
||||
printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen
|
||||
}
|
||||
printf("\n"); // neue Zeile nach jeder Zeile
|
||||
}
|
||||
}
|
||||
|
||||
int enoughTries(double feldgroesse, double sicherheit) {
|
||||
double moeglichkeiten = feldgroesse * feldgroesse; // Anzahl der Möglichkeiten
|
||||
double c = -log(-log(sicherheit)); // Zusatzterm aus Zielwahrscheinlichkeit
|
||||
double versuche = moeglichkeiten * (log(moeglichkeiten) + c); // Coupon-Collector-Formel
|
||||
return (int)ceil(versuche);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include "input.h"
|
||||
|
||||
#define MAX_SEARCH_FIELD_LEN 100
|
||||
|
||||
int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[]);
|
||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount);
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||
|
||||
int enoughTries(double feldgroesse, double sicherheit);
|
||||
#endif
|
||||
|
||||
+52
-6
@@ -6,13 +6,59 @@
|
||||
// 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 isValidWord(const char *word)
|
||||
{
|
||||
for (int i = 0;word[i]; i++)
|
||||
{
|
||||
if (!('A' <= word[i] && word[i] <= 'Z'))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
//Erstellt Pointer auf textdokument
|
||||
FILE *textdokument = fopen("words.txt", "r");
|
||||
if (file == NULL) {
|
||||
printf("words,txt konnte nicht geoffnet werden");
|
||||
}
|
||||
// ToDo 2D char Array um wörter zu speichern
|
||||
char line[1024]; // Puffer für eine Zeile
|
||||
unsigned int count = 0;
|
||||
|
||||
// Alle gewünschten Trennzeichen
|
||||
const char *delimiters = " \t\n,.;:!?\"'()[]{}";
|
||||
|
||||
// Zeile für Zeile lesen
|
||||
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) {
|
||||
// Tokenize Zeile nach Trennzeichen
|
||||
char *token = strtok(line, delimiters);
|
||||
|
||||
while (token != NULL && count < maxWordCount)
|
||||
{
|
||||
// Überspringe leere Tokens
|
||||
if (token && strlen(token) > 0)
|
||||
{
|
||||
// Alles in Großbuchstaben umwandeln
|
||||
for (int i = 0; token[i]; i++)
|
||||
{
|
||||
token[i] = toupper(token[i]);
|
||||
}
|
||||
// Prüfen, ob das Wort nur A-Z enthält
|
||||
|
||||
|
||||
|
||||
if (!isValidWord(token))
|
||||
{
|
||||
fprintf(stderr, "Ungültiges Wort gefunden: '%s'. Nur Buchstaben A-Z erlaubt.\n", token);
|
||||
token = strtok(NULL, delimiters);
|
||||
continue; // Wort ignorieren
|
||||
}
|
||||
|
||||
// Wort ins Array kopieren
|
||||
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||
count++;
|
||||
}
|
||||
token = strtok(NULL, delimiters);
|
||||
}
|
||||
}
|
||||
|
||||
return count; // Datei nicht schließen – gehört dem Aufrufer
|
||||
}
|
||||
@@ -7,5 +7,6 @@
|
||||
#define MAX_LINE_LEN 1024
|
||||
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||
int isValidWord(const char *word);
|
||||
|
||||
#endif
|
||||
|
||||
+36
-3
@@ -9,7 +9,26 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
/*
|
||||
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char words[MAX_NUMBER_OF_WORDS][100] =
|
||||
{
|
||||
"TESTN",
|
||||
"KATZE",
|
||||
"ENTE",
|
||||
"BEERE",
|
||||
"FEE",
|
||||
"KLEE",
|
||||
"Feheler",
|
||||
"SEE",
|
||||
};
|
||||
int wordCount = 8;
|
||||
createWordSalad(salad,7, words,wordCount);
|
||||
showWordSalad(salad,7);
|
||||
|
||||
*/
|
||||
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
if(argc != 2)
|
||||
@@ -39,7 +58,19 @@ int main(int argc, char *argv[])
|
||||
// TODO:
|
||||
// Check if all words were successfully placed
|
||||
// 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)
|
||||
{
|
||||
// Start the game with the created word salad
|
||||
printf("Squares: %u, Words: %u\n", SALAD_SIZE, placedWords);
|
||||
unsigned int windowWidth = 800;
|
||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Could not place all words into the word salad. Placed %u out of %u words.\n", placedWords, wordCount);
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
@@ -51,4 +82,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-8
@@ -1,9 +1,9 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
BINARIES = ./windows
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -12,7 +12,6 @@ unityfolder = ./unity
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
@@ -23,24 +22,28 @@ 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$(raylibfolder) -c $(CFLAGS) graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
TEST_BIN = runTests
|
||||
|
||||
test: input.o game.o unit_tests.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
#test: input.o game.o unit_tests.c
|
||||
# $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
test: input.o game.o unit_tests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
#clean:
|
||||
# del /f *.o *.exe
|
||||
clean:
|
||||
del /f *.o *.exe
|
||||
rm -f *.o *.exe
|
||||
|
||||
@@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
@@ -135,6 +164,7 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
Reference in New Issue
Block a user