Compare commits

...

2 Commits

Author SHA1 Message Date
d9c39024bb Code game.c 2025-11-04 17:45:30 +01:00
Maximilian Ott
096d400a40 Code game.c 2025-11-04 17:43:28 +01:00
2 changed files with 118 additions and 4 deletions

View File

@ -2,22 +2,127 @@
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_RAND_TRIES_PER_WORD 10
#define MAX_RAND_TRIES_PER_WORD 100
#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)
{
int wordlength;
int direction;
int horizontal;
int vertical;
int possible_coordinate = 0;
int place_word = 0;
// Initialisierung Zufallszahl
srand(time(NULL));
// Generierung des Wortfeldes: Alle Felder auf "Leer" setzen
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
salad[i][j] = '\0';
}
}
for (int w = 0; w < wordCount; w++)
{
wordlength = strlen(words[w]);
possible_coordinate = 0;
// Versuch bis zu MAX_RAND_TRIES_PER_WORD eine Position zu finden
for (int c = 0; c < MAX_RAND_TRIES_PER_WORD && possible_coordinate == 0; c++)
{
possible_coordinate = 1;
direction = rand() % 2;
if (direction == 0) // Vertikale Wortpositionierung
{
vertical = rand() % (searchFieldLen - wordlength + 1);
horizontal = rand() % searchFieldLen;
for (int i = 0; i < wordlength; i++)
{
char current = salad[vertical + i][horizontal];
// Feld muss leer oder mit gleichem Buchstaben belegt sein
if (current != '\0' && current != words[w][i])
{
possible_coordinate = 0;
break;
}
}
}
else // Horizontale Wortpositionierung
{
vertical = rand() % searchFieldLen;
horizontal = rand() % (searchFieldLen - wordlength + 1);
for (int i = 0; i < wordlength; i++)
{
char current = salad[vertical][horizontal + i];
if (current != '\0' && current != words[w][i])
{
possible_coordinate = 0;
break;
}
}
}
}
if (possible_coordinate == 0)
return place_word; // Rückgabe: Anzahl der positionierten Wörter
// Platzierung des Wortes
if (direction == 0) // Vertikal Wortpositionierung
{
for (int i = 0; i < wordlength; i++)
{
salad[vertical + i][horizontal] = words[w][i];
}
}
else // Horizontal Wortpositionierung
{
for (int i = 0; i < wordlength; i++)
{
salad[vertical][horizontal + i] = words[w][i];
}
}
place_word = w + 1;
}
// Auffüllen der leeren Felder
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == '\0')
{
salad[i][j] = 'A' + rand() % 26;
}
}
}
return place_word;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
printf("---Wortsalat---\n");
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
printf("%c", salad[i][j]);
}
printf("\n");
}
}

View File

@ -36,11 +36,20 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords == wordCount)
{
startGame(wordSalad, SALAD_SIZE, words, placedWords, 800);
}
else{
printf("The game can not start! %d words can not be placed!", wordCount - placedWords);
exitCode = EXIT_FAILURE;
}
}
else
{