forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1725da2148 | ||
|
|
bd830b2044 | ||
|
|
1d87ea69bc | ||
|
|
9b704552cc |
No files matched your search
+108
-1
@@ -6,7 +6,7 @@
|
|||||||
#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:
|
//DONE: Spiellogik implementieren:
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* restliche Felder mit zufälligen Buchstaben füllen */
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
@@ -14,4 +14,111 @@
|
|||||||
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], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
Horizontal,
|
||||||
|
Vertikal
|
||||||
|
} Direction;
|
||||||
|
|
||||||
|
for (unsigned int row = 0; row < searchFieldLen; row++)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (unsigned int col = 0; col < searchFieldLen; col++)
|
||||||
|
{
|
||||||
|
salad[row][col] = EMPTY_CHAR; // Alle Felder leer setzen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int placedWords = 0;
|
||||||
|
|
||||||
|
// 2. Alle Wörter nacheinander platzieren
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
|
{
|
||||||
|
unsigned int wordLen = strlen(words[i]);
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
// Wort darf nicht länger als das Spielfeld sein
|
||||||
|
if (wordLen == 0 || wordLen > searchFieldLen)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (unsigned int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
|
||||||
|
unsigned int startRow = 0;
|
||||||
|
unsigned int startCol = 0;
|
||||||
|
Direction direction = (Direction)(rand()%2);
|
||||||
|
|
||||||
|
if (direction == Horizontal)
|
||||||
|
{
|
||||||
|
startRow = rand() % searchFieldLen;
|
||||||
|
startCol = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
}
|
||||||
|
else // Vertikal
|
||||||
|
{
|
||||||
|
startRow = rand() % (searchFieldLen - wordLen + 1);
|
||||||
|
startCol = rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Überprüfen ob das Wort an der gewählten Position platziert werden kann
|
||||||
|
int canPlace = 1;
|
||||||
|
|
||||||
|
for (unsigned int j = 0; j < wordLen; j++)
|
||||||
|
{
|
||||||
|
if (direction == Horizontal)
|
||||||
|
{
|
||||||
|
if (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j])
|
||||||
|
{
|
||||||
|
canPlace = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Vertikal
|
||||||
|
{
|
||||||
|
if (salad[startRow + j][startCol] != EMPTY_CHAR && salad[startRow + j][startCol] != words[i][j])
|
||||||
|
{
|
||||||
|
canPlace = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canPlace)
|
||||||
|
{
|
||||||
|
// Wort platzieren
|
||||||
|
for (unsigned int j = 0; j < wordLen; j++)
|
||||||
|
{
|
||||||
|
if (direction == Horizontal)
|
||||||
|
{
|
||||||
|
salad[startRow][startCol + j] = words[i][j];
|
||||||
|
}
|
||||||
|
else // Vertikal
|
||||||
|
{
|
||||||
|
salad[startRow + j][startCol] = words[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
placedWords++;
|
||||||
|
placed = 1; //Markiert als gesetzt
|
||||||
|
printf("Wort '%s' nach %u Versuchen platziert.\n", words[i], attempt + 1); //Ausgabe als Test
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Restliche Felder mit zufälligen Buchstaben füllen
|
||||||
|
for (unsigned int row = 0; row < searchFieldLen; row++)
|
||||||
|
{
|
||||||
|
for (unsigned int col = 0; col < searchFieldLen; col++)
|
||||||
|
{
|
||||||
|
if (salad[row][col] == EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
salad[row][col] = 'A' + (rand() % 26); // Zufälliger Buchstabe von A-Z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
printf("Placed %u words out of %u\n", placedWords, wordCount);
|
||||||
|
return placedWords; // Gibt Anzahl gesetzter Wörter zurück
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "game.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||||
|
|
||||||
|
const char words[][MAX_WORD_LEN] = {
|
||||||
|
"HALLO",
|
||||||
|
"WELT",
|
||||||
|
"AUTO",
|
||||||
|
"BIRNE",
|
||||||
|
"APFEL",
|
||||||
|
"BANANE",
|
||||||
|
"LANGESWORT",
|
||||||
|
"REITSPORT",
|
||||||
|
"APFELBAUM",
|
||||||
|
"DRACULA",
|
||||||
|
"LAPTOP",
|
||||||
|
"BILDSCHRIM",
|
||||||
|
"HAUS"
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
|
||||||
|
|
||||||
|
int placedWords = createWordSalad(salad, MAX_SEARCH_FIELD_LEN, words, wordCount);
|
||||||
|
|
||||||
|
printf("Placed words: %d\n", placedWords);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+2
-2
@@ -43,8 +43,8 @@ graphicalGame.o: $(codefolder)/graphicalGame.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# mein Spiel bauen
|
# mein Spiel bauen
|
||||||
#---------------------------
|
#---------------------------
|
||||||
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
wordsalad_myversion: main.o
|
||||||
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_myversion main.o $(BINARIES)/libwordsalad.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ void print_test_result(int result) {
|
|||||||
printf(RED "FAILED\n" RESET);
|
printf(RED "FAILED\n" RESET);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- Tests für input.c ----------
|
// ---------- Tests für input.c ----------
|
||||||
void test_readWords_simple(void) {
|
void test_readWords_simple(void) {
|
||||||
FILE *f = fopen("testwords_simple.txt", "r");
|
FILE *f = fopen("testwords_simple.txt", "r");
|
||||||
|
|||||||
Reference in new issue
Block a user