generated from freudenreichan/info2Praktikum-Wortsalat
105 lines
2.8 KiB
C
105 lines
2.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 */
|
|
|
|
// enum to make it more clear what de current orientation is
|
|
typedef enum
|
|
{
|
|
HORIZONTAL,
|
|
VERTIKAL
|
|
} orientation;
|
|
|
|
|
|
// new struct for handing over the entire wordPosition
|
|
// alignment is HORIZONTAL or VERTICAL
|
|
// rowOrColumn is the int of the colum or row the word is suposed to be placed in
|
|
// startingCell is the int of where in the row/colum the first leter of the word suposed to be placed
|
|
typedef struct
|
|
{
|
|
orientation alignment;
|
|
unsigned int rowOrColumn;
|
|
unsigned int startingCell;
|
|
} wordPosition;
|
|
|
|
|
|
// Choses a random Position for the current Word
|
|
wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength)
|
|
{
|
|
wordPosition position = {0, 0, 0};
|
|
|
|
srand(time(NULL));
|
|
position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL;
|
|
position.rowOrColumn = rand()%(searchFieldLen);
|
|
position.startingCell = rand()%(searchFieldLen - wordLength);
|
|
|
|
return position;
|
|
}
|
|
|
|
|
|
// Checks if the current Position is free
|
|
// returns 1 if the Position is free
|
|
// returns 0 if the Position is occupied
|
|
int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position,unsigned int wordLength, unsigned int serchFieldLen)
|
|
{
|
|
int i = 0;
|
|
int letterFound = 0;
|
|
int positionfree = 0;
|
|
|
|
if (position.alignment == HORIZONTAL)
|
|
{
|
|
for (i = position.startingCell; i < serchFieldLen; i++)
|
|
{
|
|
if ((salad[position.rowOrColumn][i] < 'A') || (salad[position.rowOrColumn][i] > 'z'))
|
|
{
|
|
letterFound = 1;
|
|
}
|
|
}
|
|
}
|
|
else if (position.alignment == VERTIKAL)
|
|
{
|
|
for (i = position.startingCell; i < serchFieldLen; i++)
|
|
{
|
|
if ((salad[i][position.rowOrColumn] < 'A') || (salad[i][position.rowOrColumn] > 'z'))
|
|
{
|
|
letterFound = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
positionfree = !letterFound;
|
|
|
|
return positionfree;
|
|
}
|
|
|
|
|
|
// 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 i = 0;
|
|
wordPosition currentWordPosition = {0,0,0};
|
|
|
|
for (i = 1; i <= wordCount; i++)
|
|
{
|
|
currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// Prints the word salad to console
|
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
|
{
|
|
|
|
}
|
|
|