Author SHA1 Message Date
schneidersi103495 f5830c24eb game, main vollendet 2026-10-28 11:30:55 +01:00
schneidersi103495 fdb0390b59 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	Startcode/game.c
2026-10-28 10:59:57 +01:00
schneidersi103495 7d7e410f49 game vervollständigen 2026-10-28 10:54:43 +01:00
schreiberma103864 d09b63ea08 Merge pull request 'Implementierung von game.c' (#2) from markus-game into main
Reviewed-on: #2
2026-10-28 09:39:47 +00:00
Markus Schreiber bfef59e0a6 Implementierung von game.c 2026-10-28 10:24:13 +01:00
dunkellu103850 3ab8b26e05 Input C. 2026-10-28 09:13:17 +01:00
dunkellu103850 7d518049a1 TEST 0123456789 2026-10-13 14:48:43 +02:00
schneidersi103495 8556667227 Den Test wieder rückgängig machen 2026-10-09 16:00:12 +02:00
3 changed files with 164 additions and 7 deletions

No files matched your search

+107 -2
View File
@@ -11,7 +11,112 @@
* 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)
{ {
printf("hallo"); unsigned int placedWords = 0;
// 1. Spielfeld leeren
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
salad[i][j] = EMPTY_CHAR;
}
}
// 2. Alle Wörter durchlaufen
for (unsigned int i = 0; i < wordCount; i++) {
size_t wordLen = strlen(words[i]);
// Leere oder zu lange Wörter überspringen
if (wordLen == 0 || wordLen > searchFieldLen) {
continue;
}
// 3. Platzierungsversuche
for (int attempt = 0;
attempt < MAX_RAND_TRIES_PER_WORD;
attempt++) {
unsigned int row = rand() % searchFieldLen;
unsigned int col = rand() % searchFieldLen;
int direction = rand() % 2;
int canPlace = 1;
// 4. Horizontale Platzierung prüfen
if (direction == 0) {
if (col + wordLen > searchFieldLen) {
continue;
}
for (size_t j = 0; j < wordLen; j++) {
if (salad[row][col + j] != EMPTY_CHAR &&
salad[row][col + j] != words[i][j]) {
canPlace = 0;
break;
}
}
}
// 5. Vertikale Platzierung prüfen
else {
if (row + wordLen > searchFieldLen) {
continue;
}
for (size_t j = 0; j < wordLen; j++) {
if (salad[row + j][col] != EMPTY_CHAR &&
salad[row + j][col] != words[i][j]) {
canPlace = 0;
break;
}
}
}
// Platzierung nicht möglich
if (!canPlace) {
continue;
}
// 6. Wort tatsächlich platzieren
for (size_t j = 0; j < wordLen; j++) {
if (direction == 0) {
salad[row][col + j] = words[i][j];
}
else {
salad[row + j][col] = words[i][j];
}
}
// Erfolgreich platziertes Wort zählen
placedWords++;
// Nächstes Wort bearbeiten
break;
}
}
// 7. Leere Felder mit Zufallsbuchstaben 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] = 'A' + rand() % 26;
}
}
}
// 8. Anzahl erfolgreich platzierter Wörter zurückgeben
return (int)placedWords;
} }
+36 -1
View File
@@ -4,9 +4,44 @@
// 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.
// Test 123
// 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)
{ {
int c = 0;
int count = 0;
int letterCount = 0;
while (count < maxWordCount)
{
while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen
{
if (isspace(c) || !isalpha(c)) {
if (letterCount > 0) {
break; //Wort fertig eingelesen
}
continue; // mehrere Leerzeichen oder Sonderzeichen überspringen
}
if (letterCount < MAX_WORD_LEN - 1) //wörter Buchstabenweise einlesen
{
words[count][letterCount] = (char)toupper(c);
letterCount++;
}
}
if (letterCount == 0) {
break; //kein weiteres Wort gefunden
}
words[count][letterCount] = '\0';
letterCount = 0;
count++;
if (c == EOF) {
break;
}
}
return count;
} }
+21 -4
View File
@@ -4,12 +4,16 @@
#include "game.h" #include "game.h"
#include "graphicalGame.h" #include "graphicalGame.h"
#define MAX_NUMBER_OF_WORDS 100 #define MAX_NUMBER_OF_WORDS 100 //Maximale Anzahl an Wörtern, die eingelesen werden können
#define SALAD_SIZE 20 #define SALAD_SIZE 20 //Größe des Wortsalats 20 Zeilen * 20 Spalten
int main(int argc, char *argv[]) // Hauptfunktion des Programms
// argc = Anzahl der übergebenen Kommandozeilenargumente
// argv = enthält die übergebenen Argumente
int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS; // Speichert, ob das Programm erfolgreich beendet wird
// EXIT_SUCCESS = kein Fehler
// Check if the correct number of arguments is provided // Check if the correct number of arguments is provided
if(argc != 2) if(argc != 2)
@@ -36,6 +40,19 @@ 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)
{
startGame(wordSalad, SALAD_SIZE, words, wordCount,800);
}
else
{
fprintf(stderr, "Not all words could be placed.\n");
exitCode = EXIT_FAILURE;
}
// TODO: // TODO:
// Check if all words were successfully placed // Check if all words were successfully placed
// Start the game if successful // Start the game if successful