generated from freudenreichan/info2Praktikum-Wortsalat
126 lines
3.8 KiB
C
126 lines
3.8 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 lage;
|
|
int pos_x;
|
|
int pos_y;
|
|
int belegt = 2;
|
|
int belegt_counter;
|
|
int fehlende_woerter=0;
|
|
int startwechsel = 0;
|
|
char demostring[searchFieldLen];
|
|
char vergleichstring[searchFieldLen];
|
|
|
|
//salad befuellen mit "0"
|
|
for (int i=0;i<searchFieldLen; i++){
|
|
for (int ii=0;ii<searchFieldLen; ii++){
|
|
salad[i][ii]= '0';
|
|
}
|
|
salad[i][searchFieldLen] = '\0';
|
|
demostring[i]='0';
|
|
}
|
|
demostring[searchFieldLen-1]='\0';
|
|
|
|
while (wortrest>0){
|
|
belegt_counter = MAX_RAND_TRIES_PER_WORD;
|
|
while (belegt_counter > 0){
|
|
akt_word = (wordCount-wortrest);
|
|
|
|
if(belegt_counter<(MAX_RAND_TRIES_PER_WORD/2)){
|
|
|
|
}
|
|
//horizontal/vertikal
|
|
lage = rand()%2;
|
|
switch (lage){
|
|
|
|
case 0://Waagerecht
|
|
|
|
pos_x = rand()%(searchFieldLen-strlen(words[akt_word]));
|
|
pos_y = rand()%(searchFieldLen);
|
|
|
|
for (int leange_counter = 0;leange_counter<strlen(words[akt_word]);leange_counter++){
|
|
if (salad[pos_x+leange_counter][pos_y] != '0'){ //checken ob alle Felder frei sind
|
|
belegt = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (belegt == 2){
|
|
for(int i = 0; i < strlen(words[akt_word]); i++){ //Inhalte reinschreiben
|
|
salad[pos_x+i][pos_y] = words[akt_word][i];
|
|
}
|
|
belegt = 0;
|
|
}
|
|
break;
|
|
|
|
case 1://Senkrecht
|
|
|
|
pos_x = rand()%(searchFieldLen);
|
|
pos_y = rand()%(searchFieldLen-strlen(words[akt_word]));
|
|
|
|
for (int leange_counter = 0;leange_counter<strlen(words[akt_word]);leange_counter++){
|
|
if (salad[pos_x][pos_y+leange_counter] != '0'){ //checken ob alle Felder frei sind
|
|
belegt = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (belegt == 2){
|
|
for(int i = 0; i < strlen(words[akt_word]); i++){ //Inhalte reinschreiben
|
|
salad[pos_x][pos_y+i] = words[akt_word][i];
|
|
}
|
|
belegt = 0;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
printf("Fehler: game.c: lage-switch");
|
|
break;
|
|
}
|
|
|
|
|
|
if(belegt==0){
|
|
break;
|
|
} //Auslesen Belegtstatus
|
|
else if(belegt == 1){
|
|
belegt_counter --;
|
|
}
|
|
}
|
|
if(belegt_counter==0){
|
|
fehlende_woerter++;
|
|
}else if(belegt ==2){
|
|
printf("Fehler: game.c: Beschreiben des Salats");
|
|
}
|
|
belegt = 2;
|
|
wortrest--;
|
|
startwechsel = 0;
|
|
}
|
|
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');
|
|
}
|
|
}
|