Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef4fb6519b |
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 1000
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
//TODO: Spiellogik implementieren:
|
||||||
@@ -13,77 +13,5 @@
|
|||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// 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 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 i,j;
|
|
||||||
int Woertergesetzt = 0;
|
|
||||||
for (i=0 ; i<MAX_SEARCH_FIELD_LEN;i++)
|
|
||||||
{
|
|
||||||
for (j=0 ; j<MAX_SEARCH_FIELD_LEN;j++)
|
|
||||||
{
|
|
||||||
salad[i][j] = EMPTY_CHAR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (unsigned int k = 0 ; k < wordCount ; k++)
|
|
||||||
{
|
|
||||||
int richtigplatziert = 0;
|
|
||||||
unsigned int laenge = strlen(words[k]);
|
|
||||||
for (int f = 0 ; f < MAX_RAND_TRIES_PER_WORD && !richtigplatziert ; f++)
|
|
||||||
{
|
|
||||||
unsigned int ausrichtung, zeile, spalte;
|
|
||||||
ausrichtung = rand() % 2;
|
|
||||||
zeile = rand() % (searchFieldLen);
|
|
||||||
spalte = rand() % (searchFieldLen);
|
|
||||||
|
|
||||||
if (ausrichtung == 0) // horizontal
|
|
||||||
{
|
|
||||||
if (spalte + laenge <= searchFieldLen)
|
|
||||||
{
|
|
||||||
int moeglichePosition = 1;
|
|
||||||
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
|
||||||
{
|
|
||||||
if (salad[zeile][spalte + i1] != EMPTY_CHAR)
|
|
||||||
moeglichePosition = 0;
|
|
||||||
}
|
|
||||||
if (moeglichePosition)
|
|
||||||
{
|
|
||||||
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
|
||||||
salad[zeile][spalte + i1] = words[k][i1];
|
|
||||||
richtigplatziert = 1;
|
|
||||||
Woertergesetzt++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else // vertikal
|
|
||||||
{
|
|
||||||
if (zeile + laenge <= searchFieldLen)
|
|
||||||
{
|
|
||||||
int moeglichePosition = 1;
|
|
||||||
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
|
||||||
{
|
|
||||||
if (salad[zeile + i1][spalte] != EMPTY_CHAR)
|
|
||||||
moeglichePosition = 0;
|
|
||||||
}
|
|
||||||
if (moeglichePosition)
|
|
||||||
{
|
|
||||||
for (unsigned int i1 = 0; i1 < laenge; i1++)
|
|
||||||
salad[zeile + i1][spalte] = words[k][i1];
|
|
||||||
richtigplatziert = 1;
|
|
||||||
Woertergesetzt++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// restliche Felder mit zufaelligen Buchstaben fuellen
|
|
||||||
for (i = 0; i < (int)searchFieldLen; i++)
|
|
||||||
{
|
|
||||||
for (j = 0; j < (int)searchFieldLen; j++)
|
|
||||||
{
|
|
||||||
if (salad[i][j] == EMPTY_CHAR)
|
|
||||||
salad[i][j] = 'A' + rand() % 26;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Woertergesetzt;
|
|
||||||
}
|
}
|
||||||
@@ -36,10 +36,17 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
if (wordCount == placedWords)
|
||||||
// Check if all words were successfully placed
|
{
|
||||||
// Start the game if successful
|
|
||||||
// error message if some words couldn't be placed
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
fprintf(stderr, "Could only place %u of %u words!\n", placedWords, wordCount);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in new issue
Block a user