implementation of game.c with working example to generate gameboard

This commit is contained in:
Felix Richardt 2025-11-05 23:53:57 +01:00
parent e7f448f912
commit 3a7cb92591

View File

@ -2,6 +2,8 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
@ -10,14 +12,138 @@
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces //Enum for direction
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) typedef enum {
{ HORIZONTAL,
VERTICAL
} Direction;
//this function clears gameboard
static void initializeSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen){
for (int i = 0; i < searchFieldLen; i++){
for (int j = 0; j < searchFieldLen; j++){
salad[i][j] = EMPTY_CHAR;
}
}
}
// this function checks if the word is placable at the current coordinates
static int isPlacementPossible(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char* word, int row, int col, Direction dir){
int len = strlen(word);
for (int k = 0; k < len; k++){
if (dir == HORIZONTAL){
if (salad[row][col + k] != EMPTY_CHAR) return 0;
} else { //Vertikal
if (salad[row + k][col] != EMPTY_CHAR) return 0;
}
}
return 1;
}
//this function places the word at the current coordinate
static void placeWord (char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], const char* word, int row, int col, Direction dir){
int len = strlen(word);
for (int k = 0; k < len; k++){
if (dir == HORIZONTAL){
salad[row][col + k] = word[k];
} else {
salad[row + k][col] = word[k];
}
}
}
// this function fills the empty cells with random characters
static void fillEmtpyCellsRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen){
for (int i = 0; i < searchFieldLen; i++){
for (int j = 0; j < searchFieldLen; j++){
if (salad[i][j] == EMPTY_CHAR){
salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A';
}
}
}
}
// 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 placedWords = 0;
initializeSalad(salad, searchFieldLen);
for (int i = 0; i < wordCount; i++){
const char* currentWord = words[i];
int len = strlen(currentWord);
int tries = 0;
int success = 0;
while (tries < MAX_RAND_TRIES_PER_WORD && !success){
Direction dir = rand() % 2;
int row, col;
if (dir == HORIZONTAL){
row = rand() % searchFieldLen;
col = rand() % (searchFieldLen - len + 1);
} else { //Vertikal
row = rand() % (searchFieldLen - len + 1);
col = rand() % searchFieldLen;
}
if (isPlacementPossible(salad, currentWord, row, col, dir)){
placeWord(salad, currentWord, row, col, dir);
success = 1;
placedWords++;
}
tries++;
}
}
fillEmtpyCellsRandom(salad, searchFieldLen);
return placedWords;
} }
// Prints the word salad to console // Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{ {
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
printf("%c ", salad[i][j]);
}
printf("\n");
}
} }
int main(void)
{
// Spielfeldgröße
unsigned int searchFieldLen = 15;
// Beispiel-Wortliste
const char words[][MAX_WORD_LEN] = {
"AUTO",
"BAUM",
"HAUS",
"KATZE",
"SONNE"
};
unsigned int wordCount = sizeof(words) / sizeof(words[0]);
// Spielfeld-Array
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
// Zufall initialisieren (einmalig!)
srand((unsigned int)time(NULL));
// Wortsalat erzeugen
int placed = createWordSalad(salad, searchFieldLen, words, wordCount);
printf("Platziert: %d von %u Woertern\n\n", placed, wordCount);
// Wortsalat ausgeben
showWordSalad(salad, searchFieldLen);
return 0;
}