Browse Source

Merge branch 'dev' into stefan

remotes/origin/dev
fdai7472 11 months ago
parent
commit
d9dc94f628
  1. 250
      src/main/c/Georg/tictactoe.c
  2. 44
      src/main/c/Georg/tictactoe.h
  3. 257
      src/main/c/Tim/SchereSteinPapier.c
  4. 3
      src/main/c/Tim/SchereSteinPapier.h
  5. 261
      src/main/c/Tim/hangman.c
  6. 22
      src/main/c/Tim/hangman.h
  7. 18
      src/main/c/main.c
  8. 358
      src/test/c/Georg/test_tictactoe.c
  9. 145
      src/test/c/Tim/test_SchereSteinPapier.c
  10. 179
      src/test/c/Tim/test_hangman.c

250
src/main/c/Georg/tictactoe.c

@ -0,0 +1,250 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

44
src/main/c/Georg/tictactoe.h

@ -0,0 +1,44 @@
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include <stdbool.h>
#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

257
src/main/c/Tim/SchereSteinPapier.c

@ -1,9 +1,260 @@
#include "SchereSteinPapier.h" #include "SchereSteinPapier.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
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;
}
}

3
src/main/c/Tim/SchereSteinPapier.h

@ -1,6 +1,7 @@
#ifndef SCHERESTEINPAPIER_H #ifndef SCHERESTEINPAPIER_H
#define SCHERESTEINPAPIER_H #define SCHERESTEINPAPIER_H
int schereSteinPapier();
void schereSteinPapier();
int calculateWinner(int, int);
#endif #endif

261
src/main/c/Tim/hangman.c

@ -0,0 +1,261 @@
#include "hangman.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <ctype.h>
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<length)
{
displayWord[i] = '_';
}
else if(i == length)
{
displayWord[i] = '\0';
}
}
printf("\n\n%s\n", displayWord);
while(1)
{
printf("Bitte gib einen Buchstaben ein!\n");
scanf(" %c", &userSelection);
if(userSelection == '0')
{
break;
}
if(!letterGuessed(userSelection, guessWord, length))
{
countWrongGuess++;
printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", POSSIBLE_TRYS-countWrongGuess);
}
changeLetter(userSelection, guessWord, length, displayWord);
drawHangman(countWrongGuess);
if(wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess,guessWord))
{
break;
}
printf("\n\n%s\n", displayWord);
}
userSelection = endGame();
}while(userSelection != '0');
printf("Danke fuers Spielen! Auf Wiedersehen!\n");
}
void getWelcomeMessageHangman()
{
printf("Willkommen bei Hangman!!!\n\n");
drawHangman(6);
printf("\nPer Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest, dass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n"
"Fuer jede falsche Antwort kommst du dem Tod immer naeher, also waehle weise!\n"
"Mit der Auswahl 0 kommst du zurueck ins Hauptmenue\n\n");
}
char* getWordFromList(int x)
{
if(x>=0 && x<LISTSIZE)
{
return wordlist[x];
}
else
{
return "Index nicht vorhanden";
}
}
bool wordGuessed(char guessWord[], char displayWord[])
{
if(strcmp(guessWord, displayWord) == 0)
{
printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\n",guessWord);
return true;
}
else
{
return false;
}
}
bool letterGuessed(char selectedLetter, char guessWord[], int length)
{
int counter = 0;
for(int i = 0; i<length; i++) {
if(isupper(selectedLetter) != 0)
{
if (selectedLetter == guessWord[i] - 32 || selectedLetter == guessWord[i] )
{
counter++;
}
}
else
{
if (selectedLetter == guessWord[i] || selectedLetter == guessWord[i] + 32)
{
counter++;
}
}
}
if(counter>0)
{
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<length; i++) {
if(isupper(selectedLetter) != 0)
{
if (selectedLetter == guessWord[i] - 32 || selectedLetter == guessWord[i] )
{
if(i ==0 && isupper(guessWord[0]) != 0)
{
ptrDisplayWord[i] = selectedLetter;
}
else
{
ptrDisplayWord[i] = selectedLetter + 32;
}
}
}
else
{
if (selectedLetter == guessWord[i] || selectedLetter == guessWord[i] + 32)
{
if(i == 0 && isupper(guessWord[0]) != 0)
{
ptrDisplayWord[i] = selectedLetter - 32;
}
else {
ptrDisplayWord[i] = selectedLetter;
}
}
}
}
}
void drawHangman(int wrongGuessCount)
{
printf("%s", hangmanStages[wrongGuessCount]);
}
bool noTrysLeft(int wrongGuessCount, char guessWord[])
{
if(wrongGuessCount >= 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");
}

22
src/main/c/Tim/hangman.h

@ -0,0 +1,22 @@
#include <stdbool.h>
#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

18
src/main/c/main.c

@ -11,7 +11,9 @@
//todo //todo
// Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"' // Includiert hier euer .h-Datei für das entsprechende Spiel mit '#include "datei.h"'
#include <stdio.h> #include <stdio.h>
#include "Tim/SchereSteinPapier.h"
#include "SchereSteinPapier.h"
#include "hangman.h"
#include "tictactoe.h"
void openInterface(); void openInterface();
@ -31,7 +33,9 @@ void openInterface()
// Vergesst das \n am Ende des Namens nicht!!! // Vergesst das \n am Ende des Namens nicht!!!
printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n" printf("\n\nHallo und willkommen bei unserer Spielesammlung!!!\n"
"Du hast folgende Spiele zur Auswahl:\n\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"); 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); scanf_s("%d", &selection);
@ -44,13 +48,13 @@ void openInterface()
case(1): case(1):
schereSteinPapier(); schereSteinPapier();
break; break;
/*case(2):
//Spiel()
break;
case(2):
hangman();
break;
case(3): case(3):
//Spiel()
startTicTacToe();
break; break;
case(4):
/*case(4):
//Spiel() //Spiel()
break; break;
case(5): case(5):

358
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 );
}

145
src/test/c/Tim/test_SchereSteinPapier.c

@ -1,5 +1,10 @@
#include "SchereSteinPapier.h" #include "SchereSteinPapier.h"
#include "unity.h" #include "unity.h"
#include <stdlib.h>
const int scissor = 1;
const int rock = 2;
const int paper = 3;
void setUp(void) 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; 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); TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
} }

179
src/test/c/Tim/test_hangman.c

@ -0,0 +1,179 @@
#include "hangman.h"
#include "unity.h"
#include <string.h>
#include <stdbool.h>
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));
}
Loading…
Cancel
Save