diff --git a/src/main/c/Georg/tictactoe.c b/src/main/c/Georg/tictactoe.c new file mode 100644 index 0000000..241ee34 --- /dev/null +++ b/src/main/c/Georg/tictactoe.c @@ -0,0 +1,250 @@ +#include +#include +#include + +#include "tictactoe.h" + +void printBoard(); + +struct ticTacToe GAME; + +struct usrCommand COMMANDS[MAX_COMMANDS] = { + { 1, "\"start menu\" - startet das menu", startMenu}, + { 2, "\"start game\" - startet das spiel", startGame} +}; + +void startTicTacToe(){ + setbuf(stdout, 0); // for debug output + printf( "%s\n", getWelcomeMessageTicTacToe() ); + printf( "%s\n\n", getRulesMessageTicTacToe() ); + + GAME = createTicTacToe(); // create the "game object" + + while( GAME.currentState != -1 ){ + commandFunction usrCommand; + //printf("search command!\n"); + usrCommand = getCommandById( GAME.currentState + 1); + + if( usrCommand != NULL) + usrCommand(0); // 0, for non test behavior + else{ + printf("command not found"); + return; + } + } +} + +char* getWelcomeMessageTicTacToe(){ + return "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; +} + +char* getRulesMessageTicTacToe(){ + return "Das spiel wird über die Komandozeile gespielt.\n" + "Jeder Spielzug ist eine Eingabe in die Konsole. Die enstsprechenden Befehle stehen jeweils unterhalb des Spielfelds.\n" + "Um ein Zug zu tätigen musst du \"set x,y\" in die Konsole Eingeben. Die Koordinaten stehen dabei für Zeile und Spalte.\n" + "Mit dem Befehl \"start\" startest du das Spiel" + "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; +} + +struct ticTacToe createTicTacToe() { + struct ticTacToe newGame; + newGame.currentState = 0; + return newGame; +} + +int handleCommand( char* input ){ + if( strcmp(input, "start menu") == 0 ){ + return 0; + }else if( strcmp(input, "start game") == 0 ){ + return 1; + }else{ + return -1; + } +} + +commandFunction getCommandById( int id ){ + commandFunction result = NULL; + size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); // calculate size of Array + for (size_t i = 0; i < arraySize; i++) { + //printf( "%s", COMMANDS[i].description ); + if( COMMANDS[i].id == id ){ + result = COMMANDS[i].fun; // save the function pointer as result + break; + } + } + return result; +} + +char* getUserInput(){ + static char userInput[MAX_INPUT_LENGTH]; + printf( ":" ); + fgets(userInput, sizeof(userInput), stdin); + + size_t lengthOfInput = strlen(userInput); + if (lengthOfInput > 0 && userInput[lengthOfInput - 1] == '\n') { + userInput[lengthOfInput - 1] = '\0'; // declare end of command + } + return userInput; +} + +void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ + for (int line = 0; line < 3; ++line) { + for (int column = 0; column < 3; ++column) { + board[line][column] = 0; + } + } +} + +/** + * The function checks if the user has written a valid command + * @param input + * @return 1 for a valid command and 0 for an invalid one. + */ +int handleGameInput( char* input ){ + if( strstr(input, "set") != NULL ){ + return 1; + }else{ + return -1; + } +} + +int startMenu( int code ){ + if( code == -1 ){ // usrCommand test + return 1; + } + + printf("Welcome to the menu!\n"); + + while( GAME.currentState == 0 ){ + int nextState = handleCommand( getUserInput() ); + + if( nextState == -1 ){ + printf("command not found!"); + }else{ + GAME.currentState = nextState; + } + } + + return 0; +} + +void printBoard(){ + // calculate the size of the board and print the upper frame + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); + + for (int line = 0; line < 3; ++line) { + printf("| "); + for (int column = 0; column < 3; ++column) { + if( GAME.board[line][column] == true ) + printf( "X" ); + else + printf ( " " ); + + printf(" | "); + } + printf( "\n" ); + } + + // calculate the size of the board and print the upper frame + for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ + printf("-"); + } + printf("\n"); +} + +/** + * Get a the user input and parse it. + * @param input + * @return a array with the x and y direction where the user wants to set the marker. + */ +int* getMarkerParameters( char* input ){ + int* array = (int*)malloc(2 * sizeof(int)); + + int index = strchr(input, ',') - input; + int firstArgument = input[index-1] - '0'; + int secondArgument = input[index+1] - '0'; + + array[0] = firstArgument-1; // return x + array[1] = secondArgument-1; // return y + + return array; +} + +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ){ + board[params[0]][params[1]] = 1; +} + +void handleGame(){ + char* input = getUserInput(); + int nextState = handleCommand( input ); + + // check commands, if no command found return for new Input + // gameCommands are saved and processed after this block + int gameCommand = -1; + if( nextState == -1 ){ // no stateCommand + gameCommand = handleGameInput( input ); + if( gameCommand == -1 ){ + printf("command not found!"); + return; + } + }else{ + GAME.currentState = nextState; + return; + } + + // gameCommand processing + if( gameCommand == 1 ) { // set marker in field + int* params = getMarkerParameters( input ); // get the x and y values + setBoardMarker( GAME.board, params ); // apply the x and y values in the field + free(params); // prent memory leakage + + printBoard(); + + if( playerHasWon( GAME.board ) ){ + printf("\n\nDu hast gewonnwn!\n\n"); + // start menu + GAME.currentState=0; + } + } +} + +bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ + bool player = 1; + for (int i = 0; i < 3; i++) { + // check the rows + if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || + // check the columns + (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { + return true; // Spieler hat gewonnen + } + } + + // check the diagonal line + if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || + (board[0][2] == player && board[1][1] == player && board[2][0] == player)) { + return true; // Spieler hat gewonnen + } + + return false; +} + +int startGame( int code ){ + if( code == -1 ){ // usrCommand test + return 1; + } + + printf("Welcome to the game!\n"); + bool board[BORAD_SIZE][BORAD_SIZE]; + initializeBoard( board ); + + printBoard(); + + while( GAME.currentState == 1 ){ + handleGame(); + } + + return 0; +} \ No newline at end of file diff --git a/src/main/c/Georg/tictactoe.h b/src/main/c/Georg/tictactoe.h new file mode 100644 index 0000000..59f90d9 --- /dev/null +++ b/src/main/c/Georg/tictactoe.h @@ -0,0 +1,44 @@ +#ifndef TICTACTOE_H +#define TICTACTOE_H + +#include + +#define MAX_INPUT_LENGTH 20 +#define MAX_COMMANDS 3 +#define BORAD_SIZE 3 + +struct ticTacToe{ + int currentState; + bool board[BORAD_SIZE][BORAD_SIZE]; +}; + +// Typdefinition für einen Funktionszeiger +typedef int (*commandFunction)( int ); + +struct usrCommand{ + int id; + char* description; + commandFunction fun; +}; + +extern struct ticTacToe GAME; +extern struct usrCommand COMMANDS[MAX_COMMANDS]; + + +void startTicTacToe(); +char* getWelcomeMessageTicTacToe(void); +char* getRulesMessageTicTacToe(void); +struct ticTacToe createTicTacToe(); +int handleCommand( char* input ); +void initializeBoard( bool board[3][3] ); +int handleGameInput( char* input ); +int* getMarkerParameters(); +void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ); +bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE] ); + +/* commands */ +commandFunction getCommandById(int id); +int startMenu( int code ); +int startGame( int code ); + +#endif //TICTACTOE_H diff --git a/src/main/c/Tim/SchereSteinPapier.c b/src/main/c/Tim/SchereSteinPapier.c index 4d485f8..828d337 100644 --- a/src/main/c/Tim/SchereSteinPapier.c +++ b/src/main/c/Tim/SchereSteinPapier.c @@ -1,9 +1,260 @@ #include "SchereSteinPapier.h" #include +#include +#include +#include +void gameLoop(); +char* getWelcomeMessageSSP(); +int selectCOMChoice(); +void printResult(int, int, int); +int getuserSelection(); -int schereSteinPapier() + + +void schereSteinPapier() +{ + srand(time(NULL)); + printf("%s", getWelcomeMessageSSP()); + gameLoop(); +} + +void gameLoop() { - printf("Test"); - return 1; + while(1) + { + int comChoice, userChoice; + userChoice = getuserSelection(); + if(userChoice == 0) + { + printf("Vielen Dank fuers Spielen! Tschau!\n"); + break; + } + else if(userChoice == 1 || userChoice == 2 || userChoice == 3) + { + comChoice = selectCOMChoice(); + int winNum = calculateWinner(userChoice, comChoice); + printResult(winNum, comChoice, userChoice); + } + else + { + printf("Deine eingegebene Wahl ist ungueltig\n"); + } + } } +//Berechnung,welche Auswahl gewinnt. +//@return: 0 = unentschieden; 1 = gewonnen; -1 = verloren; 3 = Fehler bei der Wertuebergabe +//@param userSelection = UserChoice; comSelection = COMChoice +int calculateWinner(int userSelection, int comSelection) +{ + switch (userSelection) + { + case(1): + switch (comSelection) { + case(1): return 0; + + case(2): return -1; + + case(3): return 1; + + default: return 3; + } + case(2): + switch (comSelection) { + case(1): return 1; + + case(2): return 0; + + case(3): return -1; + + default: return 3; + } + case(3): + switch (comSelection) { + case(1): return -1; + + case(2): return 1; + + case(3): return 0; + + default: return 3; + } + default: return 3; + } + +} + +int selectCOMChoice() +{ + return rand() % 3 + 1; +} + +int getuserSelection() +{ + int userSelect; + printf("Bitte treffe deine Wahl!\n" + "1: Schere \n2: Stein \n3: Papier\n0: Spiel verlassen\n"); + scanf("%d", &userSelect); + + return userSelect; +} + +char* getWelcomeMessageSSP() +{ + return "\nHallo und Willkommen zu\n\n" + " _____ _ _____ _____ _ \r\n" + " | __ \\ | | | __ \\ / ____| (_) \r\n" + " | |__) |___ ___| | __ | |__) |_ _ _ __ ___ _ __ | (___ ___ _ ___ ___ ___ _ __ ___ \r\n" + " | _ // _ \\ / __| |/ / | ___/ _` | '_ \\ / _ \\ '__| \\___ \\ / __| / __/ __|/ _ \\| '__/ __|\r\n" + " | | \\ \\ (_) | (__| < | | | (_| | |_) | __/ | ____) | (__| \\__ \\__ \\ (_) | | \\__ \\\r\n" + " |_| \\_\\___/ \\___|_|\\_\\ |_| \\__,_| .__/ \\___|_| |_____/ \\___|_|___/___/\\___/|_| |___/ \r\n" + " | | \r\n" + " |_| \n" + "credits to wynand1004\n\n" + "In diesem Spiel spielst du gegen einen COM Schere-Stein-Papier!\n" + "Waehle, sobald dich die Konsole dazu auffordert, deine 'Waffe' aus, indem du die entsprechende Zahl eintippst.\n" + "Gibst du bei der Aufforderung 0 ein, gelangst du zurueck ins Hauptmenue!\n\n"; +} + +void printResult(int winNumb, int comSelect, int userSelect) +{ + char comWeapon[10]; + switch(comSelect) + { + case(1): + strcpy(comWeapon, "Schere"); + break; + case(2): + strcpy(comWeapon,"Stein"); + break; + case(3): + strcpy(comWeapon,"Papier"); + break; + default: + strcpy(comWeapon, "Not found"); + break; + } + if(userSelect == 1) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____)____ ____(____ '---\n" + " ______) (______\n" + " __________) (__________\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Scissor vs Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____)____ (____ '---\n" + " ______) (_____)\n" + " __________) (_____)\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Scissor vs Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____)____ ____(____ '----\n" + " ______) (______\n" + " __________) (_______\n" + " (____) (_______\n" + "---.__(___) (_________.---\n" + " Scissor vs Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else if(userSelect ==2) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____) ____(____ '---\n" + " (_____) (______\n" + " (_____) (__________\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Rock VS Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____) (____ '---\n" + " (_____) (_____)\n" + " (_____) (_____)\n" + " (____) (____)\n" + "---.__(___) (___)__.---\n" + " Rock VS Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____) ____(____ '----\n" + " (_____) (______\n" + " (_____) (_______\n" + " (____) (_______\n" + "---.__(___) (_________.---\n" + " Rock VS Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else if(userSelect ==3) + { + switch (comSelect) + { + case(1): + printf(" _______ _______\n" + "---' ____)____ ____(____ '---\n" + " ______) (______\n" + " _______) (__________\n" + " _______) (____)\n" + "---.__________) (___)__.---\n" + " Paper VS Scissor\n"); + break; + case(2): + printf(" _______ _______\n" + "---' ____)____ (____ '---\n" + " ______) (_____)\n" + " _______) (_____)\n" + " _______) (____)\n" + "---.__________) (___)__.---\n" + " Paper VS Rock\n"); + break; + case(3): + printf(" _______ _______\n" + "---' ____)____ ____(____ '----\n" + " ______) (______\n" + " _______) (_______\n" + " _______) (_______\n" + "---.__________) (_________.---\n" + " Paper VS Paper\n"); + break; + default: + printf("ungültige Eingabe\n"); + break; + } + } + else + { + printf("ungültige Eingabe"); + } + switch (winNumb) + { + case(-1):printf("Der Computer hat %s gewaehlt, Du hast verloren!!!\n\n", comWeapon); + break; + case(0): printf("Der Computer hat %s gewaehlt, Es steht unentschieden!!!\n\n", comWeapon); + break; + case(1): printf("Der Computer hat %s gewaehlt, Du hast gewonnen!!!\n\n", comWeapon); + break; + default: printf("Error!"); + break; + } +} \ No newline at end of file diff --git a/src/main/c/Tim/SchereSteinPapier.h b/src/main/c/Tim/SchereSteinPapier.h index 8b695c2..a96c9f9 100644 --- a/src/main/c/Tim/SchereSteinPapier.h +++ b/src/main/c/Tim/SchereSteinPapier.h @@ -1,6 +1,7 @@ #ifndef SCHERESTEINPAPIER_H #define SCHERESTEINPAPIER_H -int schereSteinPapier(); +void schereSteinPapier(); +int calculateWinner(int, int); #endif diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c new file mode 100644 index 0000000..08886d6 --- /dev/null +++ b/src/main/c/Tim/hangman.c @@ -0,0 +1,261 @@ +#include "hangman.h" +#include +#include +#include +#include +#include +#include + + + + +char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { + "Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", + "Kleiderschrank", "Schnee","Wasserhahn","Fernbedienung", + "Computertastatur", "Verlies","Zucchini","lizenzieren", + "Portemonnaie","brillant","Rückgrat","Toilettenpapier", + "Dachpappe","Hund","Zwiebelsuppe","Zebra", + "Kruzifix","Anschnallgurt","Bügeleisen","Fliesenleger", + "Adventskranz","Weihnachtsbaum","Autoreifen","Waschbecken", + "Busfahrkarte" +}; + +char hangmanStages[STAGENUM][ASCII_ART_SIZE]={ + "+---+\n" + "| |\n" + "|\n" + "|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| |\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "|\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "| /\n" + "|\n" + "=========\n", + "+---+\n" + "| |\n" + "| O\n" + "| /|\\\n" + "| / \\\n" + "|\n" + "=========\n", +}; + + +void hangman() +{ + srand(time(NULL)); + char userSelection; + getWelcomeMessageHangman(); + + do + { + char guessWord[MAX_WORD_LENGTH]; + int length; + int countWrongGuess=0; + strcpy(guessWord,getWordFromList(rand() % LISTSIZE)); + length = strlen(guessWord); + + char displayWord[MAX_WORD_LENGTH]; + drawHangman(countWrongGuess); + for (int i = 0; i <= length; i++) + { + if(i=0 && x0) + { + printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", selectedLetter); + return true; + } + else + { + return false; + } + +} + +void changeLetter(char selectedLetter, char guessWord[], int length, char ptrDisplayWord[]) +{ + for(int i = 0; i= POSSIBLE_TRYS) + { + printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\n", guessWord); + return true; + } + else + { + return false; + } +} + +char endGame() +{ + char userSelect; + endGameQuestionHangman(); + scanf(" %c", &userSelect); + + return userSelect; +} + + +void endGameQuestionHangman() +{ + printf("Moechtest du nochmal spielen?\n\nBeliebige Taste: Nochmal spielen\n 0 : Beenden\n"); +} \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h new file mode 100644 index 0000000..f68abf8 --- /dev/null +++ b/src/main/c/Tim/hangman.h @@ -0,0 +1,22 @@ +#include +#ifndef HANGMAN_H +#define HANGMAN_H + +#define LISTSIZE 30 +#define MAX_WORD_LENGTH 30 +#define STAGENUM 7 +#define ASCII_ART_SIZE 1000 +#define POSSIBLE_TRYS 6 + +void hangman(); +void getWelcomeMessageHangman(); +char* getWordFromList(int); +bool wordGuessed(char[],char[]); +bool letterGuessed(char, char[], int); +void changeLetter(char, char[], int, char[]); +void drawHangman(int); +bool noTrysLeft(int,char[]); +char endGame(); +void endGameQuestionHangman(); + +#endif diff --git a/src/main/c/main.c b/src/main/c/main.c index 6af2d92..4dc37a9 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -11,7 +11,9 @@ //todo // Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"' #include -#include "Tim/SchereSteinPapier.h" +#include "SchereSteinPapier.h" +#include "hangman.h" +#include "tictactoe.h" void openInterface(); @@ -31,7 +33,9 @@ void openInterface() // Vergesst das \n am Ende des Namens nicht!!! printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" "Du hast folgende Spiele zur Auswahl:\n\n" - "1: Schere-Stein-Papier\n"); + "1: Schere-Stein-Papier\n" + "2: Hangman\n" + "3: TicTacToe\n"); printf("\nBitte waehle die Zahl des entsprechenden Spiels aus, um damit zu starten.\nAm Ende eines Spiels kannst du mit der Taste 0 wieder zurueck zum Hauptmenue kommen.\nIm Hauptmenue beendest du mit der Auswahl 0 das Programm \n\n"); scanf_s("%d", &selection); @@ -44,13 +48,13 @@ void openInterface() case(1): schereSteinPapier(); break; - /*case(2): - //Spiel() - break; + case(2): + hangman(); + break; case(3): - //Spiel() + startTicTacToe(); break; - case(4): + /*case(4): //Spiel() break; case(5): diff --git a/src/test/c/Georg/test_tictactoe.c b/src/test/c/Georg/test_tictactoe.c new file mode 100644 index 0000000..6c2bcb5 --- /dev/null +++ b/src/test/c/Georg/test_tictactoe.c @@ -0,0 +1,358 @@ +#include "tictactoe.h" +#include "unity.h" + +void setup(void){ + +} + +void tearDown(void){ + +} + +void test_compileTest_shutBeAllwaysTrue(void){ + TEST_ASSERT_EQUAL_INT(1,1); +} + +void test_welcome_message(void){ + // arrange + char* expectedMessage = "Hallo und willkommen zu unserem TicTacToe Spiel. Anbei die Anleitung:\n"; + + // act + char* message = getWelcomeMessageTicTacToe(); + + // aassert + TEST_ASSERT_EQUAL_STRING(expectedMessage, message); +} + +void test_rules_message(void){ + // arrange + char* expectedMessage = "Das spiel wird über die Komandozeile gespielt.\n" + "Jeder Spielzug ist eine Eingabe in die Konsole. Die enstsprechenden Befehle stehen jeweils unterhalb des Spielfelds.\n" + "Um ein Zug zu tätigen musst du \"set x,y\" in die Konsole Eingeben. Die Koordinaten stehen dabei für Zeile und Spalte.\n" + "Mit dem Befehl \"start\" startest du das Spiel" + "Mit dem Befehl \"rules\" kannst du diese Nachricht erneut aufrufen."; + + // act + char* message = getRulesMessageTicTacToe(); + + // assert + TEST_ASSERT_EQUAL_STRING(expectedMessage, message); +} + +void test_initial_state(void){ + // arrange + struct ticTacToe newGame; + int expectedState = 0; + + // act + newGame = createTicTacToe(); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, newGame.currentState ); +} + +void test_command_startGame(void){ + // arrange + int expectedState = 1; + char* input = "start game"; + + // act + int actualState = handleCommand( input ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} + +void test_command_startMenu(void){ + // arrange + int expectedState = 0; + char* input = "start menu"; + + // act + int actualState = handleCommand( input ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedState, actualState ); +} + +void test_checkCommandlist(void){ + // arrange + + // act + + size_t arraySize = sizeof(*COMMANDS) / sizeof(COMMANDS[0]); + for (size_t i = 0; i < arraySize; i++) { + TEST_ASSERT_EQUAL_INT( 1, COMMANDS[i].fun(-1) ); + } +} + + +void test_callCommandById(void){ + // arrange + + // act + commandFunction actualCommand = getCommandById( 1 ); + + // assert + TEST_ASSERT_EQUAL_PTR( startMenu, actualCommand ); +} + +void test_callCommandById_startGame(void){ + // arrange + + // act + commandFunction actualCommand = getCommandById( 2); + + // assert + TEST_ASSERT_EQUAL_PTR( startGame, actualCommand ); +} + + +void test_initializeBoard(void){ + // arrange + bool expectedBoard[BORAD_SIZE][BORAD_SIZE]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + + // act + bool actualBoard[BORAD_SIZE][BORAD_SIZE]; + initializeBoard(actualBoard); + + // assert + for (size_t i = 0; i < BORAD_SIZE; i++) { + for (size_t ii = 0; ii < BORAD_SIZE; ii++) { + TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]); + } + } +} + +void test_handleGameInput(void){ + // arrange + char* teststring = "set 3,4"; + int expectedCommand = 1; + + // act + int actualState = handleGameInput( teststring ); + + // assert + TEST_ASSERT_EQUAL_INT( expectedCommand, actualState ); +} + +void test_getMarkerParameters(void){ + // arrange + char* teststring = "set 3,3"; + int expectedParams[2] = {2, 2}; + + // act + int* actualParams = getMarkerParameters( teststring ); + + // assert + for( int i = 0; i < 2; i++ ){ + TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] ); + } +} + +void test_setBoardFields(){ + // arrange + // arrange + bool board[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,0} + }; + int params[2] = {2,2}; + + // act + setBoardMarker( board, params ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, board[2][2] ); +} + +void test_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,1}, + {0,0,1} + }; + + bool board2[3][3]={ + {0,1,0}, + {0,1,0}, + {0,1,0} + }; + + bool board3[3][3]={ + {1,0,0}, + {1,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 1, result ); + TEST_ASSERT_EQUAL_INT( 1, result2 ); + TEST_ASSERT_EQUAL_INT( 1, result3 ); +} + +void test_negativ_checkOnWin_horizontally(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_horizontally2(void){ + // arrange + bool board1[3][3]={ + {1,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_vertically(void){ + // arrange + bool board1[3][3]={ + {1,0,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {1,0,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {1,0,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_vertically2(void){ + // arrange + bool board1[3][3]={ + {0,1,0}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,1,0}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,1,0} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} + +void test_negativ_checkOnWin_vertically3(void){ + // arrange + bool board1[3][3]={ + {0,0,1}, + {0,0,0}, + {0,0,0} + }; + + bool board2[3][3]={ + {0,0,0}, + {0,0,1}, + {0,0,0} + }; + + bool board3[3][3]={ + {0,0,0}, + {0,0,0}, + {0,0,1} + }; + + // act + bool result = playerHasWon( board1 ); + bool result2 = playerHasWon( board2 ); + bool result3 = playerHasWon( board3 ); + + // assert + TEST_ASSERT_EQUAL_INT( 0, result ); + TEST_ASSERT_EQUAL_INT( 0, result2 ); + TEST_ASSERT_EQUAL_INT( 0, result3 ); +} \ No newline at end of file diff --git a/src/test/c/Tim/test_SchereSteinPapier.c b/src/test/c/Tim/test_SchereSteinPapier.c index 9c41029..485a07b 100644 --- a/src/test/c/Tim/test_SchereSteinPapier.c +++ b/src/test/c/Tim/test_SchereSteinPapier.c @@ -1,5 +1,10 @@ #include "SchereSteinPapier.h" #include "unity.h" +#include + +const int scissor = 1; +const int rock = 2; +const int paper = 3; void setUp(void) { @@ -11,11 +16,147 @@ void tearDown(void) } +//Testfälle für Unentschieden +//1. Beide wählen Schere +void test_draw_USER_Scissor_COM_Scissor() +{ + //arrange + int expectedResult = 0; + //act + int actualResult = calculateWinner(scissor,scissor); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//2. Beide wählen Stein +void test_draw_USER_Rock_COM_Rock() +{ + //arrange + int expectedResult = 0; + //act + int actualResult = calculateWinner(rock,rock); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//3. Beide wählen Papier +void test_draw_USER_Paper_COM_Paper() +{ + //arrange + int expectedResult = 0; + //act + int actualResult = calculateWinner(paper,paper); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + + +//Testfälle für Niederlage +//1. User wählt Schere, Computer wählt Stein +void test_lose_USER_Scissor_COM_Rock() +{ + //arrange + int expectedResult = -1; + //act + int actualResult = calculateWinner(scissor,rock); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//2. User wählt Papier, Computer wählt Schere +void test_lose_USER_Paper_COM_Scissor() +{ + //arrange + int expectedResult = -1; + //act + int actualResult = calculateWinner(paper,scissor); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//3. User wählt Stein, Computer wählt Papier +void test_lose_USER_Rock_COM_Paper() +{ + //arrange + int expectedResult = -1; + //act + int actualResult = calculateWinner(rock,paper); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + -void testingCeedlingFunctionality() +//Testfälle für Sieg +//1. User wählt Schere, Computer wählt Papier +void test_win_USER_Scissor_COM_Paper() { + //arrange int expectedResult = 1; + //act + int actualResult = calculateWinner(scissor,paper); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//2. User wählt Stein, Computer wählt Schere +void test_win_USER_Rock_COM_Scissor() +{ + //arrange + int expectedResult = 1; + //act + int actualResult = calculateWinner(rock,scissor); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//3. User wählt Papier, Computer wählt Stein +void test_win_USER_Paper_COM_Rock() +{ + //arrange + int expectedResult = 1; + //act + int actualResult = calculateWinner(paper,rock); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} + - int actualResult = schereSteinPapier(); +//Testfälle für ungültige Parameterübergabe +//1. Durch den User wurde ein ungültiges Parameter übergeben +// Der Computer wählt eine Zahl zwischen 1 und 3 +void test_error_USER_unknownParameter_Rock_randomChoice() +{ + //arrange + int expectedResult = 3; + //act + int actualResult = calculateWinner(5,rand() % 3 + 1); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//2. Der User wählt eine Zahl zwischen 1 und 3 +// Durch den Computer wurde ein ungültiges Parameter übergeben +void test_error_USER_randomChoice_Rock_unknownParameter() +{ + //arrange + int expectedResult = 3; + //act + int actualResult = calculateWinner(rand() % 3 + 1, 5); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//3. Der User wählt eine Zahl zwischen 1 und 3 +// Durch den Computer wurde das ungültige Parameter 0 übergeben +void test_error_USER_randomChoice_Rock_zero() +{ + //arrange + int expectedResult = 3; + //act + int actualResult = calculateWinner(rand() % 3 + 1, 0); + //assert + TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); +} +//4. Durch den User wurde das ungültige Parameter 0 übergeben +// Der Computer wählt eine Zahl zwischen 1 und 3 +void test_error_USER_zero_Rock_randomChoice() +{ + //arrange + int expectedResult = 3; + //act + int actualResult = calculateWinner(0, rand() % 3 + 1); + //assert TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); } \ No newline at end of file diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c new file mode 100644 index 0000000..47f8e40 --- /dev/null +++ b/src/test/c/Tim/test_hangman.c @@ -0,0 +1,179 @@ +#include "hangman.h" +#include "unity.h" +#include +#include + + + +void setUp(void) +{ + +} + +void tearDown(void) +{ + +} + +void test_getWordFromList_Kartoffel_0() +{ + //arrange + int pos = 0; + char expectedResult[] = "Kartoffel"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + +void test_getWordFromList_Kleiderschrank_5() +{ + //arrange + int pos = 5; + char expectedResult[] = "Kleiderschrank"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + + +void test_getWordFromList_Index_lower_Listsize() +{ + //arrange + int pos = -5; + char expectedResult[] = "Index nicht vorhanden"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} +void test_getWordFromList_Index_higher_Listsize() +{ + //arrange + int pos = LISTSIZE+1; + char expectedResult[] = "Index nicht vorhanden"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + + +void test_wonGame_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] = "Kartoffel"; + //assert + TEST_ASSERT_TRUE(wordGuessed(word1, word2)); +} + +void test_not_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="Thunfisch"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} + +void test_wordGuessed_differentCaps() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="karTOFFel"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} + + +void test_letterGuessed_differentCaps_small_big() +{ + //arrange + char x ='F'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} + +void test_letterGuessed_differentCaps_big_small() +{ + //arrange + char x ='k'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} +void test_letterGuessed_sameCaps_small() +{ + //arrange + char x ='f'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} +void test_letterGuessed_differentLetter_small() +{ + //arrange + char x ='p'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} +void test_letterGuessed_differentLetter_big() +{ + //arrange + char x ='P'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} + + +void test_letterGuessed_sameCaps_big() +{ + //arrange + char x ='K'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} + +void test_noTrysLeft_x_equals_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_lower_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS-2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_FALSE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_higher_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS+2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} +