Testdatei läuft jetzt

This commit is contained in:
Lukas Koestner 2025-11-06 08:56:43 +01:00
parent 63aa67c312
commit 81d932da9f
3 changed files with 116 additions and 30 deletions

View File

@ -1,23 +1,84 @@
#include "input.h"
#include "game.h"
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h> // für rand(), srand()
#include <time.h> // für time()
#include <string.h> // für strlen()
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
#define MAX_RAND_TRIES_PER_WORD 10
//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)
{
srand(time(NULL));
int placedWords = 0;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
// Initialisiere das Feld mit 0 (leere Zeichen)
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
salad[i][j] = EMPTY_CHAR;
}
}
for (unsigned int w = 0; w < wordCount; w++) {
const char* word = words[w];
size_t len = strlen(word);
int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
int direction = rand() % 2; // 0 = horizontal, 1 = vertical
int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen;
if (direction == 0) { // horizontal
if (col + len > searchFieldLen) continue;
int fits = 1;
for (size_t i = 0; i < len; i++) {
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i]) {
fits = 0;
break;
}
}
if (fits) {
for (size_t i = 0; i < len; i++) {
salad[row][col + i] = word[i];
}
placed = 1;
}
} else { // vertical
if (row + len > searchFieldLen) continue;
int fits = 1;
for (size_t i = 0; i < len; i++) {
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i]) {
fits = 0;
break;
}
}
if (fits) {
for (size_t i = 0; i < len; i++) {
salad[row + i][col] = word[i];
}
placed = 1;
}
}
}
if (placed) {
placedWords++;
}
}
// Fülle leere Felder mit zufälligen Buchstaben
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = 'A' + rand() % 26;
}
}
}
return placedWords;
}

View File

@ -1,12 +1,38 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h> //sicherstellen, dass FILE deklariert ist
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
unsigned int count = 0;
char buffer[256]; // temporärer Speicher für Zeilen
// zusätzliche Bedingung "count < maxWordCount"
while (count < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL)
{
// gleiche Trennzeichen, aber Formatierung beibehalten
char *token = strtok(buffer, " \t\r\n.,;:!?()[]{}\"'"); // Trennzeichen
while (token != NULL)
{
if (count >= maxWordCount)
break; // Sicherheitsabbruch auch hier
// toupper(), um Großbuchstaben zu erzwingen
for (int i = 0; token[i]; i++)
token[i] = toupper((unsigned char)token[i]);
// sicheres Kopieren ins Zielarray
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // String terminieren
count++;
token = strtok(NULL, " \t\r\n.,;:!?()[]{}\"'");
}
}
return count;
}

View File

@ -11,7 +11,6 @@ int main(int argc, char *argv[])
{
int exitCode = EXIT_SUCCESS;
// Check if the correct number of arguments is provided
if(argc != 2)
{
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
@ -19,35 +18,35 @@ int main(int argc, char *argv[])
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
unsigned int wordCount = 0;
unsigned int placedWords = 0; // <-- Hierher verschoben
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
FILE *file = fopen(argv[1], "r");
if(file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file);
// 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
}
else
{
// Print error message if file couldn't be opened
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
exitCode = EXIT_FAILURE;
}
if (placedWords == wordCount)
{
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
else
{
fprintf(stderr, "Nur %u von %u Wörtern konnten im Buchstabensalat platziert werden.\n", placedWords, wordCount);
exitCode = EXIT_FAILURE;
}
}
return exitCode;