forked from freudenreichan/info2Praktikum-Wortsalat
104 lines
2.7 KiB
C
104 lines
2.7 KiB
C
#include "game.h"
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.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)
|
|
{
|
|
if (searchFieldLen == 0 ||searchFieldLen > MAX_SEARCH_FIELD_LEN)
|
|
{return 0;}
|
|
|
|
int placedWOrds = 0;
|
|
srand((unsigned int)time(NULL));
|
|
|
|
//Spielfeld leeren:
|
|
for (int r = 0; r < searchFieldLen; r++ )
|
|
{
|
|
for (int c = 0; c < searchFieldLen; c++) {
|
|
salad[r][c] = EMPTY_CHAR;
|
|
}
|
|
}
|
|
|
|
//Wörter platzieren
|
|
for (int w = 0; w < wordCount; w++)
|
|
{
|
|
int len = strlen(words[w]); //Wortlänge festgelegt
|
|
|
|
for (int atempts = 0; atempts < MAX_RAND_TRIES_PER_WORD; atempts++)
|
|
{
|
|
//Muss in for-Schleife sein damit immer neu erzeugt wird
|
|
int axis = rand() % 2;
|
|
int row = rand() % searchFieldLen;
|
|
int column = rand() % searchFieldLen;
|
|
int error = 0;
|
|
|
|
|
|
|
|
|
|
if (axis == 0) //Vertical~~~~~~~~~~~
|
|
{
|
|
//Available Space Check & Wordcollision Avoidance//////
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
if (len + row > searchFieldLen || salad[row+i][column] != EMPTY_CHAR){
|
|
error = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Write Word
|
|
if (error == 0){
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
salad[row+i][column] = words[w][i];
|
|
}
|
|
placedWOrds++;
|
|
break;
|
|
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
|
|
|
|
|
|
|
|
|
|
}else if (axis == 1){ //Horizontal~~~~~~~~~~~~~~~
|
|
|
|
//Available Space Check & Wordcollision Avoidance
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
if (len + column > searchFieldLen || salad[row][column+i] != EMPTY_CHAR){
|
|
error = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Write Word
|
|
if (error == 0){
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
salad[row][column+i] = words[w][i];
|
|
}
|
|
placedWOrds++;
|
|
break;
|
|
}else{printf("Platzierung zum %d .ten Mal fehlgeschlagen!\n", atempts + 1 );}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//Fill empty spaces
|
|
|
|
|
|
|
|
|
|
|
|
return placedWOrds;
|
|
} |