Compare commits
2 Commits
tobis_bran
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ad56c9c57 | |||
| eaa16ed84c |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +1,3 @@
|
|||||||
makefile/
|
makefile/
|
||||||
.vscode/
|
.vscode/
|
||||||
*.exe
|
*.exe
|
||||||
Start_Linux/makefile
|
|
||||||
Start_Windows/makefile
|
|
||||||
@ -6,76 +6,18 @@
|
|||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
void emptyArray();
|
|
||||||
int placeWord();
|
|
||||||
void fillEmptySpots();
|
|
||||||
|
|
||||||
//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
|
||||||
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)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
|
||||||
emptyArray(salad,MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
|
||||||
placeWord(salad,words);
|
|
||||||
fillEmptySpots(salad, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
// Prints the word salad to console
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
{
|
{
|
||||||
for(int i = 0; i <= MAX_SEARCH_FIELD_LEN; i++ )
|
|
||||||
{
|
|
||||||
for(int j = 0; j <= MAX_SEARCH_FIELD_LEN; j++)
|
|
||||||
{
|
|
||||||
printf("[%c]",salad[i][j]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void emptyArray(char array[][], int arrayLength)
|
|
||||||
{
|
|
||||||
char* element = (char*) &array;
|
|
||||||
for(int i = 0; i < arrayLength; i++ )
|
|
||||||
{
|
|
||||||
*element = EMPTY_CHAR;
|
|
||||||
element++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int placeWord(char intoArray[][], char insertedWord[][])
|
|
||||||
{
|
|
||||||
int numberWord;
|
|
||||||
int tries;
|
|
||||||
while(tries <= MAX_RAND_TRIES_PER_WORD)
|
|
||||||
{
|
|
||||||
int xCord = rand() % MAX_SEARCH_FIELD_LEN;
|
|
||||||
int yCord = rand() % MAX_SEARCH_FIELD_LEN;
|
|
||||||
int isHorizontal = rand() % 2;
|
|
||||||
unsigned int wordLength = strlen(insertedWord[numberWord][0]);
|
|
||||||
if(isHorizontal)
|
|
||||||
{
|
|
||||||
if(xCord + sizeof(insertedWord[numberWord][0]) <= MAX_SEARCH_FIELD_LEN)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void fillEmptySpots(char array[][], int arrayLength)
|
|
||||||
{
|
|
||||||
char* element = (char*) &array;
|
|
||||||
for(int i = 0; i < arrayLength; i++ )
|
|
||||||
{
|
|
||||||
if(*element == EMPTY_CHAR)
|
|
||||||
{
|
|
||||||
*element == rand() % ('Z' - 'A' + 1) + 'A';
|
|
||||||
}
|
|
||||||
element++;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,36 +1,12 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei
|
|
||||||
// (words.txt) einliest und als C-String zurückgibt.
|
// 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)
|
|
||||||
|
// Read words from file and store in 'words' array
|
||||||
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
char puffer[MAX_WORD_LEN];
|
|
||||||
file = fopen("words.txt", "r");
|
|
||||||
if (file == NULL)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char teiler[] = " ;,.\n";
|
|
||||||
char aktuellesWort;
|
|
||||||
int wortAnzahl = 0;
|
|
||||||
|
|
||||||
while (fgets(puffer, MAX_WORD_LEN, file) != NULL)
|
|
||||||
{
|
|
||||||
|
|
||||||
aktuellesWort = strtok(puffer, teiler);
|
|
||||||
|
|
||||||
while (aktuellesWort != NULL && wortAnzahl < maxWordCount)
|
|
||||||
{
|
|
||||||
strncpy(words[wortAnzahl], aktuellesWort,sizeof(words[wortAnzahl]) - 1);
|
|
||||||
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] = '\0';
|
|
||||||
wortAnzahl++; // Nächstes Wort
|
|
||||||
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fclose(file);
|
|
||||||
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
|
||||||
}
|
}
|
||||||
@ -38,18 +38,8 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
if(placedWords == wordCount)
|
// Start the game if successful
|
||||||
{
|
// error message if some words couldn't be placed
|
||||||
// Start the game if successful
|
|
||||||
startGame(wordSalad,MAX_NUMBER_OF_WORDS,placedWords, wordCount,1024);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// error message if some words couldn't be placed
|
|
||||||
fprintf(stderr, "Could not place every word \n");
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -1,54 +1,12 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <ctype.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
// (words.txt) einliest und als C-String zurückgibt.
|
|
||||||
|
|
||||||
// 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],
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
unsigned int maxWordCount) {
|
{
|
||||||
|
|
||||||
if (file == NULL) {
|
}
|
||||||
|
|
||||||
perror("File couldn't be opened");
|
|
||||||
}
|
|
||||||
|
|
||||||
// zunächst fehlerhaften String einlesen und in anderem Array
|
|
||||||
// zwischenspeichern
|
|
||||||
|
|
||||||
char fehlerhafterString[MAX_LINE_LEN];
|
|
||||||
char *teiler = " ;,.\n";
|
|
||||||
char *aktuellesWort;
|
|
||||||
int wortAnzahl = 0;
|
|
||||||
|
|
||||||
while (fgets(fehlerhafterString, sizeof(fehlerhafterString), file) !=
|
|
||||||
NULL && // während mit fgets alle Zeichen aus dem file eingelesen
|
|
||||||
// werden
|
|
||||||
wortAnzahl < maxWordCount) {
|
|
||||||
|
|
||||||
aktuellesWort =
|
|
||||||
strtok(fehlerhafterString, teiler); // Erstes Wort mit strtok aus dem
|
|
||||||
// fehlerhaften String herauslösen
|
|
||||||
|
|
||||||
while (aktuellesWort != NULL &&
|
|
||||||
wortAnzahl < maxWordCount) { // while strtok nicht am Ende ist und
|
|
||||||
// noch Wörter in words passen
|
|
||||||
|
|
||||||
strncpy(words[wortAnzahl], aktuellesWort,
|
|
||||||
sizeof(words[wortAnzahl]) -
|
|
||||||
1); // mit strcpy das aktuelle Wort in words kopieren
|
|
||||||
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] =
|
|
||||||
'\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des
|
|
||||||
// Worts setzen
|
|
||||||
|
|
||||||
wortAnzahl++; // Nächstes Wort
|
|
||||||
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user