93 lines
2.8 KiB
C

#include "game.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Input eine datei erstellen und einfügen sodass wie eine andere datei zu implementierne und dazu noch Ende und Wichtig ! noch main funktion einfügen //
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
#define MAX_WORDS 50
#define MAX_WORD_LENGTH 30
typedef enum {HORIZONTAL, VERTIKAL} Direction;
//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 placed_count=0;
for (int r=0; r< searchFieldLen; r++) {
for (int c=0;c< searchFieldLen; c++) {
salad[r][c] = EMPTY_CHAR;
}
}
for (int i = 0; i < wordCount; i++) {
const char *word = words[i];
int len = strlen(word);
int placed =0;
for (int try = 0; try < MAX_RAND_TRIES_PER_WORD && !placed; try++) {
Direction dir = (rand()%2==0) ? HORIZONTAL : VERTIKAL;
int r=rand()%searchFieldLen;
int c=rand()%searchFieldLen;
if (dir == HORIZONTAL && (c + len > searchFieldLen)) {
continue;
}
if (dir == VERTIKAL && (r + len > searchFieldLen)) {
continue;
}
int can_place = 1;
for (int k =0; k < len; k++) {
char existting_char;
if (dir == HORIZONTAL) {
existting_char = salad[r][c+k];
} else {
existting_char = salad[r+k][c];
}
if (existting_char != EMPTY_CHAR && existting_char != word[k]) {
can_place=0;
break;
}
}
if (can_place) {
for (int k =0; k < len; k++) {
if (dir == HORIZONTAL) {
salad[r][c+k] = word[k];
} else {
salad[r+k][c] = word[k];
}
}
placed = 1;
placed_count++;
}
}
}
//Restliche feledr ausfüllen
for (int r=0; r < searchFieldLen; r++) {
for (int c=0; c < searchFieldLen; c++) {
if (salad[r][c] == EMPTY_CHAR) {
salad[r][c] = 'A'+(rand()%26);
}
}
}
return placed_count;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
printf("\n--- WORTSALAT ---\n");
for (int r=0; r < searchFieldLen; r++) {
for (int c=0; c < searchFieldLen; c++) {
printf("%c", salad[r][c] ? salad[r][c] : '.');
}
printf("\n");
}
printf("-----------------\n");
}