Compare commits

..

No commits in common. "39addfa981d8c2edbee3394d3f7c3d03bcb6a089" and "ba8df4c6c8c2bdd3ef3002997e05fd2159655d7e" have entirely different histories.

13 changed files with 306 additions and 151 deletions

View File

@ -1,93 +1,223 @@
#include "game.h" #include "game.h"
#include <stdio.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#define EMPTY_CHAR '.' // Leeres Kästchen #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR '.'
typedef enum { HORIZONTAL, VERTICAL } Direction;
// 1. Funktion Array initialisieren // TODO: Spiellogik implementieren:
static void initializeArray(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size) /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */
static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen
{ {
for (unsigned int y = 0; y < size; y++)
for (unsigned int x = 0; x < size; x++)
salad[y][x] = EMPTY_CHAR;
}
// 2. Funktion prüfen, ob Wort passt for (int i = 0; i < searchFieldLen; i++)
static int canPlace(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], {
unsigned int size, unsigned int x, unsigned int y,
const char *word, Direction dir)
{
unsigned int len = strlen(word);
if ((dir == HORIZONTAL && x + len > size) || (dir == VERTICAL && y + len > size))
return 0;
for (unsigned int i = 0; i < len; i++) { for (int j = 0; j < searchFieldLen; j++)
if ((dir == HORIZONTAL && salad[y][x + i] != EMPTY_CHAR) || {
(dir == VERTICAL && salad[y + i][x] != EMPTY_CHAR)) salad[i][j] = EMPTY_CHAR;
return 0; }
} }
return 1;
} }
// 3. Funktion Wort eintragen
static void placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort
unsigned int x, unsigned int y, const char *word, Direction dir)
{ {
for (unsigned int i = 0; i < strlen(word); i++) { *x = rand() % searchFieldLen;
if (dir == HORIZONTAL)
salad[y][x + i] = word[i]; *y = rand() % searchFieldLen;
}
static int place(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], int searchFieldLen, int len [],const char words [] [MAX_WORD_LEN], unsigned int wordCount) //Prüfen ob plazierbar und plazieren
{
int x, y;
int placedWords = 0; //Counter plazierte Wörter
for(int i = 0; i < wordCount; i++)
{
int placed = 0;
for(int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; tries++)
{
len[i] = (int)strlen(words[i]);
col_row(searchFieldLen, &x, &y);
int orient = rand() % 2;
if(orient == 0) //wir prüfen und füllen horizontal (in x-Richtung)
{
int works = 1;
if(((x + len[i]) > searchFieldLen))
{
works = 0;
}
else else
salad[y + i][x] = word[i]; {
for(int j = x; j < (x + len[i]); j++)
{
if(salad[y][j] != EMPTY_CHAR)
{
works = 0;
break;
}
}
}
// ab hier platzieren wir
if(works)
{
for (int k = 0; k < len[i]; k++)
{
salad[y][x + k] = words[i][k];
}
placed = 1;
placedWords++;
}
}
else //wir prüfen und füllen vertikal (in y-Richtung)
{
int works = 1;
if(((y + len[i]) > searchFieldLen))
{
works = 0;
}
else
{
for(int j = y; j < (y + len[i]); j++)
{
if(salad[j][x] != EMPTY_CHAR)
{
works = 0;
break;
}
}
}
// ab hier platzieren wir
if(works)
{
for (int k = 0; k < len[i]; k++)
{
salad[y + k][x] = words[i][k];
}
placed = 1;
placedWords++;
}
}
}
}
return placedWords;
}
static void fillLetters(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Übrige freie Felder mit Buchstaben füllen
{
for (int i = 0; i < searchFieldLen; i++)
{
for (int j = 0; j < searchFieldLen; j++)
{
if (salad[i][j] == EMPTY_CHAR)
{
char random_letter = 'A' + (rand() % 26);
salad[i][j] = random_letter;
}
}
} }
} }
// 4. Funktion leere Felder mit Zufallsbuchstaben füllen //Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken
static void fillRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size)
{
for (unsigned int y = 0; y < size; y++)
for (unsigned int x = 0; x < size; x++)
if (salad[y][x] == EMPTY_CHAR)
salad[y][x] = 'A' + (rand() % 26);
}
// Hauptfunktion Wortsalat erzeugen 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 size, const char words[][MAX_WORD_LEN],
unsigned int wordCount)
{ {
srand(time(NULL)); srand(time(NULL));
initializeArray(salad, size);
int placed = 0; initializeArray(salad, searchFieldLen);
for (unsigned int i = 0; i < wordCount; i++) {
int success = 0; int len [wordCount];
for (int tries = 0; tries < 1000 && !success; tries++) { int count = 0;
unsigned int x = rand() % size;
unsigned int y = rand() % size; for(int i = 0; i < wordCount; i++)
Direction dir = rand() % 2 ? HORIZONTAL : VERTICAL; {
if(words [i] [0] != '\0')
count++;
if (canPlace(salad, size, x, y, words[i], dir)) {
placeWord(salad, x, y, words[i], dir);
success = 1;
placed++;
}
}
} }
fillRandomLetters(salad, size); int placeWords = place(salad, searchFieldLen, len, words, count);
return placed;
fillLetters(salad, searchFieldLen);
return placeWords;
} }
// Ausgabe-Funktion // Ausgabe von Wortsalat auf Konsole
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int size)
void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
{ {
for (unsigned int y = 0; y < size; y++) {
for (unsigned int x = 0; x < size; x++) for (int i = 0; i < searchFieldLen; i++)
printf("%c ", salad[y][x]); {
for (int j = 0; j < searchFieldLen; j++)
{
printf("%c ", salad [i][j]);
}
printf("\n"); printf("\n");
} }
} }

