Browse Source

Merge branch 'dev'

main
KaffeeMaus 11 months ago
parent
commit
c0ef5161f8
  1. 72
      src/main/c/Georg/tictactoe.c
  2. 4
      src/main/c/Georg/tictactoe.h
  3. 0
      src/main/c/Stefan/place_your_code_here.txt
  4. 164
      src/main/c/Stefan/slot_machine.c
  5. 19
      src/main/c/Stefan/slot_machine.h
  6. 256
      src/main/c/Tim/hangman.c
  7. 16
      src/main/c/Tim/hangman.h
  8. 10
      src/main/c/main.c
  9. 0
      src/test/c/Stefan/place_your_tests_here.txt
  10. 224
      src/test/c/Stefan/test_slot_machine.c
  11. 165
      src/test/c/Tim/test_hangman.c

72
src/main/c/Georg/tictactoe.c

@ -8,25 +8,25 @@ void printBoard();
struct ticTacToe GAME; struct ticTacToe GAME;
struct command COMMANDS[MAX_COMMANDS] = {
struct usrCommand COMMANDS[MAX_COMMANDS] = {
{ 1, "\"start menu\" - startet das menu", startMenu}, { 1, "\"start menu\" - startet das menu", startMenu},
{ 2, "\"start game\" - startet das spiel", startGame} { 2, "\"start game\" - startet das spiel", startGame}
}; };
void startTicTacToe(){ void startTicTacToe(){
setbuf(stdout, 0);
setbuf(stdout, 0); // for debug output
printf( "%s\n", getWelcomeMessageTicTacToe() ); printf( "%s\n", getWelcomeMessageTicTacToe() );
printf( "%s\n\n", getRulesMessageTicTacToe() ); printf( "%s\n\n", getRulesMessageTicTacToe() );
GAME = createTicTacToe();
GAME = createTicTacToe(); // create the "game object"
while( GAME.currentState != -1 ){ while( GAME.currentState != -1 ){
commandFunction command;
printf("search command!\n");
command = getCommandById( GAME.currentState + 1);
commandFunction usrCommand;
//printf("search command!\n");
usrCommand = getCommandById( GAME.currentState + 1);
if( command != NULL)
command(0);
if( usrCommand != NULL)
usrCommand(0); // 0, for non test behavior
else{ else{
printf("command not found"); printf("command not found");
return; return;
@ -64,11 +64,11 @@ int handleCommand( char* input ){
commandFunction getCommandById( int id ){ commandFunction getCommandById( int id ){
commandFunction result = NULL; commandFunction result = NULL;
size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]);
size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]); // calculate size of Array
for (size_t i = 0; i < arraySize; i++) { for (size_t i = 0; i < arraySize; i++) {
//printf( "%s", COMMANDS[i].description ); //printf( "%s", COMMANDS[i].description );
if( COMMANDS[i].id == id ){ if( COMMANDS[i].id == id ){
result = COMMANDS[i].fun;
result = COMMANDS[i].fun; // save the function pointer as result
break; break;
} }
} }
@ -80,22 +80,26 @@ char* getUserInput(){
printf( ":" ); printf( ":" );
fgets(userInput, sizeof(userInput), stdin); fgets(userInput, sizeof(userInput), stdin);
size_t len = strlen(userInput);
if (len > 0 && userInput[len - 1] == '\n') {
userInput[len - 1] = '\0';
size_t lengthOfInput = strlen(userInput);
if (lengthOfInput > 0 && userInput[lengthOfInput - 1] == '\n') {
userInput[lengthOfInput - 1] = '\0'; // declare end of command
} }
return userInput; return userInput;
} }
void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){ void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
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 ){ int handleGameInput( char* input ){
if( strstr(input, "set") != NULL ){ if( strstr(input, "set") != NULL ){
return 1; return 1;
@ -105,7 +109,7 @@ int handleGameInput( char* input ){
} }
int startMenu( int code ){ int startMenu( int code ){
if( code == -1 ){ // command test
if( code == -1 ){ // usrCommand test
return 1; return 1;
} }
@ -125,15 +129,16 @@ int startMenu( int code ){
} }
void printBoard(){ void printBoard(){
// calculate the size of the board and print the upper frame
for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ for( int i = 0; i < BORAD_SIZE*4+1; i++ ){
printf("-"); printf("-");
} }
printf("\n"); printf("\n");
for (int i = 0; i < 3; ++i) {
for (int line = 0; line < 3; ++line) {
printf("| "); printf("| ");
for (int j = 0; j < 3; ++j) {
if( GAME.board[i][j] == true )
for (int column = 0; column < 3; ++column) {
if( GAME.board[line][column] == true )
printf( "X" ); printf( "X" );
else else
printf ( " " ); printf ( " " );
@ -143,12 +148,18 @@ void printBoard(){
printf( "\n" ); printf( "\n" );
} }
// calculate the size of the board and print the upper frame
for( int i = 0; i < BORAD_SIZE*4+1; i++ ){ for( int i = 0; i < BORAD_SIZE*4+1; i++ ){
printf("-"); printf("-");
} }
printf("\n"); 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* getMarkerParameters( char* input ){
int* array = (int*)malloc(2 * sizeof(int)); int* array = (int*)malloc(2 * sizeof(int));
@ -156,8 +167,8 @@ int* getMarkerParameters( char* input ){
int firstArgument = input[index-1] - '0'; int firstArgument = input[index-1] - '0';
int secondArgument = input[index+1] - '0'; int secondArgument = input[index+1] - '0';
array[0] = firstArgument-1;
array[1] = secondArgument-1;
array[0] = firstArgument-1; // return x
array[1] = secondArgument-1; // return y
return array; return array;
} }
@ -186,9 +197,9 @@ void handleGame(){
// gameCommand processing // gameCommand processing
if( gameCommand == 1 ) { // set marker in field if( gameCommand == 1 ) { // set marker in field
int* params = getMarkerParameters( input );
setBoardMarker( GAME.board, params );
free(params);
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(); printBoard();
@ -202,17 +213,16 @@ void handleGame(){
bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){ bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){
bool player = 1; bool player = 1;
// Überprüfe Zeilen und Spalten
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
// Überprüfe Zeilen
// check the rows
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
// Überprüfe Spalten
// check the columns
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) { (board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true; // Spieler hat gewonnen return true; // Spieler hat gewonnen
} }
} }
// Überprüfe Diagonalen
// check the diagonal line
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || 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)) { (board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true; // Spieler hat gewonnen return true; // Spieler hat gewonnen
@ -222,7 +232,7 @@ bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){
} }
int startGame( int code ){ int startGame( int code ){
if( code == -1 ){ // command test
if( code == -1 ){ // usrCommand test
return 1; return 1;
} }

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

@ -15,14 +15,14 @@ struct ticTacToe{
// Typdefinition für einen Funktionszeiger // Typdefinition für einen Funktionszeiger
typedef int (*commandFunction)( int ); typedef int (*commandFunction)( int );
struct command{
struct usrCommand{
int id; int id;
char* description; char* description;
commandFunction fun; commandFunction fun;
}; };
extern struct ticTacToe GAME; extern struct ticTacToe GAME;
extern struct command COMMANDS[MAX_COMMANDS];
extern struct usrCommand COMMANDS[MAX_COMMANDS];
void startTicTacToe(); void startTicTacToe();

0
src/main/c/Stefan/place_your_code_here.txt

164
src/main/c/Stefan/slot_machine.c

@ -0,0 +1,164 @@
#include "slot_machine.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
void slotMachine(){
welcomeMessage();
char symbols[NUM_OF_SYMBOLS];
int balance = getBalance();
while(balance > 0){
int bet = getBet(balance);
balance = subtractBetFromBalance(bet, balance);
randomizeSymbols(symbols);
balance = spin(symbols, bet, balance);
}
}
int getBalance(){
int balance;
printf("Guthaben einwerfen: ");
balance = userInput();
printf("Dein Guthaben: %d\n", balance);
return balance;
}
int userInput(){
int input;
scanf("%d", &input);
return input;
}
int getBet(int balance){
while(1){
printf("Wetteinsatz: ");
int bet = userInput();
if(bet <= balance && bet > 0){
return bet;
} else {
printf("Ungueltige Eingabe!\n");
}
}
}
int subtractBetFromBalance(int bet, int balance){
return balance - bet;
}
void randomizeSymbols(char symbols[]){
srand(time(NULL));
for (int i = 0; i < NUM_OF_SYMBOLS; i++) {
int r = rand() % 100;
// 35% chance für A
if (r < 35) {
symbols[i] = 'A';
}
// 30% chance für B
else if (r < 65) {
symbols[i] = 'B';
}
// 20% chance für C
else if (r < 85) {
symbols[i] = 'C';
}
// 15% chance für D
else {
symbols[i] = 'D';
}
}
}
int spin(char symbols[], int bet, int balance){
int winnings = getWinnings(symbols, bet);
showResult(symbols, winnings);
balance += winnings;
printf("Aktuelles Guthaben: %d\n-------------------------\n", balance);
return balance;
}
int getWinnings(char symbols[], int bet){
int winnings = 0;
// 0 | 1 | 2
// 3 | 4 | 5
// 6 | 7 | 8
// 3 gleiche Symbole in einer Reihe gewinnen, Gewinn berechnen
for (int i = 0; i < ROWS; i++){
if (symbols[i * ROWS] == symbols[i * ROWS + 1] && symbols[i * ROWS] == symbols[i * ROWS + 2]) {
switch(symbols[i * ROWS]){
case 'A': winnings += bet * 3; break;
case 'B': winnings += bet * 5; break;
case 'C': winnings += bet * 10; break;
case 'D': winnings += bet * 20; break;
}
}
}
if (winnings > 0) {
printf("\nDu hast %d gewonnen!\n", winnings - bet);
}
else {
printf("\nLeider verloren.\n");
}
return winnings;
}
void showResult(char symbols[], int winnings){
char winnerMessage[] = {""};
if(winnings > 0){
strcpy(winnerMessage, " WINNER ");
} else {
strcpy(winnerMessage, " LOSER ");
}
printf("\n"
" .-------.\n"
" {-%s-} \n"
" .=============.\n"
" | | __\n"
" | [%c] [%c] [%c] |( )\n"
" | [%c] [%c] [%c] | ||\n"
" | [%c] [%c] [%c] | ||\n"
" | |_||\n"
" | xxx ::::::: |--'\n"
" | ooo ::::::: |\n"
" | $$$ ::::::: |\n"
" | __ |\n"
" |_____/__\\____|\n\n", winnerMessage, symbols[0], symbols[1], symbols[2], symbols[3], symbols[4], symbols[5], symbols[6], symbols[7], symbols[8]);
}
void welcomeMessage(){
printf("Herzlich Willkommen zur \n\n"
" _ _ _ _ \n"
" | | | | | | (_) \n"
" ___| | ___ | |_ _ __ ___ __ _ ___| |__ _ _ __ ___ \n"
"/ __| |/ _ \\| __| | '_ ` _ \\ / _` |/ __| '_ \\| | '_ \\ / _ \\\n"
"\\__ \\ | (_) | |_ | | | | | (_| | (__| | | | | | | | __/\n"
"|___/_|\\___/ \\__| |_| |_| |_|\\__,_|\\___|_| |_|_|_| |_|\\___|\n\n");
}

19
src/main/c/Stefan/slot_machine.h

@ -0,0 +1,19 @@
#ifndef SLOT_MACHINE_H
#define SLOT_MACHINE_H
#define NUM_OF_SYMBOLS 9
#define ROWS 3
void slotMachine();
void welcomeMessage();
int getBalance();
int userInput();
int getBet(int balance);
int subtractBetFromBalance(int bet, int balance);
void randomizeSymbols(char symbols[]);
int spin(char symbols[], int bet, int balance);
int getWinnings(char symbols[], int bet);
void showResult(char symbols[], int winnings);
#endif // SLOT_MACHINE_H

256
src/main/c/Tim/hangman.c

@ -1,7 +1,261 @@
#include "hangman.h" #include "hangman.h"
#include <stdio.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() void hangman()
{ {
printf("Hello World!");
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");
}

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

@ -1,6 +1,22 @@
#include <stdbool.h>
#ifndef HANGMAN_H #ifndef HANGMAN_H
#define 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 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 #endif

10
src/main/c/main.c

@ -14,6 +14,7 @@
#include "SchereSteinPapier.h" #include "SchereSteinPapier.h"
#include "hangman.h" #include "hangman.h"
#include "tictactoe.h" #include "tictactoe.h"
#include "Stefan/slot_machine.h"
void openInterface(); void openInterface();
@ -35,7 +36,8 @@ void openInterface()
"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" "2: Hangman\n"
"3: TicTacToe\n");
"3: TicTacToe\n"
"4: Slot Machine\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);
@ -54,10 +56,10 @@ void openInterface()
case(3): case(3):
startTicTacToe(); startTicTacToe();
break; break;
/*case(4):
//Spiel()
case(4):
slotMachine();
break; break;
case(5):
/*case(5):
//Spiel() //Spiel()
break;*/ break;*/
default: default:

0
src/test/c/Stefan/place_your_tests_here.txt

224
src/test/c/Stefan/test_slot_machine.c

@ -0,0 +1,224 @@
#include "slot_machine.h"
#include "unity.h"
void setUp() {}
void tearDown() {}
void test_subtract_bet_10_from_balance_10() {
//arrange
int expectedResult = 0;
//act
int actualResult = subtractBetFromBalance(10, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_subtract_bet_0_from_balance_10() {
//arrange
int expectedResult = 10;
//act
int actualResult = subtractBetFromBalance(0, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_subtract_bet_5_from_balance_10() {
//arrange
int expectedResult = 5;
//act
int actualResult = subtractBetFromBalance(5, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_no_rows_won_bet_equals_10() {
//arrange
char testSymbols[] = {"AABAACAAD"};
int expectedResult = 0;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_A_won_bet_equals_10() {
//arrange
char testSymbols[] = {"AAAAACAAD"};
int expectedResult = 30;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_B_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BBBAACAAD"};
int expectedResult = 50;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_C_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BABAACCCC"};
int expectedResult = 100;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_D_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BABDDDCAC"};
int expectedResult = 200;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_6_A_won_bet_equals_10() {
//arrange
char testSymbols[] = {"AAAAAACAC"};
int expectedResult = 60;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_6_B_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BBBBBBCAC"};
int expectedResult = 100;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_6_C_won_bet_equals_10() {
//arrange
char testSymbols[] = {"CCCCCCCAC"};
int expectedResult = 200;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_6_D_won_bet_equals_10() {
//arrange
char testSymbols[] = {"DDDDDDCAC"};
int expectedResult = 400;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_A_3_B_won_bet_equals_10() {
//arrange
char testSymbols[] = {"AAABBBCAC"};
int expectedResult = 80;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_C_3_D_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BAACCCDDD"};
int expectedResult = 300;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_9_A_won_bet_equals_10() {
//arrange
char testSymbols[] = {"AAAAAAAAA"};
int expectedResult = 90;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
void test_3_B_3_C_3_D_won_bet_equals_10() {
//arrange
char testSymbols[] = {"BBBCCCDDD"};
int expectedResult = 350;
//act
int actualResult = getWinnings(testSymbols, 10);
//assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}

165
src/test/c/Tim/test_hangman.c

@ -1,5 +1,7 @@
#include "hangman.h" #include "hangman.h"
#include "unity.h" #include "unity.h"
#include <string.h>
#include <stdbool.h>
@ -13,12 +15,165 @@ void tearDown(void)
} }
void test_ceedling_functionality()
void test_getWordFromList_Kartoffel_0()
{ {
//arrange //arrange
int expectedResult = 0;
int pos = 0;
char expectedResult[] = "Kartoffel";
//act //act
int actualResult = 0;
char actualResult[30];
strcpy(actualResult,getWordFromList(pos));
//assert //assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
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