generated from freudenreichan/info2Praktikum-Wortsalat
151 lines
5.9 KiB
C
151 lines
5.9 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 */
|
|
|
|
// 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));
|
|
|
|
int wortrest = wordCount;
|
|
int akt_word;
|
|
int belegtcounter;
|
|
int lage;
|
|
int spalte_zeile;
|
|
int fehlende_woerter;
|
|
char saladcpy[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
|
char demostring[MAX_WORD_LEN];
|
|
char comp_string[MAX_WORD_LEN];
|
|
int zerocounter;
|
|
int maxzero[2];
|
|
int ersterbuchstabe;
|
|
int erledigt;
|
|
|
|
|
|
//salad befuellen mit "0"
|
|
for (int i=0;i<searchFieldLen; i++){
|
|
for (int ii=0;ii<searchFieldLen; ii++){
|
|
salad[i][ii]= '0';
|
|
saladcpy[i][ii]= '0';
|
|
}
|
|
salad[i][searchFieldLen] = '\0';
|
|
saladcpy[i][searchFieldLen] = '\0';
|
|
demostring[i]='0';
|
|
}
|
|
demostring[searchFieldLen-1]='\0';
|
|
|
|
|
|
while(wortrest>0){
|
|
akt_word = wordCount - wortrest;
|
|
lage = rand()%2;
|
|
erledigt = 0;
|
|
belegtcounter = MAX_RAND_TRIES_PER_WORD;
|
|
while(belegtcounter>0){
|
|
|
|
strncpy(comp_string,demostring,MAX_WORD_LEN);
|
|
strncpy(comp_string,words[akt_word],strlen(words[akt_word]));
|
|
|
|
zerocounter = 1;
|
|
maxzero[0] = 1;
|
|
spalte_zeile=rand()%searchFieldLen;
|
|
switch (lage){
|
|
|
|
case 0://Waagerecht
|
|
|
|
for(int i=1;i<searchFieldLen;i++){
|
|
if(salad[spalte_zeile][i-1]=='0' && salad[spalte_zeile][i]=='0'){
|
|
zerocounter++;
|
|
if(zerocounter>maxzero[0]){
|
|
maxzero[0] = zerocounter; //Menge an Nullen
|
|
maxzero[1] = i; //Pos der letzten Null
|
|
}
|
|
}else{
|
|
zerocounter = 1;
|
|
}
|
|
}
|
|
if(maxzero[0]>=strlen(words[akt_word])){
|
|
ersterbuchstabe = maxzero[1]-(maxzero[0])+1+rand()%(maxzero[0]-strlen(words[akt_word])+1); //Start = letzter freier Platz - anzahl Platz + 1 + rand()%(freierPlatz-benötigterPlatz +1)
|
|
for(int i=0;i<strlen(words[akt_word]);i++){ // (teoretischer erster Startpunkt) (für richtigen Startwert) freieLücken + 1 wegen Startpunkt
|
|
salad[spalte_zeile][ersterbuchstabe+i]=words[akt_word][i];
|
|
}
|
|
for(int i=0;i<searchFieldLen;i++){
|
|
for(int ii=0;ii<searchFieldLen;ii++){
|
|
saladcpy[ii][i]=salad[i][ii];
|
|
}
|
|
}
|
|
erledigt =1;
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
case 1://Senkrecht
|
|
|
|
for(int i=1;i<searchFieldLen;i++){
|
|
if(saladcpy[spalte_zeile][i-1]=='0' && saladcpy[spalte_zeile][i]=='0'){
|
|
zerocounter++;
|
|
if(zerocounter>maxzero[0]){
|
|
maxzero[0] = zerocounter; //Menge an Nullen
|
|
maxzero[1] = i; //Pos der letzten Null
|
|
}
|
|
}else{
|
|
zerocounter = 1;
|
|
}
|
|
}
|
|
if(maxzero[0]>=strlen(words[akt_word])){
|
|
ersterbuchstabe = maxzero[1]-(maxzero[0])+1+rand()%(maxzero[0]-strlen(words[akt_word])+1); //Start = letzter freier Platz - anzahl Platz + 1 + rand()%(freierPlatz-benötigterPlatz +1)
|
|
for(int i=0;i<strlen(words[akt_word]);i++){ // (teoretischer erster Startpunkt) (für richtigen Startwert) freieLücken + 1 wegen Startpunkt
|
|
saladcpy[spalte_zeile][ersterbuchstabe+i]=words[akt_word][i];
|
|
}
|
|
for(int i=0;i<searchFieldLen;i++){
|
|
for(int ii=0;ii<searchFieldLen;ii++){
|
|
salad[ii][i]=saladcpy[i][ii];
|
|
}
|
|
}
|
|
erledigt = 1;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
printf("Fehler: game.c: lage-switch");
|
|
break;
|
|
}
|
|
if(erledigt==1){
|
|
break;
|
|
}
|
|
belegtcounter--;
|
|
}
|
|
if (belegtcounter==0){
|
|
fehlende_woerter++;
|
|
}
|
|
wortrest--;
|
|
}
|
|
for(int i=0;i<searchFieldLen;i++){ //Rest vom Feld füllen
|
|
for(int ii=0;ii<searchFieldLen;ii++){
|
|
if(salad[i][ii]=='0'){
|
|
salad[i][ii] = 'A' + rand()%26;
|
|
}
|
|
}
|
|
}
|
|
return wordCount-fehlende_woerter;
|
|
}
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
for(int i=0;i<searchFieldLen;i++){
|
|
printf("%s",salad[i]);
|
|
//for(int ii=0;ii<searchFieldLen+1; ii++){
|
|
// putchar(salad[i][ii]);
|
|
//}
|
|
putchar('\n');
|
|
}
|
|
} |