deleted testing lines -- tried to build main but had issues with graphicalGame.c

This commit is contained in:
pvtrx
2025-11-05 14:28:38 +01:00
parent 26efe137cb
commit c6664be88c
10 changed files with 97 additions and 23 deletions
+119
View File
@@ -0,0 +1,119 @@
#include "game.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
{
//testing variables
int MAX_RAND_TRIES_PER_WORD = 10;
int EMPTY_CHAR = 0;
//end of testing variables
memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN * sizeof(salad[0][0]));
int line = 0;
int column = 0;
int wordsplaced = 0;
int wordsaborted = 0;
while (wordsplaced < wordCount) {
int direction = rand() % 2; // 0 = horizontal, 1 = vertical
int wordlen = strlen(words[wordsplaced]);
int placed = 0;
int tries = 0;
while (!placed && tries < MAX_RAND_TRIES_PER_WORD) {
if (direction == 0) { // horizontal
if (wordlen <= searchFieldLen) {
column = rand() % (searchFieldLen - wordlen + 1);
line = rand() % searchFieldLen;
int canPlace = 1;
for (int i = 0; i < wordlen; i++) {
if (salad[line][column + i] != EMPTY_CHAR && salad[line][column + i] != words[wordsplaced][i]) {
canPlace = 0;
break;
}
}
if (canPlace) {
for (int i = 0; i < wordlen; i++) {
salad[line][column + i] = words[wordsplaced][i];
}
placed = 1;
}
}
} else { // vertical
if (wordlen <= searchFieldLen) {
line = rand() % (searchFieldLen - wordlen + 1);
column = rand() % searchFieldLen;
int canPlace = 1;
for (int i = 0; i < wordlen; i++) {
if (salad[line + i][column] != EMPTY_CHAR && salad[line + i][column] != words[wordsplaced][i]) {
canPlace = 0;
break;
}
}
if (canPlace) {
for (int i = 0; i < wordlen; i++) {
salad[line + i][column] = words[wordsplaced][i];
}
placed = 1;
}
}
}
tries++;
}
if (placed) {
wordsplaced++;
}
else if (tries == MAX_RAND_TRIES_PER_WORD) {
printf("Abort trying to place the word -%s-\n", words[wordsplaced]);
wordsplaced++; // skip this word and move to the next
wordsaborted++;
}
}
for (int i = 0; i < searchFieldLen; i++) { // replaces 0 with random letters
for (int j = 0; j < searchFieldLen; j++) {
if (salad[i][j] == EMPTY_CHAR) {
salad[i][j] = 'A' + (rand() % 26);
}
}
}
return wordsplaced;
}
int main(void) {
int SALAD_SIZE = 10;
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
unsigned int searchFieldLen = SALAD_SIZE;
const char words[][MAX_WORD_LEN] = {"TEST", "WORD", "DEINEMUDDAAAA"}; // Add your words here
unsigned int wordCount = 3; // Update this based on number of words
int placedWords;
placedWords = createWordSalad(salad, searchFieldLen, words, wordCount);
for (unsigned int i = 0; i < searchFieldLen; i++) { // code von showWordSalad
for (unsigned int j = 0; j < searchFieldLen; j++) {
printf("%c ", salad[i][j]);
}
printf("\n");
}
//code for main.c:
if (placedWords == wordCount) {
printf("All valid words placed successfully.\n");
} else {
printf("Could not place all words. Placed %d out of %d words.\n", placedWords, wordCount);
}
return 0;
}
+3
View File
@@ -0,0 +1,3 @@
int main(void) {
}
+71
View File
@@ -0,0 +1,71 @@
#include "input.h"
#include <string.h>
#include <ctype.h>
#define MAX_NUMBER_OF_WORDS 100 // wird in main definiert zum testen hier auch definiert!
//TODO:
//eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt
int readWords(FILE*file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int zeilen = 0;
file = fopen("words.txt", "r"); // öffnen der datei (Wird in main gemacht also rausmachen!!!!!!!!!)
if(file == 0)
{
printf("Fehler beim oeffnen der Woerter \n");
return -1;
}
while(fgets(words[zeilen], MAX_WORD_LEN, file) != 0) //auslesen der Zeilen
{
size_t laenge = strlen(words[zeilen]); // Datentyp size_t wird verwendet bei strlen
if(laenge > 0 && words[zeilen][laenge - 1] == '\n') //austauschen des Leerzeichens mit einem Nullzeichen falls nötig
{
words[zeilen][laenge - 1] = '\0';
laenge--; // Länge nach austauschen 1 kleiner weil nullzeichen nicht mitzählt
}
zeilen++;
}
printf("%d \n", zeilen);
fclose(file); // Datei schließen (Wird in main geschlossen also muss man das noch rausmachen!!!!!!!!)
int alt_zeilen = zeilen;
zeilen = 0;
char *teiler = " .,;\n\t\r";
char *woerter;
char aufgeteilt[MAX_NUMBER_OF_WORDS][MAX_LINE_LEN]; //zwischenspeicher fürs aufteilen der Wörter
for(int i = 0; i < alt_zeilen; i++) // Jede Zeile einzeln betrachten
{
woerter = strtok(words[i], teiler);
while(woerter != 0)
{
strncpy(aufgeteilt[zeilen], woerter, MAX_WORD_LEN); // Kopieren der aufgeteilten Wörter in den Zwischenspeicher
zeilen++;
woerter = strtok(NULL, teiler); // NULL sagt das man weiterhin in der selben Zeile bleibt und dort aufteilt
}
}
printf("%d \n", zeilen);
for(int i = 0; i < zeilen; i++)
{
strncpy(words[i], aufgeteilt[i], MAX_WORD_LEN); // überschreiben der Wörter zurück ins alte Array
size_t len = strlen(words[i]);
for(int j = 0; j < len; j++)
{
words[i][j] = toupper(words[i][j]); // groß schreiben
}
}
for(int i = 0; i < zeilen; i++)
{
printf("%s \n", words[i]);
}
return zeilen;
}
int main()
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
FILE*file;
unsigned int wordCount = 0;
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
}