Binary file not shown.

Binary file not shown.

View File

@ -1,38 +1,39 @@
#include <stdio.h> #include "input.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include "input.h" #include <stdio.h>
// 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
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; // wie viele Wörter bisher gespeichert wurden unsigned int count = 0;
char line[MAX_LINE_LEN]; // Puffer für EINE gelesene Textzeile char line[MAX_LINE_LEN];
while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount) //fgets liest eine komplette Zeile wenn es noch eine Zeile gibt und noch Platz für mehr Wörter ist while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount)
{ {
const char *delims = "\t\n\r ,;"; //Liste der Trenner (z.B. Komma) const char *delims = "\t\n\r ,;";
char *token = strtok(line, delims); //strok sucht in line das erste Wort, indem es an Trennzeichen teilt. Wenn kein Wort gefunden, wird NULL zurück gegeben char *token = strtok(line, delims);
while (token != NULL && count < maxWordCount) //Solange es noch ein Wort (token) gibt und noch Platz in words ist while (token != NULL && count < maxWordCount)
{ {
for(unsigned int i = 0; token[i] != '\0'; i++) for(unsigned int i = 0; token[i] != '\0'; i++)
{ {
token[i] = toupper((unsigned char)token[i]); //toupper macht jeden Buchstaben groß. token[i] = toupper((unsigned char)token[i]);
} }
strncpy(words[count], token, MAX_WORD_LEN -1); //Kopiert das Wort in den nächsten freien Slot words[count]. strncpy(words[count], token, MAX_WORD_LEN -1);
words[count][MAX_WORD_LEN -1] = '\0'; words[count][MAX_WORD_LEN -1] = '\0';
count++; //Wir habenein Wort abgespeichert count + 1 count++;
token = strtok(NULL, delims); //hier token = strtok(NULL, delims); //hier
} }
} }
return count; return count;
} }

Binary file not shown.

View File

@ -10,44 +10,84 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
if (argc != 2) //wenn keine zwei argumente gefunde -> alarm
// Check if the correct number of arguments is provided
if(argc != 2)
{ {
fprintf(stderr, "Benutzung: %s <Pfad zur Wörterdatei>\n", argv[0]);
return EXIT_FAILURE; fprintf(stderr, "Usage: %s <path to file with search words>\n", argv[0]);
exitCode = EXIT_FAILURE;
} }
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; //array mit eingelesenen wörtern 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; unsigned int wordCount = 0;
FILE *file = fopen(argv[1], "r"); //schaut in die words.txt datei FILE *file = fopen(argv[1], "r");
if (!file) //wenn keine datei -> alarm
if(file != NULL)
{ {
fprintf(stderr, "Konnte Datei %s nicht öffnen.\n", argv[1]);
return EXIT_FAILURE;
}
unsigned int placedWords = 0; unsigned int placedWords = 0;
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; char wordSalad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); //holt sich aus readwords funktion die wortanzahl
// Read words from file and store in 'words' array
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
fclose(file); fclose(file);
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); //holt sich anzahl der platzierten wörter von createwordsalad funktion
printf("\nWortsalat-Initialisierung\n"); //ganz viel printen // Create the word salad by placing words into grid
printf("-------------------------\n");
printf("Gelesene Woerter: %u\n", wordCount);
printf("Platzierte Woerter: %u\n", placedWords);
if (placedWords < wordCount) //macht checker, ob alle wörter salatiert wurden placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, MAX_NUMBER_OF_WORDS);
printf("Hinweis: %u Woerter konnten nicht platziert werden.\n", wordCount - placedWords);
// TODO:
// Check if all words were successfully placed
if(placedWords == wordCount)
{
printf("Alle %u Wörter wurden erfolgreich platziert.", placedWords);
// Start the game if successful
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
}
// error message if some words couldn't be placed
else
{
printf("Es konnten nicht alle Wörter ausgegeben werden. Nur %u von %u Wörtern wurde platziert.\n", placedWords, wordCount);
}
}
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;
}
}
printf("\nSpielfeld (%dx%d):\n\n", SALAD_SIZE, SALAD_SIZE);
showWordSalad(wordSalad, SALAD_SIZE);
printf("\nInitialisierung abgeschlossen.\n");
return exitCode; return exitCode;
} }

Binary file not shown.

BIN
Start_Mac/runTests Executable file

Binary file not shown.

View File

@ -1,7 +1,16 @@
Yeti,Nessie Werwolf; Vampir YETI
Monster NESSIE
Hydra;Frankenstein; fortnite WERWOLF
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist VAMPIR
Gespenst, Oger MONSTER
tomasschwester, lababage, eckesbokesseresekes HYDRA
Doener, Kebab, nacho, alejandrogarnacho, megafon, Telefon, gianluigibuffon, mehrweorter, FRANKENSTEIN
DRACULA
KINGKONG
GREMLIN
KOBOLD
HEXE
POLTERGEIST
GESPENST
OGER
APFEL

Binary file not shown.

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.wordsalad</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View File

@ -1,5 +0,0 @@
---
triple: 'arm64-apple-darwin'
binary-path: wordsalad
relocations: []
...