Compare commits
No commits in common. "2998c1c26110e0df09a9770dc5892fd974110467" and "4b028daa2ded314d7e144b8bcac8e3f8d7e2bd2e" have entirely different histories.
2998c1c261
...
4b028daa2d
@ -1,84 +1,23 @@
|
|||||||
#include "input.h"
|
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include <stdlib.h> // für rand(), srand()
|
#include <time.h>
|
||||||
#include <time.h> // für time()
|
#include <stdlib.h>
|
||||||
#include <string.h> // für strlen()
|
#include <string.h>
|
||||||
|
|
||||||
#define EMPTY_CHAR 0
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
|
#define EMPTY_CHAR 0
|
||||||
|
|
||||||
|
//TODO: Spiellogik implementieren:
|
||||||
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
|
* restliche Felder mit zufälligen Buchstaben füllen */
|
||||||
|
|
||||||
|
// 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));
|
|
||||||
int placedWords = 0;
|
|
||||||
|
|
||||||
// Initialisiere das Feld mit 0 (leere Zeichen)
|
|
||||||
for (unsigned int i = 0; i < searchFieldLen; i++) {
|
|
||||||
for (unsigned int j = 0; j < searchFieldLen; j++) {
|
|
||||||
salad[i][j] = EMPTY_CHAR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int w = 0; w < wordCount; w++) {
|
// Prints the word salad to console
|
||||||
const char* word = words[w];
|
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
size_t len = strlen(word);
|
{
|
||||||
int placed = 0;
|
|
||||||
|
|
||||||
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++) {
|
|
||||||
int direction = rand() % 2; // 0 = horizontal, 1 = vertical
|
|
||||||
int row = rand() % searchFieldLen;
|
|
||||||
int col = rand() % searchFieldLen;
|
|
||||||
|
|
||||||
if (direction == 0) { // horizontal
|
|
||||||
if (col + len > searchFieldLen) continue;
|
|
||||||
|
|
||||||
int fits = 1;
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i]) {
|
|
||||||
fits = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fits) {
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
salad[row][col + i] = word[i];
|
|
||||||
}
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
} else { // vertical
|
|
||||||
if (row + len > searchFieldLen) continue;
|
|
||||||
|
|
||||||
int fits = 1;
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i]) {
|
|
||||||
fits = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fits) {
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
salad[row + i][col] = word[i];
|
|
||||||
}
|
|
||||||
placed = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (placed) {
|
|
||||||
placedWords++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fülle leere Felder mit zufälligen Buchstaben
|
|
||||||
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] = 'A' + rand() % 26;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return placedWords;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -1,38 +1,12 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdio.h> //sicherstellen, dass FILE deklariert ist
|
|
||||||
// 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.
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
|
||||||
char buffer[256]; // temporärer Speicher für Zeilen
|
|
||||||
|
|
||||||
// zusätzliche Bedingung "count < maxWordCount"
|
|
||||||
while (count < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL)
|
|
||||||
{
|
|
||||||
// gleiche Trennzeichen, aber Formatierung beibehalten
|
|
||||||
char *token = strtok(buffer, " \t\r\n.,;:!?()[]{}\"'"); // Trennzeichen
|
|
||||||
|
|
||||||
while (token != NULL)
|
|
||||||
{
|
|
||||||
if (count >= maxWordCount)
|
|
||||||
break; // Sicherheitsabbruch auch hier
|
|
||||||
|
|
||||||
// toupper(), um Großbuchstaben zu erzwingen
|
|
||||||
for (int i = 0; token[i]; i++)
|
|
||||||
token[i] = toupper((unsigned char)token[i]);
|
|
||||||
|
|
||||||
// sicheres Kopieren ins Zielarray
|
|
||||||
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
|
||||||
words[count][MAX_WORD_LEN - 1] = '\0'; // String terminieren
|
|
||||||
count++;
|
|
||||||
|
|
||||||
token = strtok(NULL, " \t\r\n.,;:!?()[]{}\"'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
@ -11,6 +11,7 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
// Check if the correct number of arguments is provided
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||||
@ -18,33 +19,33 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
||||||
unsigned int wordCount = 0;
|
unsigned int wordCount = 0;
|
||||||
unsigned int placedWords = 0; // <-- Hierher verschoben
|
|
||||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
|
||||||
|
|
||||||
FILE *file = fopen(argv[1], "r");
|
FILE *file = fopen(argv[1], "r");
|
||||||
|
|
||||||
if(file != NULL)
|
if(file != NULL)
|
||||||
{
|
{
|
||||||
|
unsigned int placedWords = 0;
|
||||||
|
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
||||||
|
|
||||||
|
// Read words from file and store in 'words' array
|
||||||
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
|
||||||
exitCode = EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (placedWords == wordCount)
|
// TODO:
|
||||||
{
|
// Check if all words were successfully placed
|
||||||
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
// Start the game if successful
|
||||||
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Nur %u von %u Wörtern konnten im Buchstabensalat platziert werden.\n", placedWords, wordCount);
|
// Print error message if file couldn't be opened
|
||||||
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user