generated from freudenreichan/info2Praktikum-Wortsalat
105 lines
3.5 KiB
C
105 lines
3.5 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_RAND_TRIES_PER_WORD 10
|
|
#define EMPTY_CHAR 0
|
|
|
|
//TODO: Spiellogik implementieren:
|
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
|
|
|
//edited
|
|
typedef enum { HORIZONTAL, VERTICAL } Direction;
|
|
|
|
//edited: bestimmt zufällige richtung
|
|
static Direction randomDirection(void)
|
|
{
|
|
return (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
|
|
}
|
|
|
|
//edited
|
|
// 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)
|
|
{
|
|
srand(time(NULL)); // seed bei jedem funktionsaufruf neu setzen
|
|
|
|
// spielfeld initialisieren (leer)
|
|
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, 1 schleife = 1 wort
|
|
for(int w = 0; w < wordCount; w++){
|
|
const char *word = words[w]; // *word ist immer ein wort von "words" (zeigt auf eine zeile)
|
|
int len = strlen(word);
|
|
|
|
if(len > searchFieldLen ) // wenn wort zu lang, nimm nächstes wort
|
|
continue;
|
|
|
|
int placed = 0;
|
|
|
|
// versuche mehrere random positionen, 1 schleife = 1 position
|
|
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); //zeile ist 10 (0-9) breit, wort 5 lang -> höchstens in 5-9 -> 10 - 5 + 1 = 6 (startet random iwo von 0 bis 5)✅
|
|
startY = rand() % searchFieldLen; //egal
|
|
}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);
|
|
int y = startY + (dir == VERTICAL ? i : 0);
|
|
if (salad[y][x] != EMPTY_CHAR && salad[y][x] != word[i]) { // wenn die reihe mit einem buchstaben befüllt der sich nd mit wort überschneidet -> fitted nd
|
|
fits = 0;
|
|
break;
|
|
}
|
|
}
|
|
if(!fits){
|
|
continue; // versucht nächste position
|
|
}
|
|
|
|
//place word wenn posi passt
|
|
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
|
|
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");
|
|
}
|
|
}
|