Files
Wortsalat/Startcode/input-t.c
T

71 lines
2.1 KiB
C

//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.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char *tmp;
int counter = 0;
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){
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;
}