Compare commits
9
Commits
93db739464
..
baeuml
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51e9f406fd | ||
|
|
a290fba480 | ||
|
|
b76c87a19e | ||
|
|
783bb755e9 | ||
|
|
1cb18630be | ||
|
|
24466628da | ||
|
|
fbf443e02d | ||
|
|
42a8142f03 | ||
|
|
917f79708e |
No files matched your search
+54
-2
@@ -11,7 +11,59 @@
|
|||||||
* 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));
|
||||||
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
|
||||||
|
salad[i][j] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int failed = 0;
|
||||||
|
for(int i = 0; i < wordCount; i++){
|
||||||
|
int wordlen = strlen(words[i]);
|
||||||
|
if(wordlen >= searchFieldLen){
|
||||||
|
failed++;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if(rand() % 2){
|
||||||
|
//place horizontal
|
||||||
|
while(wordlen){
|
||||||
|
int row = rand() % searchFieldLen;
|
||||||
|
int col = rand() % (searchFieldLen - wordlen);
|
||||||
|
int j = 0;
|
||||||
|
for(; (j <= wordlen) && (salad[row][col + j] == 1); j++){}
|
||||||
|
if(j == wordlen + 1){
|
||||||
|
//word can fit
|
||||||
|
wordlen = 0;
|
||||||
|
strcpy(&(salad[row][col]), words[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//place vertical
|
||||||
|
while(wordlen){
|
||||||
|
int row = rand() % (searchFieldLen - wordlen);
|
||||||
|
int col = rand() % searchFieldLen;
|
||||||
|
int j = 0;
|
||||||
|
for(; (j <= wordlen) && (salad[row + j][col] == 1); j++){}
|
||||||
|
if(j == wordlen + 1){
|
||||||
|
//word can fit
|
||||||
|
for(int k = 0; k <= wordlen; k++){
|
||||||
|
salad[row + k][col] = words[i][k];
|
||||||
|
}
|
||||||
|
wordlen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//fill remaining spots with random chars
|
||||||
|
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){
|
||||||
|
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++){
|
||||||
|
salad[i][j] = (salad[i][j] == 1) || (salad[i][j] == 0) ? (rand() % 25) + 65 : salad[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wordCount - failed;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,19 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#define DELIM_COUNT 4
|
||||||
|
|
||||||
|
int find_delim(const char* line, char delim_list[DELIM_COUNT]){
|
||||||
|
int i = 0;
|
||||||
|
while(line[i] != '\n'){
|
||||||
|
for(int j = 0; j < DELIM_COUNT; j++){
|
||||||
|
if(line[i] == delim_list[j])
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||||
@@ -8,5 +21,33 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
char line[MAX_LINE_LEN];
|
||||||
|
int count = 0;
|
||||||
|
int word_end = 0;
|
||||||
|
int word_start = 0;
|
||||||
|
char delim_list[DELIM_COUNT] = {'\n', ';', ',', ' '};
|
||||||
|
while (fgets(line, MAX_LINE_LEN, file) != NULL &&
|
||||||
|
count < maxWordCount)
|
||||||
|
{
|
||||||
|
word_end = 0;
|
||||||
|
word_start = 0;
|
||||||
|
while(line[word_start ? word_start - 1 : 0] != '\n' && count < maxWordCount){
|
||||||
|
word_end = find_delim(line + word_start, delim_list);
|
||||||
|
// Wort ins Array kopieren
|
||||||
|
if(word_end) {
|
||||||
|
strncpy(words[count], line + word_start, word_end);
|
||||||
|
words[count][word_end] = '\0';
|
||||||
|
|
||||||
|
// In Großbuchstaben umwandeln
|
||||||
|
for (int i = 0; words[count][i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
words[count][i] = toupper((unsigned char)words[count][i]);
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
word_start += word_end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
+12
-2
@@ -37,9 +37,19 @@ int main(int argc, char *argv[])
|
|||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
|
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
// Start the game if successful
|
if (placedWords == wordCount)
|
||||||
// error message if some words couldn't be placed
|
{
|
||||||
|
// Start the game if successful
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// error message if some words couldn't be placed
|
||||||
|
fprintf(stderr, "Error: Only %u of %u words could be placed.\n", placedWords, wordCount);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in new issue
Block a user