generated from freudenreichan/info2Praktikum-Wortsalat
main.c: Spielstart und Fehlerprüfung implementiert
This commit is contained in:
commit
7a7062a925
4 files changed
+213
-7
No files matched your search
+134
-2
@@ -2,16 +2,148 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h> //für toupper
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
//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 */
|
||||||
|
|
||||||
// Creates the word salad by placing words randomly and filling empty spaces
|
// Creates the word salad by placing words randomly and filling empty spaces
|
||||||
|
|
||||||
|
void fillArrayEmpty(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||||
|
void generateCoordinates(unsigned int searchFieldLen, int *platzierung, int *xKoord, int *yKoord);
|
||||||
|
int placeWords(unsigned int searchFieldLen, const char *wort, int platzierung, int xKoord, int yKoord, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]);
|
||||||
|
void fillRandLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
|
||||||
|
|
||||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
||||||
{
|
{
|
||||||
|
fillArrayEmpty(salad, searchFieldLen);
|
||||||
|
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
|
int numberWords = 0;
|
||||||
|
for (unsigned int i = 0; i < wordCount; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
const char *wort = 0;
|
||||||
|
wort = words[i];
|
||||||
|
|
||||||
|
for (int k = 0; k < MAX_RAND_TRIES_PER_WORD; k++)
|
||||||
|
{
|
||||||
|
int platzierung = 0;
|
||||||
|
int xKoord = 0;
|
||||||
|
int yKoord = 0;
|
||||||
|
int success = 0;
|
||||||
|
|
||||||
|
generateCoordinates(searchFieldLen, &platzierung, &xKoord, &yKoord);
|
||||||
|
success = placeWords(searchFieldLen, wort, platzierung, xKoord, yKoord, salad);
|
||||||
|
|
||||||
|
if (success != 0)
|
||||||
|
{
|
||||||
|
numberWords++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fillRandLetters(salad, searchFieldLen);
|
||||||
|
|
||||||
|
return numberWords;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fillArrayEmpty(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateCoordinates(unsigned int searchFieldLen, int *platzierung, int *xKoord, int *yKoord)
|
||||||
|
{
|
||||||
|
*platzierung = (unsigned int)rand() % 2; // 0=Horizont.; 1=Vertik.
|
||||||
|
*xKoord = (unsigned int)rand() % searchFieldLen;
|
||||||
|
*yKoord = (unsigned int)rand() % searchFieldLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
int placeWords(unsigned int searchFieldLen, const char *wort, int platzierung, int xKoord, int yKoord, char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN])
|
||||||
|
{
|
||||||
|
|
||||||
|
if (platzierung == 0) // Horizontal
|
||||||
|
{
|
||||||
|
size_t len = strnlen(wort, MAX_WORD_LEN);
|
||||||
|
|
||||||
|
if ((size_t)xKoord + len > searchFieldLen) //Addition da unsigned int
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
char c = salad[yKoord][xKoord + j];
|
||||||
|
char w = (char)toupper((unsigned char)wort[j]); //Umwandlung in Großbuchstaben
|
||||||
|
if (c != EMPTY_CHAR && c != w)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
salad[yKoord][xKoord + j] = (char)toupper((unsigned char)wort[j]); //Umwandlung in Großbuchstaben
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (platzierung == 1) // Vertikal (analog zu Horizontal)
|
||||||
|
{
|
||||||
|
size_t len = strnlen(wort, MAX_WORD_LEN);
|
||||||
|
|
||||||
|
if ((size_t)yKoord + len > searchFieldLen)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
char c = salad[yKoord + j][xKoord];
|
||||||
|
char w = (char)toupper((unsigned char)wort[j]);
|
||||||
|
if (c != EMPTY_CHAR && c != w)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < len; j++)
|
||||||
|
{
|
||||||
|
salad[yKoord + j][xKoord] = (char)toupper((unsigned char)wort[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fillRandLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
if (salad[i][j] == EMPTY_CHAR)
|
||||||
|
{
|
||||||
|
salad[i][j] = (unsigned int)rand() % 26 + 'A'; // Assci A=65='A', Z=90
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,5 +8,51 @@
|
|||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
|
int c;
|
||||||
|
unsigned int i = 0; // Wort
|
||||||
|
unsigned int j = 0; // Zeichen im Wort
|
||||||
|
|
||||||
|
while ((c = fgetc(file)) != EOF)
|
||||||
|
{
|
||||||
|
if (c == ',' || c == ';' || c == '\n' || c == ' ')
|
||||||
|
{
|
||||||
|
if (j > 0)
|
||||||
|
{
|
||||||
|
words[i][j] = '\0';
|
||||||
|
i++;
|
||||||
|
j = 0;
|
||||||
|
|
||||||
|
if (i >= maxWordCount)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Auf Pufferüberlauf im Wort achten
|
||||||
|
if (j < MAX_WORD_LEN - 1)
|
||||||
|
{
|
||||||
|
words[i][j] = (char)toupper(c);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ans Ende \0 hinzufügen
|
||||||
|
if (j > 0 && i < maxWordCount)
|
||||||
|
{
|
||||||
|
words[i][j] = '\0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MIT CHAT GEMACHT ZUR KONTROLLE: Konsolenausgabe zur Kontrolle der eingelesenen Wörter
|
||||||
|
printf("Eingelesene Wörter (%u Stück):\n", i);
|
||||||
|
for (unsigned int idx = 0; idx < i; idx++)
|
||||||
|
{
|
||||||
|
printf("%u. Entry: %s\n", idx + 1, words[idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
+32
-4
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
#define WINDOW_WIDTH 800 //KI hat 800 geraten mal sehen ob es passt
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -36,10 +37,37 @@ int main(int argc, char *argv[])
|
|||||||
// Create the word salad by placing words into grid
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Check if all words were successfully placed
|
if (wordCount == 0)
|
||||||
// Start the game if successful
|
{
|
||||||
// error message if some words couldn't be placed
|
fprintf(stderr, "Keine Wörter in %s gefunden.\n", argv[1]);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prüfung ob manche Wörter nicht platziert wurden
|
||||||
|
else if(placedWords != wordCount){
|
||||||
|
|
||||||
|
fprintf(stderr, "Es konnten nur %u von %u Wörtern platziert werden, such den Fehler\n", placedWords, wordCount);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
//sonst alles erfolgreich verlaufen
|
||||||
|
else
|
||||||
|
{
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, WINDOW_WIDTH);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
+1
-1
@@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
|||||||
Monster
|
Monster
|
||||||
Hydra;Frankenstein
|
Hydra;Frankenstein
|
||||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||||
Gespenst, Oger, Zombie, Baum
|
Gespenst, Oger, Hannes
|
||||||
Reference in new issue
Block a user