Compare commits

...
13 Commits
3 changed files with 161 additions and 4 deletions

No files matched your search

+90 -1
View File
@@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 20
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: //TODO: Spiellogik implementieren:
@@ -13,11 +13,100 @@
// 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)
{ {
int saladclone[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
int wordsplacedcounter=0;
for (int i=0;i<MAX_SEARCH_FIELD_LEN;i++){ //Feld 1 mit 0ern füllen
for (int j=0;j<MAX_SEARCH_FIELD_LEN;j++) {
saladclone[i][j] = 0;
}
}
srand(time(NULL));
for (int i = 0;i < searchFieldLen; i++) { //Feld 2 mit random Buchstaben füllen
for (int j = 0; j < searchFieldLen; j++) {
salad[i][j] = 'A' + (rand() % 26);
}
}
int wordOrientation[wordCount]; //Array das mit der Anzahl der Wörter gefüllt wird
for (int w = 0;w < wordCount; w++) { //Randomizer, ob Wort horizontal oder vertikal wird
wordOrientation[w] = rand() % 2; //vertikal = 0, horizontal = 1
}
for (int w = 0;w < wordCount; w++) {
const char *word = words[w]; //zeiger word zeigt auf Anfang des aktuellen Wortes
int wordLen = strlen(word); //Länge des Wortes wird ermittelt
int horizontal = wordOrientation[w]; //horizontal bekommt 0 und 1 aus dem array
int wordPlaced = 0;
int check=0;
for (int tries = 0;tries < MAX_RAND_TRIES_PER_WORD && !wordPlaced;tries++) { //Das Wort wird entweder in eine Zeile oder Spalte random plaziert
int row = rand() % searchFieldLen;
int col = rand() % searchFieldLen;
check = 0;
if (horizontal) {
if (col + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt
continue;
for (int i=0;i < wordLen; i++) { //es wird überprüft ob 1er im Feld 1 vorhanden sind
if (saladclone[row][col + i]==1) {
check=1;
}
}
if (check == 1) continue;
printf("Wort: %d,%d \n", row,col);
for (int i = 0;i < wordLen; i++) { //Wort wird in Spalte geschrieben (Feld 2)
salad[row][col + i] = word[i];
saladclone[row][col + i] = 1; //Feld 1 wird mit 1ern gefüllt
}
wordPlaced = 1;
wordsplacedcounter++;
}
else {
if (row + wordLen > searchFieldLen) //erst wird überprüft, ob das Wort überlappt
continue;
for (int i=0;i < wordLen; i++) { //es wird überprüft ob 1er im Feld 1 vorhanden sind
if (saladclone[row+i][col]==1) {
check=1;
}
}
if (check == 1) continue;
printf("Wort: %d,%d \n", row,col);
for (int i = 0;i < wordLen; i++) { //Wort wird in Zeile geschrieben (Feld 2)
salad[row + i][col] = word[i];
saladclone[row+i][col] = 1; //Feld 1 wird mit 1ern gefüllt
}
wordPlaced = 1;
wordsplacedcounter++;
}
}
}
return wordsplacedcounter;
} }
// 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)
{ {
printf("WORTSALAT:\n\n");
for (int i = 0; i < searchFieldLen; i++) {
for (int j = 0; j < searchFieldLen; j++) {
printf("%c ", salad[i][j]);
}
printf("\n");
}
} }
+63 -3
View File
@@ -8,7 +8,67 @@
// 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)
{ {
file = fopen("words.txt", "r"); //liest das doc ein
if (file == NULL) char text [2000];
return -1; char line[200];
int i = 0;
int textcount = 0;
int counter = 0;
char *token;
//einlesen der textdatei in text[] (unterhalb)
while (fgets(line, sizeof(line), file)) {
for (int j = 0; line[j] != '\0'&& i < sizeof(text)-1;j++) {
text[i++] = line[j];
textcount++;
if (textcount>1999) {
break;
}
}
if (textcount>1999) {
printf("Textdatei zu gross!!!");
return 1;
}
}
text[i] = '\0';
//sortierung der wörter in words(unterhalb)
const char *trenner = ",; \n";
token = strtok(text, trenner);
while (token != NULL&& counter < maxWordCount) {
strncpy(words[counter], token, MAX_WORD_LEN - 1);
words[counter][MAX_WORD_LEN - 1] = '\0';
token = strtok(NULL, trenner);
counter++;
}
//alle Buchstaben in Großbuchstaben ändern
for (int h = 0; h < counter; h++) {
for (int check = 0; words[h][check] != '\0'; check++) {
if (words[h][check] >= 'a' && words[h][check] <= 'z') {
words[h][check] -= 32; // direkt ins Array schreiben
}
}
}
return counter;
} }
+8
View File
@@ -35,6 +35,14 @@ 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);
if (placedWords == wordCount) {
showWordSalad(wordSalad, SALAD_SIZE);
startGame(wordSalad,SALAD_SIZE,words,wordCount,1000);
}else {
printf("Es konnten nur %d Woerter plaziert werden.\n", placedWords);
printf("-> Programm abgebrochen!!!");
}
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed