forked from freudenreichan/info2Praktikum-Wortsalat
Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
17a74ddc81 | ||
|
|
ebb886ae9e | ||
|
|
1ca4567185 | ||
|
|
bda3c2b104 | ||
|
|
65b20f7ea4 | ||
|
|
8592476b50 | ||
|
|
d269bc321b | ||
|
|
7f69b71455 | ||
|
|
de24c33688 | ||
|
|
32a6133d18 | ||
|
|
39e8380916 | ||
|
|
ffaed665b9 | ||
|
|
e20bf82c19 | ||
|
|
1ed03a3444 | ||
|
|
6b435cf82d | ||
|
|
92c0ab2c28 |
No files matched your search
@@ -0,0 +1,5 @@
|
||||
makefile/
|
||||
.vscode/
|
||||
*.exe
|
||||
Start_Linux/makefile
|
||||
Start_Windows/makefile
|
||||
@@ -0,0 +1,5 @@
|
||||
[Buildset]
|
||||
BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x000\x00i\x00n\x00f\x00o\x002\x00P\x00r\x00a\x00k\x00t\x00i\x00k\x00u\x00m\x00-\x00W\x00o\x00r\x00t\x00s\x00a\x00l\x00a\x00t)
|
||||
|
||||
[Project]
|
||||
VersionControlSupport=kdevgit
|
||||
+60
-2
@@ -6,18 +6,76 @@
|
||||
#define MAX_RAND_TRIES_PER_WORD 10
|
||||
#define EMPTY_CHAR 0
|
||||
|
||||
void emptyArray();
|
||||
int placeWord();
|
||||
void fillEmptySpots();
|
||||
|
||||
//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)
|
||||
{
|
||||
|
||||
srand(time(NULL));
|
||||
emptyArray(salad,MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
||||
placeWord(salad,words);
|
||||
fillEmptySpots(salad, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN);
|
||||
}
|
||||
|
||||
// Prints the word salad to console
|
||||
void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen)
|
||||
{
|
||||
for(int i = 0; i <= MAX_SEARCH_FIELD_LEN; i++ )
|
||||
{
|
||||
for(int j = 0; j <= MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
printf("[%c]",salad[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void emptyArray(char array[][], int arrayLength)
|
||||
{
|
||||
char* element = (char*) &array;
|
||||
for(int i = 0; i < arrayLength; i++ )
|
||||
{
|
||||
*element = EMPTY_CHAR;
|
||||
element++;
|
||||
}
|
||||
}
|
||||
|
||||
int placeWord(char intoArray[][], char insertedWord[][])
|
||||
{
|
||||
int numberWord;
|
||||
int tries;
|
||||
while(tries <= MAX_RAND_TRIES_PER_WORD)
|
||||
{
|
||||
int xCord = rand() % MAX_SEARCH_FIELD_LEN;
|
||||
int yCord = rand() % MAX_SEARCH_FIELD_LEN;
|
||||
int isHorizontal = rand() % 2;
|
||||
unsigned int wordLength = strlen(insertedWord[numberWord][0]);
|
||||
if(isHorizontal)
|
||||
{
|
||||
if(xCord + sizeof(insertedWord[numberWord][0]) <= MAX_SEARCH_FIELD_LEN)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void fillEmptySpots(char array[][], int arrayLength)
|
||||
{
|
||||
char* element = (char*) &array;
|
||||
for(int i = 0; i < arrayLength; i++ )
|
||||
{
|
||||
if(*element == EMPTY_CHAR)
|
||||
{
|
||||
*element == rand() % ('Z' - 'A' + 1) + 'A';
|
||||
}
|
||||
element++;
|
||||
}
|
||||
|
||||
}
|
||||
+29
-5
@@ -1,12 +1,36 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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)
|
||||
{
|
||||
char puffer[MAX_WORD_LEN];
|
||||
file = fopen("words.txt", "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
char teiler[] = " ;,.\n";
|
||||
char aktuellesWort;
|
||||
int wortAnzahl = 0;
|
||||
|
||||
while (fgets(puffer, MAX_WORD_LEN, file) != NULL)
|
||||
{
|
||||
|
||||
aktuellesWort = strtok(puffer, teiler);
|
||||
|
||||
while (aktuellesWort != NULL && wortAnzahl < maxWordCount)
|
||||
{
|
||||
strncpy(words[wortAnzahl], aktuellesWort,sizeof(words[wortAnzahl]) - 1);
|
||||
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] = '\0';
|
||||
wortAnzahl++; // Nächstes Wort
|
||||
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
+12
-2
@@ -38,8 +38,18 @@ int main(int argc, char *argv[])
|
||||
|
||||
// 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)
|
||||
{
|
||||
// Start the game if successful
|
||||
startGame(wordSalad,MAX_NUMBER_OF_WORDS,placedWords, wordCount,1024);
|
||||
}
|
||||
else
|
||||
{
|
||||
// error message if some words couldn't be placed
|
||||
fprintf(stderr, "Could not place every word \n");
|
||||
exitCode = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lGL -lX11 -lm
|
||||
BINARIES = ./linux
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -28,7 +28,7 @@ game.o: game.c
|
||||
$(CC) $(CFLAGS) -c game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) $(CFLAGS) -c graphicalGame.c
|
||||
$(CC) $(CFLAGS) -I$(raylibfolder) -c graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
|
||||
@@ -101,7 +101,37 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
// falls Vor- und Nachbereitungen für die Tests benötigt.
|
||||
|
||||
@@ -134,6 +164,7 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Executable
BIN
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+2
-2
@@ -4,7 +4,7 @@ LDFLAGS = -framework OpenGL -framework CoreFoundation -framework CoreGraphics -f
|
||||
ARCH := $(shell uname -m)
|
||||
BINARIES = ./macos-$(ARCH)
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -43,4 +43,4 @@ test: input.o game.o unit_tests.c $(BINARIES)/libunity.a
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
rm -f *.o wordsalad
|
||||
rm -f *.o wordsalad $(TEST_BIN)
|
||||
@@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
@@ -135,6 +164,7 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
+46
-4
@@ -1,12 +1,54 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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) {
|
||||
|
||||
if (file == NULL) {
|
||||
|
||||
perror("File couldn't be opened");
|
||||
}
|
||||
|
||||
// zunächst fehlerhaften String einlesen und in anderem Array
|
||||
// zwischenspeichern
|
||||
|
||||
char fehlerhafterString[MAX_LINE_LEN];
|
||||
char *teiler = " ;,.\n";
|
||||
char *aktuellesWort;
|
||||
int wortAnzahl = 0;
|
||||
|
||||
while (fgets(fehlerhafterString, sizeof(fehlerhafterString), file) !=
|
||||
NULL && // während mit fgets alle Zeichen aus dem file eingelesen
|
||||
// werden
|
||||
wortAnzahl < maxWordCount) {
|
||||
|
||||
aktuellesWort =
|
||||
strtok(fehlerhafterString, teiler); // Erstes Wort mit strtok aus dem
|
||||
// fehlerhaften String herauslösen
|
||||
|
||||
while (aktuellesWort != NULL &&
|
||||
wortAnzahl < maxWordCount) { // while strtok nicht am Ende ist und
|
||||
// noch Wörter in words passen
|
||||
|
||||
strncpy(words[wortAnzahl], aktuellesWort,
|
||||
sizeof(words[wortAnzahl]) -
|
||||
1); // mit strcpy das aktuelle Wort in words kopieren
|
||||
words[wortAnzahl][sizeof(words[wortAnzahl]) - 1] =
|
||||
'\0'; // Nullterminator mit sizeof des aktuellen Worts - 1 an Ende des
|
||||
// Worts setzen
|
||||
|
||||
wortAnzahl++; // Nächstes Wort
|
||||
aktuellesWort = strtok(NULL, teiler); // Nächstes Token
|
||||
}
|
||||
}
|
||||
|
||||
return wortAnzahl; // Anzahl der eingelesenen Wörter
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I$(raylibfolder)
|
||||
CFLAGS = -g -Wall
|
||||
LDFLAGS = -lopengl32 -lgdi32 -lwinmm
|
||||
BINARIES = ./windows
|
||||
|
||||
raylib_folder = ./raylib
|
||||
raylibfolder = ./raylib
|
||||
unityfolder = ./unity
|
||||
|
||||
# --------------------------
|
||||
@@ -12,7 +12,6 @@ unityfolder = ./unity
|
||||
wordsalad_initial:
|
||||
$(CC) -o wordsalad_initial $(BINARIES)/libwordsalad_complete.a $(BINARIES)/libraylib.a $(LDFLAGS)
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Normales Spiel bauen
|
||||
# --------------------------
|
||||
@@ -23,13 +22,13 @@ main.o: main.c
|
||||
$(CC) -c $(CFLAGS) main.c
|
||||
|
||||
input.o: input.c
|
||||
$(CC) -c $(CFLAGS)input.c
|
||||
$(CC) -c $(CFLAGS) input.c
|
||||
|
||||
game.o: game.c
|
||||
$(CC) -c $(CFLAGS) game.c
|
||||
|
||||
graphicalGame.o: graphicalGame.c
|
||||
$(CC) -I$(raylib_folder) -c $(CFLAGS) graphicalGame.c
|
||||
$(CC) -I$(raylibfolder) -c $(CFLAGS) graphicalGame.c
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
|
||||
@@ -101,6 +101,35 @@ void test_createWordSalad_too_small(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void test_createWordSalad_allWordsPlaced() {
|
||||
char words[3][MAX_WORD_LEN] = {"CAT", "DOG", "MOUSE"};
|
||||
char saladHoriz[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
char saladVert[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN];
|
||||
|
||||
int placed = createWordSalad(saladHoriz, 20, words, 3);
|
||||
for(int i = 0; i < MAX_SEARCH_FIELD_LEN; i++)
|
||||
{
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
saladVert[j][i] = saladHoriz[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < 3; i++) {
|
||||
const char* word = words[i];
|
||||
int wordFound = 0;
|
||||
for(int j = 0; j < MAX_SEARCH_FIELD_LEN; j++)
|
||||
{
|
||||
const char* row = saladHoriz[j];
|
||||
const char* col = saladVert[j];
|
||||
wordFound |= strstr(row, word) || strstr(col, word);
|
||||
}
|
||||
TEST_ASSERT_TRUE_MESSAGE(wordFound, "Not all words were placed.");
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(3, placed);
|
||||
}
|
||||
|
||||
// ---------- Test Setup und TearDown Funktionen ----------
|
||||
|
||||
// Hier Setup- und TearDown-Funktionen definieren,
|
||||
@@ -135,6 +164,7 @@ int main(void) {
|
||||
RUN_TEST(test_readWords_empty_file);
|
||||
RUN_TEST(test_createWordSalad_all_fit);
|
||||
RUN_TEST(test_createWordSalad_too_small);
|
||||
RUN_TEST(test_createWordSalad_allWordsPlaced);
|
||||
|
||||
int result = UNITY_END(); // Test-Ergebnisse
|
||||
print_test_result(result);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
[Project]
|
||||
CreatedFrom=
|
||||
Manager=KDevGenericManager
|
||||
Name=info2Praktikum-Wortsalat
|
||||
Reference in new issue
Block a user