Compare commits

...

31 Commits
main ... main

Author SHA1 Message Date
9b6699b543
seed random number 2025-11-07 08:02:30 +01:00
e3e5d4d434
no magic numbers 2025-11-06 19:40:15 +01:00
97ed155236
adapt gui to missing words 2025-11-06 19:37:13 +01:00
b9ca7ff70c
simplify gitignore 2025-11-06 19:31:09 +01:00
02134e96e6
set game up to start if some words could not be fitted into the grid 2025-11-06 19:29:40 +01:00
b4f891d331
Merge pull request 'simon' (#4) from simon into main 2025-11-03 20:17:20 +01:00
4ba1251b56
set up graphical game 2025-11-03 20:12:14 +01:00
cbf806ae1b
sync linux folder
Co-authored-by: Fabrice <fabrice.reintsch@gmail.com>
2025-11-03 20:05:08 +01:00
90342761fc Merge branch 'main' into simon 2025-11-03 18:41:50 +00:00
Fabrice
7319049bea game.c finished 2025-11-03 19:28:59 +01:00
96917cc4e9
Merge branch 'main' into simon 2025-11-02 20:41:00 +01:00
69f9945fe0
remove fclose() because it's already called in main.c 2025-11-02 20:28:35 +01:00
0aa5c5e9e3
update gitignore 2025-11-02 20:25:54 +01:00
ed4a9c7520
sync linux folder 2025-11-02 20:24:02 +01:00
26bc089865 Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/wiesendsi102436/info2Praktikum-Wortsalat 2025-11-02 14:38:05 +01:00
90827c9ad6 input.c besteht alle Tests 2025-11-02 13:21:15 +01:00
0ee04e2b9e
update .gitignore 2025-10-31 12:55:00 +01:00
c95c264a3a
don't exit if some words couldn't be placed 2025-10-31 12:45:15 +01:00
Fabrice
147fc502eb kleine Änderungen 2025-10-31 09:29:12 +01:00
27eafd14cc Merge pull request 'simon' (#3) from simon into main
Reviewed-on: wiesendsi102436/info2Praktikum-Wortsalat#3
2025-10-31 08:05:56 +00:00
4041e9ee9b
clean up 2025-10-31 09:05:06 +01:00
11871e8c65
return error if some words couldn't be placed 2025-10-31 08:55:47 +01:00
6244fad3d4
update gitignore 2025-10-31 08:34:32 +01:00
67a8247535 merge upstream 2025-10-31 07:30:19 +00:00
5fbc71cf03 Merge pull request 'simon' (#2) from simon into main
Reviewed-on: wiesendsi102436/info2Praktikum-Wortsalat#2
2025-10-31 07:20:04 +00:00
c97f4bd97d
sync linux folder 2025-10-31 08:19:03 +01:00
1098b0f8c0
init random number generator 2025-10-31 08:18:19 +01:00
Fabrice
dc9b59395b game.c bearbeitet,noch nicht fertig 2025-10-31 00:25:02 +01:00
d7c0c30de9 Merge pull request 'add gitignore file' (#1) from gitignore into main
Reviewed-on: wiesendsi102436/info2Praktikum-Wortsalat#1
2025-10-24 07:25:50 +00:00
dcb1e44c98
add more gcc output files 2025-10-24 09:17:31 +02:00
970ce3049a
add .gitignore and ignore wordsalad_initial bin file 2025-10-24 08:13:24 +02:00
9 changed files with 321 additions and 18 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
wordsalad_initial
runTests
wordsalad
*.o
*.exe
testwords_delims.txt
testwords_empty.txt
testwords_simple.txt
.vscode

View File

@ -6,18 +6,118 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
// 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, char words[][MAX_WORD_LEN], unsigned int wordCount)
{
srand(time(NULL));
int row, col, placedWords = 0;
// Gesamtes Feld auf 0 setzen
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
salad[row][col] = EMPTY_CHAR;
}
}
// Wortlänge wird für jedes Wort erfasst
for (int w = 0; w < wordCount; w++)
{
const char *word = words[w];
int wordLen = strlen(word);
if (wordLen == 0 || wordLen > searchFieldLen)
{
continue;
}
int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
{
int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
row = 0;
col = 0;
if (dir == 0)
{ // horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
row = rand() % searchFieldLen;
col = rand() % (searchFieldLen - wordLen + 1);
}
else
{ // vertikal
row = rand() % (searchFieldLen - wordLen + 1);
col = rand() % searchFieldLen;
}
int placeable = 1;
for (int i = 0; i < wordLen; i++)
{ // Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR))
{
placeable = 0;
break;
}
}
if (placeable)
{ // wenn es keine Überschneidungen gibt, wird Wort platziert
for (int i = 0; i < wordLen; i++)
{
if (dir == 0)
{
salad[row][col + i] = word[i];
}
else
{
salad[row + i][col] = word[i];
}
}
// copy actually placed words to the front of the array
// it's safe to use strcpy here
strcpy(words[placedWords], word);
placed = 1;
placedWords++;
}
}
}
// Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = 'A' + (rand() % 26);
}
}
}
// return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
return placedWords;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
int row, col;
// Ausgabe des gesamten Feldes
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
printf("%c ", salad[row][col]);
}
printf("\n");
}
}

View File

@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 100
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, char words[][MAX_WORD_LEN], unsigned int wordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif

View File

