generated from freudenreichan/info2Praktikum-Wortsalat
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b28d19e79 | ||
|
|
76ecf700bb | ||
|
|
db3fffe3f5 | ||
|
|
332b3fa0c3 |
@ -10,14 +10,95 @@
|
|||||||
/* * 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 */
|
||||||
|
|
||||||
|
//enum für richtungen
|
||||||
|
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
||||||
|
|
||||||
|
//bestimmt zufällige richtung
|
||||||
|
static Direction randomDirection(void)
|
||||||
|
{
|
||||||
|
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
|
||||||
|
}
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
|
srand(time(NULL)); //damit bei jedme aufruf "neuer zufall" (seed wird immer auf aktuelle zeit gesetzt)
|
||||||
|
|
||||||
|
// spielfeld initialisieren mit nullen
|
||||||
|
for (int y = 0; y < searchFieldLen; y++) {
|
||||||
|
for (int x = 0; x < searchFieldLen; x++) {
|
||||||
|
salad[y][x] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int placedWrdsAmnt = 0;
|
||||||
|
|
||||||
|
//wörter platzieren
|
||||||
|
for(int w = 0; w < wordCount; w++){
|
||||||
|
const char *word = words[w];
|
||||||
|
int len = strlen(word);
|
||||||
|
|
||||||
|
if(len > searchFieldLen )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
|
||||||
|
//versucht max 10 versch. positionen
|
||||||
|
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++){
|
||||||
|
Direction dir = randomDirection();
|
||||||
|
int startX = 0;
|
||||||
|
int startY = 0;
|
||||||
|
|
||||||
|
if(dir == HORIZONTAL){
|
||||||
|
startX = rand() % (searchFieldLen - len + 1); //zb zeile ist 10 breit (0-9), wort 5 lang -> start nur in 0-5 möglich -> 10 - 5 + 1 = 6 (rand() % 6 = 0 bis 5)
|
||||||
|
startY = rand() % searchFieldLen; //egal wo
|
||||||
|
}else{
|
||||||
|
startX = rand() % searchFieldLen;
|
||||||
|
startY = rand() % (searchFieldLen - len + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//passt wort rein? (komplett leer/hat einen überschneidenen buchstaben) --> geht ganze wort platz durch (vert oder horiz), 1 schleife = 1 buchstabe im wort
|
||||||
|
int fits = 1;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
int x = startX + (dir == HORIZONTAL ? i : 0); //wenn zb horiz. dann erhöhe x um i, sonst bleibt x konst.
|
||||||
|
int y = startY + (dir == VERTICAL ? i : 0);
|
||||||
|
//wenn i.d. reihe/spalte ein buchstabe der sich nicht mit dem aktuellen wort überschneidet -> fitted nicht, stopt DIESE for loop
|
||||||
|
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) {
|
||||||
|
fits = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!fits){
|
||||||
|
continue; //wenn wort nicht fitted nächster positionsversuch
|
||||||
|
}
|
||||||
|
|
||||||
|
//wenn wort fitted -> setze ins spielfeld
|
||||||
|
for(int i = 0; i < len; i++){
|
||||||
|
int x = startX + (dir == HORIZONTAL ? i : 0);
|
||||||
|
int y = startY + (dir == VERTICAL ? i : 0);
|
||||||
|
salad[y][x] = word[i];
|
||||||
|
}
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
placedWrdsAmnt++;
|
||||||
|
}
|
||||||
|
//leere felder mit random buchstaben füllen
|
||||||
|
for(int y = 0; y < searchFieldLen; y++){
|
||||||
|
for(int x = 0; x < searchFieldLen; x++){
|
||||||
|
if(salad[y][x] == EMPTY_CHAR)
|
||||||
|
salad[y][x] = (rand() % 26) + 'A';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return placedWrdsAmnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// 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 y = 0; y < searchFieldLen; y++){
|
||||||
|
for(int x = 0; x < searchFieldLen; x++){
|
||||||
|
printf("%c", salad[y][x]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,23 +7,33 @@
|
|||||||
// 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
|
// Read words from file and store in 'words' array
|
||||||
int anzahlWoerter = 0;
|
|
||||||
char zeile[500];
|
char zeile[500];
|
||||||
|
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
|
int anzahlWoerter = 0;
|
||||||
char *wort[] = strtok(zeile, ",; \n");
|
|
||||||
|
//1 durchlauf = 1 zeile, solange wortlimit nicht erreicht und fgets eine zeile liefern kann
|
||||||
|
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
|
||||||
|
char *wort = strtok(zeile, ",; \n");
|
||||||
|
|
||||||
|
//1 durchlauf = 1 wort, solange weiteres wort vorhanden und limit nicht erreicht
|
||||||
|
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
||||||
|
if(strlen(wort) == 0){
|
||||||
|
wort = strtok(NULL, ",; \n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(words[anzahlWoerter], wort);
|
||||||
|
|
||||||
|
//aktuelles wort gross schreiben
|
||||||
|
for(int i = 0; words[anzahlWoerter][i]; i++){
|
||||||
|
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //unsigned damit toupper keie neg. zahlen übergeben bekommt bei zb sonderzeichen und umlauten
|
||||||
|
}
|
||||||
|
|
||||||
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
|
||||||
strncpy(words[anzahlWoerter], wort, anzahlWoerter -1);
|
|
||||||
words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0';
|
|
||||||
anzahlWoerter++;
|
anzahlWoerter++;
|
||||||
*wort = strtok(NULL, ",; \n");
|
wort = strtok(NULL, ",; \n");
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return anzahlWoerter;
|
return anzahlWoerter;
|
||||||
}
|
}
|
||||||
@ -30,19 +30,25 @@ int main(int argc, char *argv[])
|
|||||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //funktion aus input.c, returned anzahl wörter
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //funktion aus game.c, returned in spielfeld platzierte wörter
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// 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
|
||||||
|
int windowWidth = 600;
|
||||||
|
|
||||||
|
//checkt ob alle wörter aus .txt übernommen wurden
|
||||||
|
if(placedWords == wordCount){
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, windowWidth);
|
||||||
|
}else{
|
||||||
|
printf("Fehler: Nicht alle Wörter konnten platziert werden.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Print error message if file couldn't be opened
|
// Print error message if file couldn't be opened
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
|
|||||||
@ -13,6 +13,12 @@ unityfolder = ./unity
|
|||||||
wordsalad_initial:
|
wordsalad_initial:
|
||||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Eigene Version
|
||||||
|
# --------------------------
|
||||||
|
wordsalad_myversion: main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a
|
||||||
|
$(CC) $(CFLAGS) -o wordsalad_myversion main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Normales Spiel bauen
|
# Normales Spiel bauen
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
BIN
Start_Mac/wordsalad_myversion
Executable file
BIN
Start_Mac/wordsalad_myversion
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user