Setze random Buchstaben in die noch leeren Felder (hab bei mir die unit-tests nicht zum laufen bekommen)

This commit is contained in:
sengpielto106561 committed 2026-10-25 19:43:39 +01:00
1 parent a02b363e20
commit ce17403a1b
2 files changed
+69 -13

No files matched your search

+54 -12
View File
@@ -1,8 +1,15 @@
//Torbens Kopie
#include "input.h"
//Torbens Kopie zum Testen
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100
#define MAX_WORD_LEN 100
#define MAX_LINE_LEN 1024
#define SALAD_SIZE 20
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
@@ -11,19 +18,54 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char *tmp;
int counter = 0;
const char delimiters[] = ",!.;:? "
while(fgets(tmp, MAX_WORD_LEN, flie) != NULL){
char *token = strtok_r (tmp, delimiters, &delimiters);
if(strncpy(words[counter], token, MAX_WORD_LEN) == NULL){
//failure
return -1;
}
counter++;
char delimiters[] = ",!.;:? \n";
char *token;
while(fgets(tmp, MAX_LINE_LEN, file)){
printf("Input Line: %s\n", tmp);
token = strtok (tmp, delimiters);
while(token != NULL){
token = strtpk_r (NULL, delimiters, &delimiters);
strncpy(words[counter], token, MAX_WORD_LEN);
printf("Token:%s\n", token);
size_t len = strlen(token);
char copy[len+1];
strncpy(copy, token, len+1);
copy[len] = '\0';
strncpy(words[counter+1], copy, len);
words[counter+1][len] = '\0';
printf("Word: %s\n", words[counter+1]);
token = strtok (NULL, delimiters);
counter++;
}
}
return counter;
}
int main(void){
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
unsigned int wordCount = 0;
FILE *file = fopen("words.txt", "r");
if(file != NULL)
{
unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word sala
// Read words from file and store in 'words' array
printf("readWords start\n");
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
printf("%d", wordCount);
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
}
return EXIT_SUCCESS;
}