Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
1f22de8f99
212
Start_Mac/game.c
212
Start_Mac/game.c
@ -4,20 +4,220 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_RAND_TRIES_PER_WORD 10
|
#define MAX_RAND_TRIES_PER_WORD 10
|
||||||
#define EMPTY_CHAR 0
|
#define EMPTY_CHAR '.'
|
||||||
|
|
||||||
//TODO: Spiellogik implementieren:
|
|
||||||
|
// TODO: Spiellogik implementieren:
|
||||||
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
|
||||||
* 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
|
|
||||||
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
|
|
||||||
|
static void initializeArray(char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) //Array initialisieren & mit Filler füllen
|
||||||
{
|
{
|
||||||
|
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
salad[i][j] = EMPTY_CHAR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void col_row (unsigned int searchFieldLen, int *x, int *y) //Zufällige x/y Koordinate für Anfang von Wort
|
||||||
|
{
|
||||||
|
*x = rand() % searchFieldLen;
|
||||||
|
|
||||||
|
*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
|
||||||
|
{
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the word salad to console
|
|
||||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Erstellt Wortsalat, plaziert Wörter zufällig & füllt die Lücken
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
initializeArray(salad, searchFieldLen);
|
||||||
|
|
||||||
|
int len [wordCount];
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < wordCount; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(words [i] [0] != '\0')
|
||||||
|
count++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int placeWords = place(salad, searchFieldLen, len, words, count);
|
||||||
|
|
||||||
|
|
||||||
|
fillLetters(salad, searchFieldLen);
|
||||||
|
|
||||||
|
return placeWords;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ausgabe von Wortsalat auf Konsole
|
||||||
|
|
||||||
|
void showWordSalad(const char salad [MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; i < searchFieldLen; i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int j = 0; j < searchFieldLen; j++)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("%c ", salad [i][j]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Start_Mac/game.o
Normal file
BIN
Start_Mac/game.o
Normal file
Binary file not shown.
@ -1,6 +1,7 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.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.
|
||||||
@ -9,4 +10,30 @@
|
|||||||
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 line[MAX_LINE_LEN];
|
||||||
|
|
||||||
|
while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
const char *delims = "\t\n\r ,;";
|
||||||
|
char *token = strtok(line, delims);
|
||||||
|
|
||||||
|
|
||||||
|
while (token != NULL && count < maxWordCount)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; token[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
token[i] = toupper((unsigned char)token[i]);
|
||||||
|
}
|
||||||
|
strncpy(words[count], token, MAX_WORD_LEN -1);
|
||||||
|
words[count][MAX_WORD_LEN -1] = '\0';
|
||||||
|
count++;
|
||||||
|
token = strtok(NULL, delims); //hier
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
Start_Mac/input.o
Normal file
BIN
Start_Mac/input.o
Normal file
Binary file not shown.
@ -7,18 +7,27 @@
|
|||||||
#define MAX_NUMBER_OF_WORDS 100
|
#define MAX_NUMBER_OF_WORDS 100
|
||||||
#define SALAD_SIZE 20
|
#define SALAD_SIZE 20
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
int exitCode = EXIT_SUCCESS;
|
int exitCode = EXIT_SUCCESS;
|
||||||
|
|
||||||
|
|
||||||
// Check if the correct number of arguments is provided
|
// 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]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN]; // Array to hold the words to be used in the game
|
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;
|
||||||
|
|
||||||
@ -26,29 +35,59 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if(file != NULL)
|
if(file != NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
unsigned int placedWords = 0;
|
unsigned int placedWords = 0;
|
||||||
char wordSalad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; // 2D array to store the word salad
|
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
|
// 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
|
// Create the word salad by placing words into grid
|
||||||
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount);
|
|
||||||
|
placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, MAX_NUMBER_OF_WORDS);
|
||||||
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// Check if all words were successfully placed
|
// Check if all words were successfully placed
|
||||||
|
|
||||||
|
if(placedWords == wordCount)
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("Alle %u Wörter wurden erfolgreich platziert.", placedWords);
|
||||||
|
|
||||||
// Start the game if successful
|
// Start the game if successful
|
||||||
// error message if some words couldn't be placed
|
|
||||||
|
startGame(wordSalad, SALAD_SIZE, words, wordCount, 800);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// error message if some words couldn't be placed
|
||||||
|
|
||||||
|
|
||||||
else
|
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
|
// Print error message if file couldn't be opened
|
||||||
|
|
||||||
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]);
|
||||||
exitCode = EXIT_FAILURE;
|
exitCode = EXIT_FAILURE;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
return exitCode;
|
||||||
|
|
||||||
}
|
}
|
||||||
BIN
Start_Mac/runTests
Executable file
BIN
Start_Mac/runTests
Executable file
Binary file not shown.
@ -1,5 +1,16 @@
|
|||||||
Yeti,Nessie Werwolf; Vampir
|
YETI
|
||||||
Monster
|
NESSIE
|
||||||
Hydra;Frankenstein
|
WERWOLF
|
||||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
VAMPIR
|
||||||
Gespenst, Oger
|
MONSTER
|
||||||
|
HYDRA
|
||||||
|
FRANKENSTEIN
|
||||||
|
DRACULA
|
||||||
|
KINGKONG
|
||||||
|
GREMLIN
|
||||||
|
KOBOLD
|
||||||
|
HEXE
|
||||||
|
POLTERGEIST
|
||||||
|
GESPENST
|
||||||
|
OGER
|
||||||
|
APFEL
|
||||||
Loading…
x
Reference in New Issue
Block a user