Files
Wortsalat/Startcode/main.c
T
2026-10-29 15:27:06 +01:00

109 lines
3.5 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "input.h"
#include "game.h"
#include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 36
int checkSalad(char words[][MAX_WORD_LEN], char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int placedWords, int wordCount){
//Iteriere über alle verfügbaren Wörter
int foundWords = 0;
//suche horizontal
for(int i = 0; i < wordCount; i++){
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++){
if(strstr(wordSalad[h], words[i]) != NULL){
foundWords++;
break;
}
}
}
//flip Board
char copySalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
for (int i = 0; i< MAX_SEARCH_FIELD_LEN; i++){
for (int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
copySalad[i][j] = wordSalad[j][i];
}
}
// sucher vertikal
for(int i = 0; i < wordCount; i++){
for(int h = 0; h < MAX_SEARCH_FIELD_LEN; h++){
if(strstr(copySalad[h], words[i]) != NULL){
foundWords++;
break;
}
}
}
if(foundWords == placedWords) return 0;
return -1;
}
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]);
exitCode = EXIT_FAILURE;
}
else
{
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
int wordCount = 0;
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 (return input.c / game.c <=0)
// Prüfen, ob input.c (readWords) einen Fehler oder 0 Wörter zurückgegeben hat
if (wordCount <= 0)
{
fprintf(stderr, "Fehler: Es konnten keine Wörter eingelesen werden (input.c Rückgabewert <= 0).\n");
exitCode = EXIT_FAILURE;
}
// Prüfen, ob game.c (createWordSalad) einen Fehler zurückgegeben hat (<= 0) oder ob nicht alle eingelesenen Wörter platziert werden konnten
else if (placedWords <= 0 || placedWords < wordCount)
{
fprintf(stderr, "Fehler: Nicht alle Wörter konnten im Wortsalat platziert werden (game.c Rückgabewert <= 0).\n");
exitCode = EXIT_FAILURE;
}
//keine Ahnung wiegroß pixelSize sein soll
startGame(wordSalad, MAX_SEARCH_FIELD_LEN, words, placedWords, MAX_SEARCH_FIELD_LEN * SALAD_SIZE);
}
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;
}
}
return exitCode;
}