generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
8
Commits
aed3cce12e
...
my-changes
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c5e903057 | ||
|
|
21a7232788 | ||
|
|
43ee886b45 | ||
|
|
6ebb78c2b0 | ||
|
|
4e250b1642 | ||
|
|
cb7e72b7ee | ||
|
|
15f5dfbd40 | ||
|
|
b8074e6751 |
No files matched your search
+75
-29
@@ -2,7 +2,6 @@
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
@@ -14,48 +13,95 @@
|
||||
// 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)
|
||||
{
|
||||
// Initialisiere Salad mit EMPTY_CHAR
|
||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
salad[i][j] = EMPTY_CHAR;
|
||||
if (searchFieldLen == 0 || searchFieldLen > MAX_SEARCH_FIELD_LEN) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Start with empty field
|
||||
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
salad[r][c] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// Platziere Wörter (einfache horizontale Platzierung für Tests)
|
||||
unsigned int placedCount = 0;
|
||||
for(unsigned int w = 0; w < wordCount && w < searchFieldLen; w++)
|
||||
{
|
||||
unsigned int wordLen = strlen(words[w]);
|
||||
if(wordLen <= searchFieldLen)
|
||||
{
|
||||
// Platziere Wort horizontal in Zeile w
|
||||
for(unsigned int j = 0; j < wordLen; j++)
|
||||
{
|
||||
salad[w][j] = words[w][j];
|
||||
// Platzieren der Wörter
|
||||
unsigned int placedWords = 0;
|
||||
for (unsigned int w = 0; w < wordCount; ++w) {
|
||||
const char *word = words[w];
|
||||
size_t wordLen = strlen(word);
|
||||
if (wordLen == 0 || wordLen > searchFieldLen) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int placed = 0;
|
||||
for (int attempts = 0; attempts < MAX_RAND_TRIES_PER_WORD && !placed; ++attempts) {
|
||||
int vertical = rand() % 2; // 0=horizontal, 1=vertikal
|
||||
unsigned int row, col;
|
||||
|
||||
if (vertical) {
|
||||
row = rand() % (searchFieldLen - wordLen + 1);
|
||||
col = rand() % searchFieldLen;
|
||||
} else {
|
||||
row = rand() % searchFieldLen;
|
||||
col = rand() % (searchFieldLen - wordLen + 1);
|
||||
}
|
||||
placedCount++;
|
||||
|
||||
// Check if placeable
|
||||
int ok = 1;
|
||||
for (size_t i = 0; i < wordLen; ++i) {
|
||||
unsigned int r = row + (vertical ? i : 0);
|
||||
unsigned int c = col + (vertical ? 0 : i);
|
||||
char current = salad[r][c];
|
||||
if (current != EMPTY_CHAR && current != word[i]) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ok) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set letters
|
||||
for (size_t i = 0; i < wordLen; ++i) {
|
||||
unsigned int r = row + (vertical ? i : 0);
|
||||
unsigned int c = col + (vertical ? 0 : i);
|
||||
salad[r][c] = word[i];
|
||||
}
|
||||
placed = 1;
|
||||
}
|
||||
|
||||
if (placed) {
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
|
||||
// Fülle restliche Felder mit zufälligen Buchstaben
|
||||
for(unsigned int i = 0; i < searchFieldLen; i++)
|
||||
{
|
||||
for(unsigned int j = 0; j < searchFieldLen; j++)
|
||||
{
|
||||
if(salad[i][j] == EMPTY_CHAR)
|
||||
{
|
||||
salad[i][j] = 'A' + (rand() % 26);
|
||||
// Rest mit zufälligen Buchstaben füllen
|
||||
for (unsigned int r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
if (salad[r][c] == EMPTY_CHAR) {
|
||||
salad[r][c] = 'A' + (rand() % 26);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return placedCount;
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
// 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 r = 0; r < searchFieldLen; ++r) {
|
||||
for (unsigned int c = 0; c < searchFieldLen; ++c) {
|
||||
char ch = salad[r][c];
|
||||
if (ch == EMPTY_CHAR) {
|
||||
ch = '.';
|
||||
}
|
||||
putchar(ch);
|
||||
if (c + 1 < searchFieldLen) {
|
||||
putchar(' ');
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
+10
-5
@@ -1,6 +1,7 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
@@ -11,19 +12,23 @@ char* readWord(FILE *file)
|
||||
int c; // Variable für jedes gelesene Zeichen
|
||||
|
||||
// Whitespace und Delimiters überspringen
|
||||
while((c = fgetc(file)) != EOF && (isspace(c) || c == ',' || c == ';' || c == '.'));
|
||||
while ((c = fgetc(file)) != EOF && (isspace((unsigned char)c) || c == ',' || c == ';' || c == '.'));
|
||||
|
||||
if (c == EOF) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Buchstaben einlesen bis nächstes whitespace/Delimiter/EOF
|
||||
while(c != EOF && !isspace(c) && c != ',' && c != ';' && c != '.' && index < MAX_WORD_LEN - 1) //-1 wegen Nullterminator
|
||||
while (c != EOF && !isspace((unsigned char)c) && c != ',' && c != ';' && c != '.' && index < MAX_WORD_LEN - 1) //-1 wegen Nullterminator
|
||||
{
|
||||
word[index++] = (char)toupper(c); // Konvertiere zu Großbuchstaben
|
||||
word[index++] = (char)toupper((unsigned char)c); // Konvertiere zu Großbuchstaben
|
||||
c = fgetc(file);
|
||||
}
|
||||
word[index] = '\0'; // Nullterminator (= Ende String)
|
||||
|
||||
// Leere überspringen
|
||||
if(index == 0)
|
||||
if (index == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return strdup(word); // Rückgabe string, dynamisch allokiert da Zeiger auf lokalen Puffer zurückgegeben
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+32
-35
@@ -1,45 +1,42 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lGL -lX11 -lm
|
||||
BINARIES = ./linux
|
||||
CFLAGS = -g -Wall -Wextra
|
||||
CPPFLAGS = -I$(raylibfolder) -I$(unityfolder)
|
||||
LDFLAGS =
|
||||
LDLIBS = -lGL -lX11 -lm
|
||||
|
||||
BINARIES = ./linux
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
# initiales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad_ourversion:
|
||||
$(CC) -o wordsalad_ourversion -L. $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
wordsalad: 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)
|
||||
|
||||
main.o: main.c
|
||||
$(CC) $(CFLAGS) -c main.c
|
||||
|
||||
input.o: input.c
|
||||
$(CC) $(CFLAGS) -c input.c
|
||||
|
||||
game.o: game.c
|
||||
$(CC) $(CFLAGS) -c game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
TARGET = wordsalad
|
||||
TEST_BIN = runTests
|
||||
|
||||
OBJS = main.o input.o game.o graphicalGame.o
|
||||
|
||||
.PHONY: all clean test
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
wordsalad_ourversion: main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o $@ main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDLIBS)
|
||||
|
||||
$(TARGET): $(OBJS) $(BINARIES)/libraylib.a
|
||||
$(CC) $(CFLAGS) -o $@ $(OBJS) $(BINARIES)/libraylib.a $(LDLIBS)
|
||||
|
||||
main.o: main.c input.h game.h graphicalGame.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
||||
|
||||
input.o: input.c input.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
||||
|
||||
game.o: game.c game.h input.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
||||
|
||||
graphicalGame.o: graphicalGame.c graphicalGame.h game.h
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
||||
|
||||
test: input.o game.o unit_tests.c
|
||||
$(CC) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
rm -f *.o wordsalad $(TEST_BIN)
|
||||
rm -f *.o $(TARGET) $(TEST_BIN) wordsalad_ourversion
|
||||
Binary file not shown.
@@ -1 +1,5 @@
|
||||
Yeti Nessie Werwolf Vampir Monster Hydra Frankenstein Dracula KingKong Gremlin Kobold Hexe Poltergeist Gespenst Oger
|
||||
Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Binary file not shown.
Reference in new issue
Block a user