hello world
This commit is contained in:
commit
3a00180f02
129
game.c
Normal file
129
game.c
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#include "game.h"
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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)
|
||||||
|
// {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Prints the word salad to console
|
||||||
|
// void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
// {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Funktion, die einen zufälligen Großbuchstaben generiert
|
||||||
|
char randomLetter()
|
||||||
|
{
|
||||||
|
return 'A' + rand() % 26;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hauptfunktion: Erstellt das Wortsalat-Spiel
|
||||||
|
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
|
||||||
|
unsigned int searchFieldLen,
|
||||||
|
const char words[][MAX_WORD_LEN],
|
||||||
|
unsigned int wordCount)
|
||||||
|
{
|
||||||
|
// 1. Leeres Spielfeld initialisieren
|
||||||
|
for (unsigned int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
for (unsigned int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
|
// 2. Jedes Wort zufällig im Spielfeld platzieren
|
||||||
|
for (unsigned int w = 0; w < wordCount; w++)
|
||||||
|
{
|
||||||
|
const char *word = words[w];
|
||||||
|
size_t len = strlen(word);
|
||||||
|
if (len > searchFieldLen)
|
||||||
|
continue; // Überspringen, falls das Wort zu lang ist
|
||||||
|
|
||||||
|
int placed = 0;
|
||||||
|
for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++)
|
||||||
|
{
|
||||||
|
int dir = rand() % 2; // 0 = horizontal, 1 = vertikal
|
||||||
|
int row = rand() % searchFieldLen;
|
||||||
|
int col = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
// Prüfen, ob das Wort an die Position passt
|
||||||
|
if (dir == 0 && col + len <= searchFieldLen)
|
||||||
|
{
|
||||||
|
// Prüfen, ob keine Überschneidungen mit anderen Buchstaben auftreten
|
||||||
|
int ok = 1;
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i])
|
||||||
|
{
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
salad[row][col + i] = word[i];
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (dir == 1 && row + len <= searchFieldLen)
|
||||||
|
{
|
||||||
|
int ok = 1;
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i])
|
||||||
|
{
|
||||||
|
ok = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ok)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
salad[row + i][col] = word[i];
|
||||||
|
placed = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Leere Felder mit zufälligen Buchstaben füllen
|
||||||
|
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] = randomLetter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // 0 = erfolgreich
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion zur Anzeige des Spielfelds in der Konsole
|
||||||
|
void showWordSalad(const 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++)
|
||||||
|
{
|
||||||
|
printf("%c ", salad[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
58
input.c
Normal file
58
input.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "input.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// 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
|
||||||
|
// int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
|
// {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Wörter aus einer Datei lesen und im Array 'words' speichern
|
||||||
|
// Gibt die Anzahl der gelesenen Wörter zurück oder -1 im Fehlerfall.
|
||||||
|
|
||||||
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
|
{
|
||||||
|
if (file == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Fehler: Datei ist NULL.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int count = 0;
|
||||||
|
char line[MAX_WORD_LEN + 5]; // etwas Puffer, um Überlauf zu vermeiden
|
||||||
|
|
||||||
|
// Zeilen einzeln lesen, bis EOF erreicht oder das Limit erreicht ist
|
||||||
|
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
// Zeilenumbruch '\n' oder '\r\n' entfernen
|
||||||
|
line[strcspn(line, "\r\n")] = '\0';
|
||||||
|
|
||||||
|
// Leere Zeilen überspringen
|
||||||
|
if (strlen(line) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Leerzeichen am Anfang und Ende entfernen
|
||||||
|
char *start = line;
|
||||||
|
while (isspace((unsigned char)*start))
|
||||||
|
start++;
|
||||||
|
|
||||||
|
char *end = start + strlen(start) - 1;
|
||||||
|
while (end > start && isspace((unsigned char)*end))
|
||||||
|
{
|
||||||
|
*end = '\0';
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sichere Kopie in das Array 'words'
|
||||||
|
strncpy(words[count], start, MAX_WORD_LEN - 1);
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
64
main.c
Normal file
64
main.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "input.h"
|
||||||
|
#include "game.h"
|
||||||
|
#include "graphicalGame.h"
|
||||||
|
|
||||||
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
// Check if the correct number of arguments is provided
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
||||||
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
|
FILE *file = fopen(argv[1], "r");
|
||||||
|
|
||||||
|
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);
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
// Create the word salad by placing words into grid
|
||||||
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// Check if all words were successfully placed
|
||||||
|
if (placedWords < wordCount)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Warning: Only %d out of %d words could be placed in the salad.\n",
|
||||||
|
placedWords, wordCount);
|
||||||
|
fprintf(stderr, "Some words could not be placed.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("All %u words placed successfully!\n", wordCount);
|
||||||
|
}
|
||||||
|
// Start the game if successful
|
||||||
|
showWordSalad(wordSalad, SALAD_SIZE);
|
||||||
|
// error message if some words couldn't be placed
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Print error message if file couldn't be opened
|
||||||
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
|
exitCode = EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return exitCode;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user