diff --git a/src/c/labyrinth.c b/src/c/labyrinth.c new file mode 100644 index 0000000..10998d1 --- /dev/null +++ b/src/c/labyrinth.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include "labyrinth.h" +#include "userinput.h" + + + +int printlabyrinth(lab laby, int hoehe, int breite){ + + for(int i = 0; i < hoehe; i++){ + for(int j = 0; j < breite; j++){ + printf("%c ", laby[i][j]); + } + printf("\n"); + } + printf("\n"); + printf("How many steps do you need to reach your goal?\n"); + printf("\n"); +return 0; +} + + +void wegsuchen(lab laby, bool* done, int y, int x, int ziely, int zielx){ + + laby[y][x] = MARKIERT; + + + if(x == zielx && y == ziely){ + *done = true; + } + else{ + if (!*done && y + 1 <= ziely && laby[y+1][x] == WEG){ + wegsuchen(laby, done, y + 1, x, ziely, zielx); + } + if (!*done && x + 1 <= zielx && laby[y][x+1] == WEG){ + wegsuchen(laby, done, y, x + 1, ziely, zielx); + } + if (!*done && y - 1 >= 0 && laby[y-1][x] == WEG){ // oben + wegsuchen(laby, done, y - 1, x, ziely, zielx); + } + if (!*done && x - 1 >= 0 && laby[y][x-1] == WEG){ // links + wegsuchen(laby, done, y, x - 1, ziely, zielx); + } + if (!*done){ + laby[y][x] = WEG; + } + } +} + +void labyrinthschritte(lab laby, int hoehe, int breite, int schritte, int versuche){ + + int antwort = 0; + + + antwort = userInput(); + + if(antwort == schritte){ + printf("Correct you need %d steps.\n", schritte); + + for(int i = 0; i < hoehe; i++){ + for(int j = 0; j < breite; j++){ + printf("%c ", laby[i][j]); + } + printf("\n"); + } + printf("\n"); + } + else{ + if(versuche != 3){ + printf("Your answer is wrong. Try again.\n"); + versuche = versuche + 1; + labyrinthschritte(laby, hoehe, breite, schritte, versuche); //if schleife für 3 versuche + } + else{ + printf("You lost.\n"); + } + } +} + +void labyrinthauswahl(int auswahl){ + + printf("Bitte wählen Sie ein Labyrinth aus\n"); + + switch (auswahl){ + case 1: + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + int hoehe = 5; + int breite = 6; + printlabyrinth(laby, hoehe, breite); + break; + + default: + break; + } + +} \ No newline at end of file diff --git a/src/c/labyrinth.h b/src/c/labyrinth.h new file mode 100644 index 0000000..0147da5 --- /dev/null +++ b/src/c/labyrinth.h @@ -0,0 +1,16 @@ +#ifndef LABYRINTH_H +#define LABYRINTH_H + +#include + +#define MAXZEILEN 10 +#define MAXSPALTEN 10 +#define WEG '0' +#define MARKIERT 'X' + +typedef char lab[MAXZEILEN][MAXSPALTEN]; +int printlabyrinth(lab laby, int hoehe, int breite); +void wegsuchen(lab laby, bool* done, int y, int x, int ziely, int zielx); +void labyrinthschritte(lab laby, int hoehe, int breite, int schritte, int versuche); +void labyrinthauswahl(int auswahl); +#endif \ No newline at end of file diff --git a/test/c/test_labyrinth.c b/test/c/test_labyrinth.c new file mode 100644 index 0000000..cf05752 --- /dev/null +++ b/test/c/test_labyrinth.c @@ -0,0 +1,313 @@ +#ifdef TEST +#include "unity.h" +//in example.h wird die Funktion deklariert +#include "labyrinth.h" +#include +#include "userinput.h" +#include "mock_userinput.h" + +//Vor- bzw. Nachbereitung +void setUp(void) +{ +} + +void tearDown(void) +{ +} + + +void test_runExampleTest(void) +{ + int result; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'1', '2', '3'}, + {'4', '5', '6'}, + {'7', '8', '9'}, + }; + + + result = printlabyrinth(laby, hoehe, breite); + + + TEST_ASSERT_EQUAL_INT(0, result); +} + + +void test_LabyrinthAmZielAngekommen(void){ + + bool result; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'1', '2', '3'}, + {'4', '5', '6'}, + {'7', '8', '9'}, + }; + + wegsuchen(laby, &result, 0, 0, 0, 0); + TEST_ASSERT_EQUAL_INT(1, result); + + +} + +void test_LabyrinthAmZielNichtAngekommen(void){ + + bool result = 0; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'0', '1', '1'}, + {'1', '0', '1'}, + {'1', '1', '1'}, + }; + + wegsuchen(laby, &result, 1, 1, 0, 0); + TEST_ASSERT_EQUAL_INT(0, result); + + +} + +void test_LabyrinthMarkiert(void){ + + char result; + bool fertig; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'0', '1', '1'}, + {'1', '1', '1'}, + {'1', '1', '1'}, + }; + + + wegsuchen(laby, &fertig, 0, 0, 0, 0); + result = laby[0][0]; + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_CHAR('X', result); + + +} + +void test_LabyrinthUnten(void){ + + bool result; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'0', '1', '1'}, + {'0', '1', '1'}, + {'0', '1', '1'}, + }; + + wegsuchen(laby, &result, 0, 0, 2, 0); + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_INT(1, result); + + +} + +void test_LabyrinthRechts(void){ + + bool result; + int input = 1; + int hoehe = 3; + int breite = 3; + lab laby = { + {'0', '0', '0'}, + {'1', '1', '1'}, + {'1', '1', '1'}, + }; + + wegsuchen(laby, &result, 0, 0, 0, 2); + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_INT(1, result); +} + + +void test_LabyrinthOben(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 5; + lab laby = { + {'0', '1', '0', '0', '0'}, + {'0', '1', '0', '1', '0'}, + {'0', '1', '0', '1', '0'}, + {'0', '1', '0', '1', '0'}, + {'0', '0', '0', '1', '0'}, + }; + + wegsuchen(laby, &result, 0, 0, 4, 4); + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_INT(1, result); +} + + +void test_LabyrinthLinks(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + }; + + wegsuchen(laby, &result, 0, 0, 4, 5); + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_INT(1, result); +} + +void test_LabyrinthVerzweigung(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + + + wegsuchen(laby, &result, 0, 0, 4, 5); + printlabyrinth(laby, hoehe, breite); + TEST_ASSERT_EQUAL_INT(1, result); +} + + +void test_LabyrinthSchritte(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + int schritte = 14; + int versuche = 0; + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + + printlabyrinth(laby, hoehe, breite); //hier in die funktion die print frage machen + wegsuchen(laby, &result, 0, 0, 4, 5); + + userInput_ExpectAndReturn(14); + + labyrinthschritte(laby, hoehe, breite, schritte, versuche); //die geliche funktion nur mit dem if vergleich und userinput + TEST_ASSERT_EQUAL_INT(1, result); + +} + +void test_LabyrinthSchritteVerloren(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + int schritte = 14; + int versuche = 0; + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + + printlabyrinth(laby, hoehe, breite); //hier in die funktion die print frage machen + wegsuchen(laby, &result, 0, 0, 4, 5); + + userInput_ExpectAndReturn(5); + userInput_ExpectAndReturn(8); + userInput_ExpectAndReturn(10); + userInput_ExpectAndReturn(9); + + labyrinthschritte(laby, hoehe, breite, schritte, versuche); //die geliche funktion nur mit dem if vergleich und userinput + TEST_ASSERT_EQUAL_INT(1, result); + +} + +void test_LabyrinthSchritteBeiDreiGewonnen(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + int schritte = 14; + int versuche = 0; + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + + printlabyrinth(laby, hoehe, breite); //hier in die funktion die print frage machen + wegsuchen(laby, &result, 0, 0, 4, 5); + + userInput_ExpectAndReturn(5); + userInput_ExpectAndReturn(8); + userInput_ExpectAndReturn(10); + userInput_ExpectAndReturn(14); + + labyrinthschritte(laby, hoehe, breite, schritte, versuche); //die geliche funktion nur mit dem if vergleich und userinput + TEST_ASSERT_EQUAL_INT(1, result); + +} + +void test_LabyrinthAuswahl(void){ + + bool result; + int input = 1; + int hoehe = 5; + int breite = 6; + int schritte = 14; + int versuche = 0; + int auswahl = 1; + + + + lab laby = { + {'0', '1', '0', '0', '0', '0'}, + {'0', '1', '0', '1', '1', '0'}, + {'0', '0', '0', '0', '1', '0'}, + {'0', '1', '1', '0', '1', '0'}, + {'0', '1', '0', '0', '1', '0'}, + }; + + labyrinthauswahl(auswahl); + //printlabyrinth(laby, hoehe, breite); //hier in die funktion die print frage machen + wegsuchen(laby, &result, 0, 0, 4, 5); + + userInput_ExpectAndReturn(5); + userInput_ExpectAndReturn(8); + userInput_ExpectAndReturn(10); + userInput_ExpectAndReturn(14); + + labyrinthschritte(laby, hoehe, breite, schritte, versuche); //die geliche funktion nur mit dem if vergleich und userinput + TEST_ASSERT_EQUAL_INT(1, result); + +} + +#endif // TEST \ No newline at end of file