game.c implementiert und getestet, sollte jetzt immer kompilieren :)
This commit is contained in:
1 parent
f0bb20bab0
commit
0344fc3566
6 files changed
+745
-18
No files matched your search
+109
-5
@@ -5,13 +5,117 @@
|
||||
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
#define SIMPLE_MODE 1 // keine Reverse einträge?
|
||||
|
||||
typedef enum
|
||||
{
|
||||
HORIZONTAL,
|
||||
VERTICAL
|
||||
} Direction;
|
||||
|
||||
static int canPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char *word, unsigned int wordLen, unsigned int row, unsigned int col, Direction dir){
|
||||
for(unsigned int k = 0; k < wordLen; k++){
|
||||
//iteriert durch das 2D Grid je nach Richtung
|
||||
unsigned int r = (dir == VERTICAL) ? row + k : row;
|
||||
unsigned int c = (dir == HORIZONTAL) ? col + k: col;
|
||||
|
||||
// Falls das Feld leer ist oder der gleiche Buchstabe drinnen ist.
|
||||
if(salad[r][c] != EMPTY_CHAR && salad[r][c] != word[k])
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char *word, unsigned int wordLen, unsigned int row, unsigned int col, Direction dir){
|
||||
for(unsigned int k = 0; k < wordLen; k++){
|
||||
//iteriert durch das 2D Grid je nach Richtung
|
||||
unsigned int r = (dir == VERTICAL) ? row + k : row;
|
||||
unsigned int c = (dir == HORIZONTAL) ? col + k : col;
|
||||
|
||||
//schreibt das wort
|
||||
salad[r][c] = word[k];
|
||||
}
|
||||
}
|
||||
|
||||
static int tryPlaceWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char *word){
|
||||
unsigned int wordLen = strlen(word);
|
||||
|
||||
if(wordLen == 0 || wordLen > searchFieldLen) // sichergehen, dass es ein valides Wort ist
|
||||
return 0;
|
||||
|
||||
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD; tries++){
|
||||
Direction dir = (rand() % 2 == 0 ) ? HORIZONTAL : VERTICAL;
|
||||
unsigned int searchSize;
|
||||
unsigned int row, col;
|
||||
|
||||
/* Hatte ueberlegt hier einen schwer Modus zubauen der auch woerter rueckwaerts setzt, war dann aber zu faul.
|
||||
if(SIMPLE_MODE){
|
||||
searchSize = searchFieldLen - wordLen;
|
||||
} else {
|
||||
searchSize = searchFieldLen;
|
||||
}
|
||||
*/
|
||||
searchSize = searchFieldLen - wordLen;
|
||||
|
||||
if(dir == HORIZONTAL)
|
||||
{
|
||||
row = rand() % searchFieldLen; // Macht das sinn? Wenn 412 % bspw. 8 = 4
|
||||
col = rand() % (searchSize + 1); // Bspw. 154 % ((20 - 6) +1) = 14
|
||||
}
|
||||
else
|
||||
{
|
||||
row = rand() % (searchSize + 1);
|
||||
col = rand() % searchFieldLen;
|
||||
}
|
||||
|
||||
if(canPlaceWord(salad, word, wordLen, row, col, dir)) // schaut nach ob es platzieren kann
|
||||
{
|
||||
placeWord(salad, word, wordLen, row, col, dir); // platziert das wort, falls ja
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// füllt restliche Leere Felder mit random Buchstaben.
|
||||
static void fillEmpty(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen){
|
||||
for(unsigned int r = 0; r < searchFieldLen; r++){
|
||||
for(unsigned int c = 0; c < searchFieldLen; c++){
|
||||
if(salad[r][c] == EMPTY_CHAR){
|
||||
salad[r][c] = 'A' + (rand() % 26); // start mit A also 0100 0001/Dec(65)und addiert dann 0-25 um bis Z abzudecken.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//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)
|
||||
{
|
||||
static int isSeeded = 0;
|
||||
int placedWords = 0;
|
||||
|
||||
}
|
||||
if (searchFieldLen > MAX_SEARCH_FIELD_LEN){
|
||||
return 0;
|
||||
}
|
||||
// stellt sich wieder haben Random Seed
|
||||
if(!isSeeded){
|
||||
srand((unsigned int)time(NULL));
|
||||
isSeeded = 1;
|
||||
}
|
||||
//setzt alle einträge im array auf 0
|
||||
for(unsigned int r = 0; r < searchFieldLen; r++){
|
||||
for(unsigned int c = 0; c < searchFieldLen; c++){
|
||||
salad[r][c] = EMPTY_CHAR;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop für alle Wörter
|
||||
for(unsigned int i = 0; i < wordCount; i++){
|
||||
if(tryPlaceWord(salad, searchFieldLen, words[i])){
|
||||
placedWords++;
|
||||
}
|
||||
}
|
||||
fillEmpty(salad, searchFieldLen);
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
Reference in new issue
Block a user