@ -1,4 +1,5 @@
#include "input.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
@ -6,7 +7,39 @@
// 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)
{
char buffer[MAX_WORD_LEN];
unsigned int count = 0;
if (file == NULL)
{
return 1;
}
while (fgets(buffer, sizeof(buffer), file) != NULL)
{
// Zerlege Zeile in einzelne Wörter anhand der Trennzeichen
char *token = strtok(buffer, " ,;\n");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]);
}
// Token in das words-Array kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren
count++;
// Nächsten Token holen
token = strtok(NULL, " ,;\n");
}
}
return count;
}

View File

@ -6,13 +6,14 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_WIDTH 800
int main(int argc, char *argv[])
{
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]);
exitCode = EXIT_FAILURE;
@ -24,7 +25,7 @@ int main(int argc, char *argv[])
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
@ -36,11 +37,24 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords < wordCount)
{
int notPlacedNum = wordCount - placedWords;
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
}
// Start game if at least 1 word was successfully placed
if (placedWords > 0)
{
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
}
else
{
fprintf(stderr, "no word could be placed");
exitCode = EXIT_FAILURE;
}
}
else
{

View File

@ -6,18 +6,118 @@
#define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0
//TODO: Spiellogik implementieren:
// 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, char words[][MAX_WORD_LEN], unsigned int wordCount)
{
srand(time(NULL));
int row, col, placedWords = 0;
// Gesamtes Feld auf 0 setzen
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
salad[row][col] = EMPTY_CHAR;
}
}
// Wortlänge wird für jedes Wort erfasst
for (int w = 0; w < wordCount; w++)
{
const char *word = words[w];
int wordLen = strlen(word);
if (wordLen == 0 || wordLen > searchFieldLen)
{
continue;
}
int placed = 0;
for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
{
int dir = (rand() % 2); // 0 oder 1 zur Festlegung von vertikal oder horizontal
row = 0;
col = 0;
if (dir == 0)
{ // horizontal //Startpunkt so festlegen, dass Wort nicht aus Feld raus ragt
row = rand() % searchFieldLen;
col = rand() % (searchFieldLen - wordLen + 1);
}
else
{ // vertikal
row = rand() % (searchFieldLen - wordLen + 1);
col = rand() % searchFieldLen;
}
int placeable = 1;
for (int i = 0; i < wordLen; i++)
{ // Felder ab Startpunkt werden durchgegangen und gecheckt, ob Felder auf Null sind:
// JA =>Wort wird im nächsten Schritt platziert, NEIN => Abbruch, Neuer Startpunkt wird festgelegt
if ((dir == 0 && salad[row][col + i] != EMPTY_CHAR) || (dir == 1 && salad[row + i][col] != EMPTY_CHAR))
{
placeable = 0;
break;
}
}
if (placeable)
{ // wenn es keine Überschneidungen gibt, wird Wort platziert
for (int i = 0; i < wordLen; i++)
{
if (dir == 0)
{
salad[row][col + i] = word[i];
}
else
{
salad[row + i][col] = word[i];
}
}
// copy actually placed words to the front of the array
// it's safe to use strcpy here
strcpy(words[placedWords], word);
placed = 1;
placedWords++;
}
}
}
// Felder, in denen keine Buchstaben stehen, werden mit zufälligen Buchstaben befüllt
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
if (salad[row][col] == EMPTY_CHAR)
{
salad[row][col] = 'A' + (rand() % 26);
}
}
}
// return "Anzahl platzierter Wörter", wird in Main dann mit "soll" verglichen
return placedWords;
}
// Prints the word salad to console
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{
int row, col;
// Ausgabe des gesamten Feldes
for (row = 0; row < searchFieldLen; row++)
{
for (col = 0; col < searchFieldLen; col++)
{
printf("%c ", salad[row][col]);
}
printf("\n");
}
}

View File

@ -5,7 +5,7 @@
#define MAX_SEARCH_FIELD_LEN 100
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, char words[][MAX_WORD_LEN], unsigned int wordCount);
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen);
#endif

View File

@ -1,4 +1,5 @@
#include "input.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
@ -6,7 +7,39 @@
// 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)
{
char buffer[MAX_WORD_LEN];
unsigned int count = 0;
if (file == NULL)
{
return 1;
}
while (fgets(buffer, sizeof(buffer), file) != NULL)
{
// Zerlege Zeile in einzelne Wörter anhand der Trennzeichen
char *token = strtok(buffer, " ,;\n");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]);
}
// Token in das words-Array kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren
count++;
// Nächsten Token holen
token = strtok(NULL, " ,;\n");
}
}
return count;
}

View File

@ -6,13 +6,14 @@
#define MAX_NUMBER_OF_WORDS 100
#define SALAD_SIZE 20
#define WINDOW_WIDTH 800
int main(int argc, char *argv[])
{
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]);
exitCode = EXIT_FAILURE;
@ -24,7 +25,7 @@ int main(int argc, char *argv[])
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
@ -36,11 +37,24 @@ int main(int argc, char *argv[])
// Create the word salad by placing words into grid
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
// TODO:
// Check if all words were successfully placed
// Start the game if successful
// error message if some words couldn't be placed
if (placedWords < wordCount)
{
int notPlacedNum = wordCount - placedWords;
fprintf(stderr, "%d word(s) couldn't be placed\n", notPlacedNum);
}
// Start game if at least 1 word was successfully placed
if (placedWords > 0)
{
startGame(wordSalad, SALAD_SIZE, words, placedWords, WINDOW_WIDTH);
}
else
{
fprintf(stderr, "no word could be placed");
exitCode = EXIT_FAILURE;
}
}
else
{