game.c: prototype done; input.c: started

This commit is contained in:
Nick Haller 2025-10-23 21:03:50 +02:00
parent 10a40bce76
commit 90bf5a0067
2 changed files with 148 additions and 2 deletions

View File

@ -6,18 +6,147 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: // TODO: Spiellogik implementieren:
/* * 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 // 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)
{ {
// set seed
srand(time(NULL));
int wordsPlaced = 0;
// loop through all words
for (int n = 0; n < wordCount; n++)
{
int checkSquare, letterPlaced, retries = 0;
int wordLength = strlen(words[n]);
// get random coordinates
int rndX = rand() % (searchFieldLen - wordLength + 1);
int rndY = rand() % (searchFieldLen - wordLength + 1);
// 0 = horizontal 1 = vertikal
int direction = rand() % 2;
switch (direction)
{
case 0:
retries = 0;
while (letterPlaced < wordLength)
{
// checks if word fits into gamefield
while (!(checkSquare == wordLength) && (retries < 10))
{
checkSquare = 0;
// checks if squares to paste into is already taken
for (int x = rndX; x < (rndX + wordLength); x++)
{
if (salad[rndY][x] == '\0')
checkSquare++;
}
// new coords when doesnt fit
if (checkSquare != wordLength)
{
rndX = rand() % (searchFieldLen - wordLength + 1);
rndY = rand() % (searchFieldLen - wordLength + 1);
retries++;
}
}
if (retries >= 10)
{
printf("Word %u couldn't be placed", n);
break;
}
// pastes word into the line
if (checkSquare == wordLength)
{
for (int x = 0; x < wordLength; x++)
{
salad[rndY][rndX + x] = words[n][x];
letterPlaced++;
}
}
}
wordsPlaced++;
case 1:
retries = 0;
while (letterPlaced < wordLength)
{
// checks if word fits into gamefield
while (!(checkSquare == wordLength) && (retries < 10))
{
checkSquare = 0;
// checks if squares to paste into is already taken
for (int y = rndY; y < (rndY + wordLength); y++)
{
if (salad[y][rndX] == '\0')
checkSquare++;
}
// new coords when doesnt fit
if (checkSquare != wordLength)
{
rndX = rand() % (searchFieldLen - wordLength + 1);
rndY = rand() % (searchFieldLen - wordLength + 1);
retries++;
}
}
if (retries >= 10)
{
printf("Word %u couldn't be placed", n);
break;
}
// pastes word into the line
if (checkSquare == wordLength)
{
for (int y = 0; y < wordLength; y++)
{
salad[y + rndY][rndX] = words[n][y];
letterPlaced++;
}
}
}
wordsPlaced++;
}
}
//fill voids
for(int x = 0; x < searchFieldLen; x++)
{
for(int y = 0; y < searchFieldLen; y++)
{
if(salad[y][x] == '\0')
{
// random number from 65 (A) to 90 (Z) with ASCII
salad[y][x] = (char) (rand() % (25 + 1) + 65);
}
}
}
} }
// 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)
{ {
int i, j = 0;
for (i = 0; i < searchFieldLen; i++)
{
for (j = 0; j < searchFieldLen - 1; j++)
{
printf(" %c,", salad[i][j]);
}
printf(" %c\n", salad[i][searchFieldLen]);
}
} }

View File

@ -8,5 +8,22 @@
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
int wordLength = MAX_WORD_LEN;
//open file in read mode
file = fopen("words.txt", "r");
if(file == NULL)
{
printf("\nNot able to opne file.\n");
}
else
{
int n = 0;
while (n < MAX_WORDS && fscanf(file, "%49s", words[n]) == 1)
n++;
fclose(file);
}
} }