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

+15 -1
View File
@@ -6,6 +6,11 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
typedef enum Richtung {
VERTIKAL = 0;
HORIZONTAL = 1;
};
//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 */
@@ -18,6 +23,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
//Initialisiere spielfeld auf '0' //Initialisiere spielfeld auf '0'
// TODO: memset() // TODO: memset()
//memset(salad, '0', MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){ for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){ for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
salad[i][j] = '0'; salad[i][j] = '0';
@@ -53,7 +59,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
} }
//place word vertically //place word vertically
if(dir == 1){ if(dir == HORIZONTAL){
//see if all neccessary spaces are empty //see if all neccessary spaces are empty
bool cont = false; bool cont = false;
for (int i = 0; i < (int)len; i++){ for (int i = 0; i < (int)len; i++){
@@ -95,5 +101,13 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi
placedWords++; placedWords++;
} }
} }
for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
if(salad[i][j] == '0'){
char c = 'A' + (rand() % 26);
salad[i][j] = c;
}
}
}
return placedWords; return placedWords;
} }
+54 -12
View File
@@ -1,8 +1,15 @@
//Torbens Kopie //Torbens Kopie zum Testen
#include "input.h"
#include <string.h> #include <string.h>
#include <ctype.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: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // 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; char *tmp;
int counter = 0; int counter = 0;
const char delimiters[] = ",!.;:? " char delimiters[] = ",!.;:? \n";
while(fgets(tmp, MAX_WORD_LEN, flie) != NULL){ char *token;
char *token = strtok_r (tmp, delimiters, &delimiters); while(fgets(tmp, MAX_LINE_LEN, file)){
if(strncpy(words[counter], token, MAX_WORD_LEN) == NULL){ printf("Input Line: %s\n", tmp);
//failure token = strtok (tmp, delimiters);
return -1;
}
counter++;
while(token != NULL){ while(token != NULL){
token = strtpk_r (NULL, delimiters, &delimiters); printf("Token:%s\n", token);
strncpy(words[counter], token, MAX_WORD_LEN);
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++; counter++;
} }
} }
return 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;
}