From b6dfc360e648fdd9d491ccf399d6967fa17b1316 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 11 Jan 2023 10:27:42 +0100 Subject: [PATCH 01/23] made ceedling-extention usable --- project.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/project.yml b/project.yml index e253248..33ad5c2 100644 --- a/project.yml +++ b/project.yml @@ -98,4 +98,5 @@ :enabled: - stdout_pretty_tests_report - module_generator + - xml_tests_report ... From d329ec78cb2e4ec64b2f9b1cb262d16dbf3951a0 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 11 Jan 2023 11:34:01 +0100 Subject: [PATCH 02/23] same Results no winner --- src/c/rockPaperScissors.c | 12 ++++++++++ src/c/rockPaperScissors.h | 6 +++++ test/c/test_rockPaperScissors.c | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/c/rockPaperScissors.c create mode 100644 src/c/rockPaperScissors.h create mode 100644 test/c/test_rockPaperScissors.c diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c new file mode 100644 index 0000000..2a1b239 --- /dev/null +++ b/src/c/rockPaperScissors.c @@ -0,0 +1,12 @@ +#include +#include +#include + +#include "rockPaperScissors.h" + + +char findWinner(int inputPlayer, int inputComputer){ + if (inputPlayer == inputComputer){ + return 'n'; + } +} \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h new file mode 100644 index 0000000..4531af5 --- /dev/null +++ b/src/c/rockPaperScissors.h @@ -0,0 +1,6 @@ +#ifndef ROCKPAPERSCISSORS_H +#define ROCKPAPERSCISSORS_H + +char findWinner(int inputPlayer, int inputComputer); + +#endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c new file mode 100644 index 0000000..fd2745e --- /dev/null +++ b/test/c/test_rockPaperScissors.c @@ -0,0 +1,42 @@ +#ifdef TEST +#include "unity.h" +//in example.h wird die Funktion deklariert +#include "rockPaperScissors.h" + +//Vor- bzw. Nachbereitung +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +//Hier läuft der Test +void test_rockPaperScissors_sameResult(void) +{ + /* arrange */ + //Hier die Werte eingeben + char result; //p=player, c=computer, n=none + int inputPlayer = 1; + int inputComputer = inputPlayer; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('n', result); +} + +#endif // TEST + +/*Testcases: +rockPaperScissors_sameResult +rockPaperScissors_differentResultsPlayerWins +rockPaperScissors_differentResultsPlayerLoses +rockPaperScissors_playerGetsBestOutOf3 +rockPaperScissors_computerGetsBestOutOf3 +rockPaperScissors_outputResult +*/ \ No newline at end of file From 83699f1a2708663c8ebe6d42c2b08a935b7010b3 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 11 Jan 2023 21:06:15 +0100 Subject: [PATCH 03/23] player wins round --- src/c/rockPaperScissors.c | 15 +++++++++++ src/c/rockPaperScissors.h | 6 +++++ test/c/test_rockPaperScissors.c | 46 ++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 2a1b239..a741b51 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -9,4 +9,19 @@ char findWinner(int inputPlayer, int inputComputer){ if (inputPlayer == inputComputer){ return 'n'; } + else if (inputPlayer == ROCK){ + if (inputComputer == SCISSORS){ + return 'p'; + } + } + else if (inputPlayer == PAPER){ + if (inputComputer == ROCK){ + return 'p'; + } + } + else if (inputPlayer == SCISSORS){ + if (inputComputer == PAPER){ + return 'p'; + } + } } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 4531af5..7ce0730 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -1,6 +1,12 @@ #ifndef ROCKPAPERSCISSORS_H #define ROCKPAPERSCISSORS_H +enum inputOptions{ + ROCK = 1, + PAPER = 2, + SCISSORS = 3 + }; + char findWinner(int inputPlayer, int inputComputer); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index fd2745e..e533bf1 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -18,7 +18,7 @@ void test_rockPaperScissors_sameResult(void) /* arrange */ //Hier die Werte eingeben char result; //p=player, c=computer, n=none - int inputPlayer = 1; + int inputPlayer = ROCK; int inputComputer = inputPlayer; /* act */ @@ -30,6 +30,50 @@ void test_rockPaperScissors_sameResult(void) TEST_ASSERT_EQUAL_INT('n', result); } +void test_rockPaperScissors_differentResultsPlayerWins(void) +{ + /* arrange */ + //Hier die Werte eingeben + char result; //p=player, c=computer, n=none + int inputPlayer = ROCK; + int inputComputer = SCISSORS; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('p', result); + + /* arrange */ + //Hier die Werte eingeben + inputPlayer = PAPER; + inputComputer = ROCK; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('p', result); + + /* arrange */ + //Hier die Werte eingeben + inputPlayer = SCISSORS; + inputComputer = PAPER; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('p', result); +} + + #endif // TEST /*Testcases: From 96183ac1150728d80d3498237105d72dff04abb3 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 12 Jan 2023 11:09:11 +0100 Subject: [PATCH 04/23] player loses round --- src/c/rockPaperScissors.c | 9 +++++++ test/c/test_rockPaperScissors.c | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index a741b51..40a00fe 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -13,15 +13,24 @@ char findWinner(int inputPlayer, int inputComputer){ if (inputComputer == SCISSORS){ return 'p'; } + else if (inputComputer == PAPER){ + return 'c'; + } } else if (inputPlayer == PAPER){ if (inputComputer == ROCK){ return 'p'; } + else if (inputComputer == SCISSORS){ + return 'c'; + } } else if (inputPlayer == SCISSORS){ if (inputComputer == PAPER){ return 'p'; } + else if (inputComputer == ROCK){ + return 'c'; + } } } \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index e533bf1..327b2d7 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -73,6 +73,48 @@ void test_rockPaperScissors_differentResultsPlayerWins(void) TEST_ASSERT_EQUAL_INT('p', result); } +void test_rockPaperScissors_differentResultsPlayerLoses(void) +{ + /* arrange */ + //Hier die Werte eingeben + char result; //p=player, c=computer, n=none + int inputPlayer = ROCK; + int inputComputer = PAPER; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('c', result); + + /* arrange */ + //Hier die Werte eingeben + inputPlayer = PAPER; + inputComputer = SCISSORS; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('c', result); + + /* arrange */ + //Hier die Werte eingeben + inputPlayer = SCISSORS; + inputComputer = ROCK; + + /* act */ + //Die Funktion wird ausgeführt + result = findWinner(inputPlayer, inputComputer); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT('c', result); +} #endif // TEST @@ -83,4 +125,5 @@ rockPaperScissors_differentResultsPlayerLoses rockPaperScissors_playerGetsBestOutOf3 rockPaperScissors_computerGetsBestOutOf3 rockPaperScissors_outputResult +rockPaperScissors_invalidInput */ \ No newline at end of file From 03e14ca157a9968a456d17f7574c3ed83f7f3367 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 12 Jan 2023 11:46:04 +0100 Subject: [PATCH 05/23] refactoring: findWinner duplications removed --- src/c/rockPaperScissors.c | 33 +++++++++++---------------------- src/c/rockPaperScissors.h | 15 +++++++++++---- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 40a00fe..0171e87 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -6,31 +6,20 @@ char findWinner(int inputPlayer, int inputComputer){ + if (inputPlayer == inputComputer){ - return 'n'; + return NOWINNER; + } + else if (inputPlayer == ROCK && inputComputer == SCISSORS) { + return PLAYERWINSROUND; } - else if (inputPlayer == ROCK){ - if (inputComputer == SCISSORS){ - return 'p'; - } - else if (inputComputer == PAPER){ - return 'c'; - } + else if (inputPlayer == PAPER && inputComputer == ROCK) { + return PLAYERWINSROUND; } - else if (inputPlayer == PAPER){ - if (inputComputer == ROCK){ - return 'p'; - } - else if (inputComputer == SCISSORS){ - return 'c'; - } + else if (inputPlayer == SCISSORS && inputComputer == PAPER) { + return PLAYERWINSROUND; } - else if (inputPlayer == SCISSORS){ - if (inputComputer == PAPER){ - return 'p'; - } - else if (inputComputer == ROCK){ - return 'c'; - } + else { + return COMPUTERWINSROUND; } } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 7ce0730..cda1894 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -2,11 +2,18 @@ #define ROCKPAPERSCISSORS_H enum inputOptions{ - ROCK = 1, - PAPER = 2, - SCISSORS = 3 - }; + ROCK, + PAPER, + SCISSORS +}; +enum roundWinner{ + PLAYERWINSROUND = 'p', + COMPUTERWINSROUND = 'c', + NOWINNER = 'n' +}; + + char findWinner(int inputPlayer, int inputComputer); #endif \ No newline at end of file From b5af819bb7ca97276d1a89b1245a071ef8e9ee98 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 12 Jan 2023 11:52:16 +0100 Subject: [PATCH 06/23] refactoring: unittests naming improved --- src/c/rockPaperScissors.h | 6 +++--- test/c/test_rockPaperScissors.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index cda1894..73dd319 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -8,9 +8,9 @@ enum inputOptions{ }; enum roundWinner{ - PLAYERWINSROUND = 'p', - COMPUTERWINSROUND = 'c', - NOWINNER = 'n' + PLAYERWINSROUND, + COMPUTERWINSROUND, + NOWINNER }; diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 327b2d7..dea8ee4 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -17,7 +17,7 @@ void test_rockPaperScissors_sameResult(void) { /* arrange */ //Hier die Werte eingeben - char result; //p=player, c=computer, n=none + char result; int inputPlayer = ROCK; int inputComputer = inputPlayer; @@ -27,14 +27,14 @@ void test_rockPaperScissors_sameResult(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('n', result); + TEST_ASSERT_EQUAL_INT(NOWINNER, result); } void test_rockPaperScissors_differentResultsPlayerWins(void) { /* arrange */ //Hier die Werte eingeben - char result; //p=player, c=computer, n=none + char result; int inputPlayer = ROCK; int inputComputer = SCISSORS; @@ -44,7 +44,7 @@ void test_rockPaperScissors_differentResultsPlayerWins(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('p', result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); /* arrange */ //Hier die Werte eingeben @@ -57,7 +57,7 @@ void test_rockPaperScissors_differentResultsPlayerWins(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('p', result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); /* arrange */ //Hier die Werte eingeben @@ -70,14 +70,14 @@ void test_rockPaperScissors_differentResultsPlayerWins(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('p', result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); } void test_rockPaperScissors_differentResultsPlayerLoses(void) { /* arrange */ //Hier die Werte eingeben - char result; //p=player, c=computer, n=none + char result; int inputPlayer = ROCK; int inputComputer = PAPER; @@ -87,7 +87,7 @@ void test_rockPaperScissors_differentResultsPlayerLoses(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('c', result); + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); /* arrange */ //Hier die Werte eingeben @@ -100,7 +100,7 @@ void test_rockPaperScissors_differentResultsPlayerLoses(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('c', result); + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); /* arrange */ //Hier die Werte eingeben @@ -113,7 +113,7 @@ void test_rockPaperScissors_differentResultsPlayerLoses(void) /* assert */ //Vergleichen - TEST_ASSERT_EQUAL_INT('c', result); + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); } #endif // TEST From 4da4f238f9367cbb2e88bb735e86e36eceb809c5 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 12 Jan 2023 13:26:47 +0100 Subject: [PATCH 07/23] generate computer input --- src/c/rockPaperScissors.c | 7 +++++-- src/c/rockPaperScissors.h | 6 ++++++ test/c/test_rockPaperScissors.c | 18 +++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 0171e87..240f0ec 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -4,9 +4,12 @@ #include "rockPaperScissors.h" +char getComputerInput(){ + int input = rand() % 3; + return input; +} char findWinner(int inputPlayer, int inputComputer){ - if (inputPlayer == inputComputer){ return NOWINNER; } @@ -22,4 +25,4 @@ char findWinner(int inputPlayer, int inputComputer){ else { return COMPUTERWINSROUND; } -} \ No newline at end of file +} diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 73dd319..e31dbcd 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -13,7 +13,13 @@ enum roundWinner{ NOWINNER }; +enum gameWinner{ + PLAYERWINSGAME, + COMPUTERWINSGAME +}; + char findWinner(int inputPlayer, int inputComputer); +char getComputerInput(); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index dea8ee4..141ebb8 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -116,12 +116,24 @@ void test_rockPaperScissors_differentResultsPlayerLoses(void) TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); } +void test_rockPaperScissors_generateComputerInput(void) +{ + /* arrange */ + //Hier die Werte eingeben + char result; + + /* act */ + //Die Funktion wird ausgeführt + result = getComputerInput(); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(ROCK || PAPER || SCISSORS, result); +} + #endif // TEST /*Testcases: -rockPaperScissors_sameResult -rockPaperScissors_differentResultsPlayerWins -rockPaperScissors_differentResultsPlayerLoses rockPaperScissors_playerGetsBestOutOf3 rockPaperScissors_computerGetsBestOutOf3 rockPaperScissors_outputResult From 793c94a2149a38dedaa951d9baa68a79087898d4 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Sun, 15 Jan 2023 17:04:52 +0100 Subject: [PATCH 08/23] player wins game --- src/c/rockPaperScissors.c | 9 +++++++++ src/c/rockPaperScissors.h | 4 +++- test/c/test_rockPaperScissors.c | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 240f0ec..29b6ed5 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -26,3 +26,12 @@ char findWinner(int inputPlayer, int inputComputer){ return COMPUTERWINSROUND; } } + +char wasGameWon(roundsToWin, playerWins, computerWins){ + char winner; + if (playerWins == roundsToWin){ + winner = PLAYERWINSGAME; + } + return winner; +} + diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index e31dbcd..611fcf6 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -15,11 +15,13 @@ enum roundWinner{ enum gameWinner{ PLAYERWINSGAME, - COMPUTERWINSGAME + COMPUTERWINSGAME, + NOTWONYET }; char findWinner(int inputPlayer, int inputComputer); char getComputerInput(); +char wasGameWon(roundsToWin, playerWins, computerWins); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 141ebb8..ac07ab9 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -131,6 +131,23 @@ void test_rockPaperScissors_generateComputerInput(void) TEST_ASSERT_EQUAL_INT(ROCK || PAPER || SCISSORS, result); } +void test_rockPaperScissors_playerGetsBestOutOf3(void) +{ + /* arrange */ + //Hier die Werte eingeben + char result; + int roundsToWin = 2; + int playerWins = 2, computerWins = 1; + + /* act */ + //Die Funktion wird ausgeführt + result = wasGameWon(roundsToWin, playerWins, computerWins); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result); +} + #endif // TEST /*Testcases: From 4354c80aab2627e472d67fb7de057367d9f741f7 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Sun, 15 Jan 2023 17:09:27 +0100 Subject: [PATCH 09/23] computer wins game --- src/c/rockPaperScissors.c | 11 +++++++---- src/c/rockPaperScissors.h | 6 +++--- test/c/test_rockPaperScissors.c | 27 ++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 29b6ed5..40598d7 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -4,12 +4,12 @@ #include "rockPaperScissors.h" -char getComputerInput(){ +int getComputerInput(){ int input = rand() % 3; return input; } -char findWinner(int inputPlayer, int inputComputer){ +int findWinner(int inputPlayer, int inputComputer){ if (inputPlayer == inputComputer){ return NOWINNER; } @@ -27,11 +27,14 @@ char findWinner(int inputPlayer, int inputComputer){ } } -char wasGameWon(roundsToWin, playerWins, computerWins){ - char winner; +int wasGameWon(roundsToWin, playerWins, computerWins){ + int winner; if (playerWins == roundsToWin){ winner = PLAYERWINSGAME; } + else if (computerWins == roundsToWin){ + winner = COMPUTERWINSGAME; + } return winner; } diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 611fcf6..e3eb73f 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -20,8 +20,8 @@ enum gameWinner{ }; -char findWinner(int inputPlayer, int inputComputer); -char getComputerInput(); -char wasGameWon(roundsToWin, playerWins, computerWins); +int findWinner(int inputPlayer, int inputComputer); +int getComputerInput(); +int wasGameWon(roundsToWin, playerWins, computerWins); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index ac07ab9..eb74c62 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -17,7 +17,7 @@ void test_rockPaperScissors_sameResult(void) { /* arrange */ //Hier die Werte eingeben - char result; + int result; int inputPlayer = ROCK; int inputComputer = inputPlayer; @@ -34,7 +34,7 @@ void test_rockPaperScissors_differentResultsPlayerWins(void) { /* arrange */ //Hier die Werte eingeben - char result; + int result; int inputPlayer = ROCK; int inputComputer = SCISSORS; @@ -77,7 +77,7 @@ void test_rockPaperScissors_differentResultsPlayerLoses(void) { /* arrange */ //Hier die Werte eingeben - char result; + int result; int inputPlayer = ROCK; int inputComputer = PAPER; @@ -120,7 +120,7 @@ void test_rockPaperScissors_generateComputerInput(void) { /* arrange */ //Hier die Werte eingeben - char result; + int result; /* act */ //Die Funktion wird ausgeführt @@ -135,7 +135,7 @@ void test_rockPaperScissors_playerGetsBestOutOf3(void) { /* arrange */ //Hier die Werte eingeben - char result; + int result; int roundsToWin = 2; int playerWins = 2, computerWins = 1; @@ -148,6 +148,23 @@ void test_rockPaperScissors_playerGetsBestOutOf3(void) TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result); } +void test_rockPaperScissors_computerGetsBestOutOf3(void) +{ + /* arrange */ + //Hier die Werte eingeben + int result; + int roundsToWin = 2; + int playerWins = 1, computerWins = 2; + + /* act */ + //Die Funktion wird ausgeführt + result = wasGameWon(roundsToWin, playerWins, computerWins); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result); +} + #endif // TEST /*Testcases: From 6ef9c9e436dd4f963145197e9b5e8806b17f5dd5 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Sun, 15 Jan 2023 17:11:55 +0100 Subject: [PATCH 10/23] game was not won yet --- src/c/rockPaperScissors.c | 3 +++ test/c/test_rockPaperScissors.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 40598d7..4f482e4 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -35,6 +35,9 @@ int wasGameWon(roundsToWin, playerWins, computerWins){ else if (computerWins == roundsToWin){ winner = COMPUTERWINSGAME; } + else { + winner = NOTWONYET; + } return winner; } diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index eb74c62..49a6b99 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -165,6 +165,23 @@ void test_rockPaperScissors_computerGetsBestOutOf3(void) TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result); } +void test_rockPaperScissors_gameWasNotWon(void) +{ + /* arrange */ + //Hier die Werte eingeben + int result; + int roundsToWin = 2; + int playerWins = 1, computerWins = 1; + + /* act */ + //Die Funktion wird ausgeführt + result = wasGameWon(roundsToWin, playerWins, computerWins); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(NOTWONYET, result); +} + #endif // TEST /*Testcases: From d213df6de3a95eb9f938025f8d6d03bbfdb2741c Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 23 Jan 2023 10:05:42 +0100 Subject: [PATCH 11/23] Player input is valid --- src/c/rockPaperScissors.c | 8 ++++++++ src/c/rockPaperScissors.h | 5 ++++- test/c/test_rockPaperScissors.c | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 4f482e4..a8477e6 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "rockPaperScissors.h" @@ -41,3 +42,10 @@ int wasGameWon(roundsToWin, playerWins, computerWins){ return winner; } +bool validatePlayerInput(int playerInput){ + bool inputValid; + if (playerInput == ROCK || PAPER || SCISSORS){ + inputValid = true; + } + return inputValid; +} \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index e3eb73f..d6df3ec 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -1,6 +1,8 @@ #ifndef ROCKPAPERSCISSORS_H #define ROCKPAPERSCISSORS_H +#include + enum inputOptions{ ROCK, PAPER, @@ -22,6 +24,7 @@ enum gameWinner{ int findWinner(int inputPlayer, int inputComputer); int getComputerInput(); -int wasGameWon(roundsToWin, playerWins, computerWins); +int wasGameWon(int roundsToWin, int playerWins, int computerWins); +bool validatePlayerInput(int playerInput); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 49a6b99..69d6761 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -182,11 +182,25 @@ void test_rockPaperScissors_gameWasNotWon(void) TEST_ASSERT_EQUAL_INT(NOTWONYET, result); } +void test_rockPaperScissors_validPlayerInput(void) +{ + /* arrange */ + //Hier die Werte eingeben + bool result; + int playerInput = 1; + + /* act */ + //Die Funktion wird ausgeführt + result = validatePlayerInput(playerInput); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(true, result); +} + #endif // TEST /*Testcases: -rockPaperScissors_playerGetsBestOutOf3 -rockPaperScissors_computerGetsBestOutOf3 rockPaperScissors_outputResult rockPaperScissors_invalidInput */ \ No newline at end of file From 4bc6f7965ff937ca45403fc1650605e0e5c28a75 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 23 Jan 2023 10:10:57 +0100 Subject: [PATCH 12/23] player input is invalid --- src/c/rockPaperScissors.c | 5 ++++- test/c/test_rockPaperScissors.c | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index a8477e6..7a98a97 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -44,8 +44,11 @@ int wasGameWon(roundsToWin, playerWins, computerWins){ bool validatePlayerInput(int playerInput){ bool inputValid; - if (playerInput == ROCK || PAPER || SCISSORS){ + if (playerInput == ROCK || playerInput == PAPER || playerInput == SCISSORS){ inputValid = true; } + else { + inputValid = false; + } return inputValid; } \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 69d6761..5b492e4 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -187,7 +187,7 @@ void test_rockPaperScissors_validPlayerInput(void) /* arrange */ //Hier die Werte eingeben bool result; - int playerInput = 1; + int playerInput = 2; /* act */ //Die Funktion wird ausgeführt @@ -198,6 +198,22 @@ void test_rockPaperScissors_validPlayerInput(void) TEST_ASSERT_EQUAL_INT(true, result); } +void test_rockPaperScissors_invalidPlayerInput(void) +{ + /* arrange */ + //Hier die Werte eingeben + bool result; + int playerInput = 5; + + /* act */ + //Die Funktion wird ausgeführt + result = validatePlayerInput(playerInput); + + /* assert */ + //Vergleichen + TEST_ASSERT_EQUAL_INT(false, result); +} + #endif // TEST /*Testcases: From fea4ba17045318a1aca7c7549b7b06674e151619 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Mon, 23 Jan 2023 10:53:06 +0100 Subject: [PATCH 13/23] get player input --- src/c/rockPaperScissors.c | 14 +++++++++++++- src/c/rockPaperScissors.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 7a98a97..fa87c41 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -28,7 +28,7 @@ int findWinner(int inputPlayer, int inputComputer){ } } -int wasGameWon(roundsToWin, playerWins, computerWins){ +int wasGameWon(int roundsToWin, int playerWins, int computerWins){ int winner; if (playerWins == roundsToWin){ winner = PLAYERWINSGAME; @@ -51,4 +51,16 @@ bool validatePlayerInput(int playerInput){ inputValid = false; } return inputValid; +} + +int getPlayerInput(){ + bool inputValid = false; + int inputPlayer; + while(!inputValid){ + printf("Enter your choice:\n1 for Rock\n2 for Paper\n3 for Scissors\n"); + scanf("%d", &inputPlayer); + inputPlayer -= 1; + inputValid = validatePlayerInput(inputPlayer); + } + return inputPlayer; } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index d6df3ec..e935a05 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -26,5 +26,6 @@ int findWinner(int inputPlayer, int inputComputer); int getComputerInput(); int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); +int getPlayerInput(); #endif \ No newline at end of file From 01d870252fad583a49f9ab064f649f72cbea3500 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 11:59:44 +0100 Subject: [PATCH 14/23] play function --- src/c/rockPaperScissors.c | 24 ++++++++++++++++++++++++ src/c/rockPaperScissors.h | 1 + test/c/test_rockPaperScissors.c | 7 +------ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index fa87c41..bb65845 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -63,4 +63,28 @@ int getPlayerInput(){ inputValid = validatePlayerInput(inputPlayer); } return inputPlayer; +} + +int play(int rounds){ + int playerWins = 0, computerWins = 0; + int roundsToWin = (rounds/2)+1; + int computerInput, playerInput; + int roundwinner = NOWINNER; + int winner = NOTWONYET; + + printf("Lets play a game\n"); + while (winner == NOTWONYET){ + playerInput = getPlayerInput(); + computerInput = getComputerInput(); + roundwinner = findWinner(playerInput, computerInput); + if (roundwinner == PLAYERWINSROUND){ + playerWins += 1; + } + else if (roundwinner == COMPUTERWINSROUND){ + computerWins += 1; + } + winner = wasGameWon(roundsToWin, playerWins, computerWins); + printf("Something happened\n"); + } + printf("Someone won\n"); } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index e935a05..503e375 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -27,5 +27,6 @@ int getComputerInput(); int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); int getPlayerInput(); +int play(int rounds); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 5b492e4..d3708a2 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -214,9 +214,4 @@ void test_rockPaperScissors_invalidPlayerInput(void) TEST_ASSERT_EQUAL_INT(false, result); } -#endif // TEST - -/*Testcases: -rockPaperScissors_outputResult -rockPaperScissors_invalidInput -*/ \ No newline at end of file +#endif // TEST \ No newline at end of file From c00b4b86f92552bb97cd4eb4fd21313ce5b5ec7d Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 12:10:23 +0100 Subject: [PATCH 15/23] refactoring: naming improved --- src/c/rockPaperScissors.c | 109 ++++++++++++++++++++------------------ src/c/rockPaperScissors.h | 10 ++-- 2 files changed, 62 insertions(+), 57 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index bb65845..25c0f0f 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -5,22 +5,73 @@ #include "rockPaperScissors.h" +int playRockPaperScissors(int rounds){ + int playerWins = 0, computerWins = 0; + int roundsToWin = (rounds/2)+1; + int computerInput, playerInput; + int roundwinner = NOWINNER; + int winner = NOTWONYET; + + printf("Lets play a game\n"); + while (winner == NOTWONYET){ + playerInput = getPlayerInput(); + computerInput = getComputerInput(); + roundwinner = findWinner(playerInput, computerInput); + if (roundwinner == PLAYERWINSROUND){ + playerWins += 1; + } + else if (roundwinner == COMPUTERWINSROUND){ + computerWins += 1; + } + winner = wasGameWon(roundsToWin, playerWins, computerWins); + printf("Something happened\n"); + } + printf("Someone won\n"); +} + + +int getPlayerInput(){ + bool inputValid = false; + int playerInput; + while(!inputValid){ + printf("Enter your choice:\n1 for Rock\n2 for Paper\n3 for Scissors\n"); + scanf("%d", &playerInput); + playerInput -= 1; + inputValid = validatePlayerInput(playerInput); + } + return playerInput; +} + + +bool validatePlayerInput(int playerInput){ + bool inputValid; + if (playerInput == ROCK || playerInput == PAPER || playerInput == SCISSORS){ + inputValid = true; + } + else { + inputValid = false; + } + return inputValid; +} + + int getComputerInput(){ int input = rand() % 3; return input; } -int findWinner(int inputPlayer, int inputComputer){ - if (inputPlayer == inputComputer){ + +int findWinner(int playerInput, int computerInput){ + if (playerInput == computerInput){ return NOWINNER; } - else if (inputPlayer == ROCK && inputComputer == SCISSORS) { + else if (playerInput == ROCK && computerInput == SCISSORS) { return PLAYERWINSROUND; } - else if (inputPlayer == PAPER && inputComputer == ROCK) { + else if (playerInput == PAPER && computerInput == ROCK) { return PLAYERWINSROUND; } - else if (inputPlayer == SCISSORS && inputComputer == PAPER) { + else if (playerInput == SCISSORS && computerInput == PAPER) { return PLAYERWINSROUND; } else { @@ -28,6 +79,7 @@ int findWinner(int inputPlayer, int inputComputer){ } } + int wasGameWon(int roundsToWin, int playerWins, int computerWins){ int winner; if (playerWins == roundsToWin){ @@ -40,51 +92,4 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins){ winner = NOTWONYET; } return winner; -} - -bool validatePlayerInput(int playerInput){ - bool inputValid; - if (playerInput == ROCK || playerInput == PAPER || playerInput == SCISSORS){ - inputValid = true; - } - else { - inputValid = false; - } - return inputValid; -} - -int getPlayerInput(){ - bool inputValid = false; - int inputPlayer; - while(!inputValid){ - printf("Enter your choice:\n1 for Rock\n2 for Paper\n3 for Scissors\n"); - scanf("%d", &inputPlayer); - inputPlayer -= 1; - inputValid = validatePlayerInput(inputPlayer); - } - return inputPlayer; -} - -int play(int rounds){ - int playerWins = 0, computerWins = 0; - int roundsToWin = (rounds/2)+1; - int computerInput, playerInput; - int roundwinner = NOWINNER; - int winner = NOTWONYET; - - printf("Lets play a game\n"); - while (winner == NOTWONYET){ - playerInput = getPlayerInput(); - computerInput = getComputerInput(); - roundwinner = findWinner(playerInput, computerInput); - if (roundwinner == PLAYERWINSROUND){ - playerWins += 1; - } - else if (roundwinner == COMPUTERWINSROUND){ - computerWins += 1; - } - winner = wasGameWon(roundsToWin, playerWins, computerWins); - printf("Something happened\n"); - } - printf("Someone won\n"); } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 503e375..3c655e2 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -3,30 +3,30 @@ #include -enum inputOptions{ +enum InputOptions{ ROCK, PAPER, SCISSORS }; -enum roundWinner{ +enum RoundWinner{ PLAYERWINSROUND, COMPUTERWINSROUND, NOWINNER }; -enum gameWinner{ +enum GameWinner{ PLAYERWINSGAME, COMPUTERWINSGAME, NOTWONYET }; -int findWinner(int inputPlayer, int inputComputer); +int findWinner(int playerInput, int computerInput); int getComputerInput(); int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); int getPlayerInput(); -int play(int rounds); +int playRockPaperScissors(int rounds); #endif \ No newline at end of file From 37c2f33dbd2f8f861e0381958ad407e25d4def6d Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 12:22:23 +0100 Subject: [PATCH 16/23] refactoring: made print prompt a function + text --- src/c/rockPaperScissors.c | 7 ++++++- src/c/rockPaperScissors.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 25c0f0f..c5105cd 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -12,7 +12,7 @@ int playRockPaperScissors(int rounds){ int roundwinner = NOWINNER; int winner = NOTWONYET; - printf("Lets play a game\n"); + printPrompt(roundsToWin); while (winner == NOTWONYET){ playerInput = getPlayerInput(); computerInput = getComputerInput(); @@ -92,4 +92,9 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins){ winner = NOTWONYET; } return winner; +} + + +void printPrompt(int roundsToWin){ + printf("Hello NAME.\nLet us play a game, shall we? I assume you are familiar with Rock-Paper-Scissors?\nIf not, here are the rules:\nThe first one to win %d rounds wins the game. Rock beats scissors, scissors beats paper and paper beats rock.\nThey are quite simple, even you should understand. Got it?\n", roundsToWin); } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 3c655e2..7d9c11f 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -28,5 +28,6 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); int getPlayerInput(); int playRockPaperScissors(int rounds); +void printPrompt(int roundsToWin); #endif \ No newline at end of file From 852c84e0a3e6d9fdeaafbd573bb492e0092e1088 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 12:32:04 +0100 Subject: [PATCH 17/23] refactoring: improved testnames + removed comments --- test/c/test_rockPaperScissors.c | 71 ++++++++++----------------------- 1 file changed, 22 insertions(+), 49 deletions(-) diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index d3708a2..0336bde 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -1,6 +1,5 @@ #ifdef TEST #include "unity.h" -//in example.h wird die Funktion deklariert #include "rockPaperScissors.h" //Vor- bzw. Nachbereitung @@ -12,205 +11,179 @@ void tearDown(void) { } -//Hier läuft der Test + void test_rockPaperScissors_sameResult(void) { /* arrange */ - //Hier die Werte eingeben int result; int inputPlayer = ROCK; int inputComputer = inputPlayer; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(NOWINNER, result); } -void test_rockPaperScissors_differentResultsPlayerWins(void) + +void test_rockPaperScissors_playerWinsRound(void) { /* arrange */ - //Hier die Werte eingeben int result; + int inputPlayer = ROCK; int inputComputer = SCISSORS; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); - /* arrange */ - //Hier die Werte eingeben + + /* arrange */ inputPlayer = PAPER; inputComputer = ROCK; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); - /* arrange */ - //Hier die Werte eingeben + + /* arrange */ inputPlayer = SCISSORS; inputComputer = PAPER; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); } -void test_rockPaperScissors_differentResultsPlayerLoses(void) + +void test_rockPaperScissors_computerWinsRound(void) { /* arrange */ - //Hier die Werte eingeben int result; int inputPlayer = ROCK; int inputComputer = PAPER; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); - /* arrange */ - //Hier die Werte eingeben + + /* arrange */ inputPlayer = PAPER; inputComputer = SCISSORS; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); - /* arrange */ - //Hier die Werte eingeben + + /* arrange */ inputPlayer = SCISSORS; inputComputer = ROCK; /* act */ - //Die Funktion wird ausgeführt result = findWinner(inputPlayer, inputComputer); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); } + void test_rockPaperScissors_generateComputerInput(void) { /* arrange */ - //Hier die Werte eingeben int result; /* act */ - //Die Funktion wird ausgeführt result = getComputerInput(); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(ROCK || PAPER || SCISSORS, result); } -void test_rockPaperScissors_playerGetsBestOutOf3(void) + +void test_rockPaperScissors_playerWinsGame(void) { /* arrange */ - //Hier die Werte eingeben int result; int roundsToWin = 2; int playerWins = 2, computerWins = 1; /* act */ - //Die Funktion wird ausgeführt result = wasGameWon(roundsToWin, playerWins, computerWins); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result); } -void test_rockPaperScissors_computerGetsBestOutOf3(void) + +void test_rockPaperScissors_computerWinsGame(void) { /* arrange */ - //Hier die Werte eingeben int result; int roundsToWin = 2; int playerWins = 1, computerWins = 2; /* act */ - //Die Funktion wird ausgeführt result = wasGameWon(roundsToWin, playerWins, computerWins); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result); } + void test_rockPaperScissors_gameWasNotWon(void) { /* arrange */ - //Hier die Werte eingeben int result; int roundsToWin = 2; int playerWins = 1, computerWins = 1; /* act */ - //Die Funktion wird ausgeführt result = wasGameWon(roundsToWin, playerWins, computerWins); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(NOTWONYET, result); } + void test_rockPaperScissors_validPlayerInput(void) { /* arrange */ - //Hier die Werte eingeben bool result; int playerInput = 2; /* act */ - //Die Funktion wird ausgeführt result = validatePlayerInput(playerInput); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(true, result); } + void test_rockPaperScissors_invalidPlayerInput(void) { /* arrange */ - //Hier die Werte eingeben bool result; int playerInput = 5; /* act */ - //Die Funktion wird ausgeführt result = validatePlayerInput(playerInput); /* assert */ - //Vergleichen TEST_ASSERT_EQUAL_INT(false, result); } From ece1a547ef0a5eefee5dd55bdb7252d5f0fdad6a Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 12:58:32 +0100 Subject: [PATCH 18/23] print round results --- src/c/rockPaperScissors.c | 25 +++++++++++++++++++++++++ src/c/rockPaperScissors.h | 1 + 2 files changed, 26 insertions(+) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index c5105cd..5309aa4 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -97,4 +97,29 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins){ void printPrompt(int roundsToWin){ printf("Hello NAME.\nLet us play a game, shall we? I assume you are familiar with Rock-Paper-Scissors?\nIf not, here are the rules:\nThe first one to win %d rounds wins the game. Rock beats scissors, scissors beats paper and paper beats rock.\nThey are quite simple, even you should understand. Got it?\n", roundsToWin); +} + + +void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins){ + switch (playerInput){ + case ROCK: printf("So you chose rock.\n"); break; + case PAPER: printf("So you chose paper.\n"); break; + case SCISSORS: printf("So you chose scissors.\n"); break; + } + + switch (roundWinner) + { + case PLAYERWINSROUND: printf("Seems you win this round, I chose "); break; + case COMPUTERWINSROUND: printf("Too bad for you. I win this one, I chose "); break; + case NOWINNER: printf("Looks like it's a draw, I also chose "); break; + } + + switch (computerInput) + { + case ROCK: printf("rock.\n"); break; + case PAPER: printf("paper.\n"); break; + case SCISSORS: printf("scissors.\n"); break; + } + + printf("With this, you are at %d wins and I am at %d.\n", playerWins, computerWins); } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 7d9c11f..ba5b38f 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -29,5 +29,6 @@ bool validatePlayerInput(int playerInput); int getPlayerInput(); int playRockPaperScissors(int rounds); void printPrompt(int roundsToWin); +void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); #endif \ No newline at end of file From 6faa5dfbffd4786f0ef9249562182728ed796ae8 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 13:07:11 +0100 Subject: [PATCH 19/23] refactoring: added print winner + text --- src/c/rockPaperScissors.c | 13 +++++++++++-- src/c/rockPaperScissors.h | 2 -- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 5309aa4..14fb1cf 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -24,9 +24,9 @@ int playRockPaperScissors(int rounds){ computerWins += 1; } winner = wasGameWon(roundsToWin, playerWins, computerWins); - printf("Something happened\n"); + printResult(playerInput, computerInput, roundwinner, playerWins, computerWins); } - printf("Someone won\n"); + printWinner(winner); } @@ -122,4 +122,13 @@ void printResult(int playerInput, int computerInput, int roundWinner, int player } printf("With this, you are at %d wins and I am at %d.\n", playerWins, computerWins); +} + +void printWinner(int winner){ + if (winner == PLAYERWINSGAME){ + printf("Damn you, you beat me! You actually won! Oh well, I stand by my word. You may pass.\n"); + } + else if (winner == COMPUTERWINSGAME){ + printf("Oh poor you, seems like I won. You're gomma have to try again. Do better next time.\n"); + } } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index ba5b38f..3c655e2 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -28,7 +28,5 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); int getPlayerInput(); int playRockPaperScissors(int rounds); -void printPrompt(int roundsToWin); -void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); #endif \ No newline at end of file From c166123a0048eced96d43669f649506a578e15be Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 13:36:18 +0100 Subject: [PATCH 20/23] improved random numbers --- src/c/rockPaperScissors.c | 2 ++ src/c/rockPaperScissors.h | 3 +++ test/c/test_rockPaperScissors.c | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 14fb1cf..3de9aaf 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "rockPaperScissors.h" @@ -56,6 +57,7 @@ bool validatePlayerInput(int playerInput){ int getComputerInput(){ + srand(rand() % 300); int input = rand() % 3; return input; } diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 3c655e2..c35397c 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -28,5 +28,8 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins); bool validatePlayerInput(int playerInput); int getPlayerInput(); int playRockPaperScissors(int rounds); +void printPrompt(int roundsToWin); +void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); +void printWinner(int winner); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index 0336bde..b6ba580 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -31,7 +31,7 @@ void test_rockPaperScissors_playerWinsRound(void) { /* arrange */ int result; - + int inputPlayer = ROCK; int inputComputer = SCISSORS; @@ -111,7 +111,7 @@ void test_rockPaperScissors_generateComputerInput(void) result = getComputerInput(); /* assert */ - TEST_ASSERT_EQUAL_INT(ROCK || PAPER || SCISSORS, result); + TEST_ASSERT_INT_WITHIN(1, 1, result); } From 2914b8d98562035546cfe58fcd507bb11163afdc Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 25 Jan 2023 10:21:49 +0100 Subject: [PATCH 21/23] refactoring: extracted runGame function --- src/c/rockPaperScissors.c | 36 ++++++++++++++++++++++-------------- src/c/rockPaperScissors.h | 1 + 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 3de9aaf..9b314e0 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -7,27 +7,34 @@ #include "rockPaperScissors.h" int playRockPaperScissors(int rounds){ - int playerWins = 0, computerWins = 0; int roundsToWin = (rounds/2)+1; - int computerInput, playerInput; - int roundwinner = NOWINNER; int winner = NOTWONYET; + int playerWins = 0, computerWins = 0, *playerPt = &playerWins, *computerPt = &computerWins; printPrompt(roundsToWin); while (winner == NOTWONYET){ - playerInput = getPlayerInput(); - computerInput = getComputerInput(); - roundwinner = findWinner(playerInput, computerInput); - if (roundwinner == PLAYERWINSROUND){ - playerWins += 1; - } - else if (roundwinner == COMPUTERWINSROUND){ - computerWins += 1; - } + runGame(playerPt, computerPt); winner = wasGameWon(roundsToWin, playerWins, computerWins); - printResult(playerInput, computerInput, roundwinner, playerWins, computerWins); } printWinner(winner); + return winner; +} + + +void runGame(int *playerWins, int *computerWins){ + int computerInput, playerInput; + int roundwinner = NOWINNER; + + playerInput = getPlayerInput(); + computerInput = getComputerInput(); + roundwinner = findWinner(playerInput, computerInput); + if (roundwinner == PLAYERWINSROUND){ + *playerWins += 1; + } + else if (roundwinner == COMPUTERWINSROUND){ + *computerWins += 1; + } + printResult(playerInput, computerInput, roundwinner, *playerWins, *computerWins); } @@ -57,7 +64,7 @@ bool validatePlayerInput(int playerInput){ int getComputerInput(){ - srand(rand() % 300); + srand(time(0)); int input = rand() % 3; return input; } @@ -126,6 +133,7 @@ void printResult(int playerInput, int computerInput, int roundWinner, int player printf("With this, you are at %d wins and I am at %d.\n", playerWins, computerWins); } + void printWinner(int winner){ if (winner == PLAYERWINSGAME){ printf("Damn you, you beat me! You actually won! Oh well, I stand by my word. You may pass.\n"); diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index c35397c..fd6333f 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -31,5 +31,6 @@ int playRockPaperScissors(int rounds); void printPrompt(int roundsToWin); void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); void printWinner(int winner); +void runGame(int *playerWins, int *computerWins); #endif \ No newline at end of file From fe59a589261a9febec3913b62fa19f9bb6618da7 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 25 Jan 2023 10:31:25 +0100 Subject: [PATCH 22/23] refactoring: extracted setScore function --- src/c/rockPaperScissors.c | 9 +++++++-- src/c/rockPaperScissors.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 9b314e0..1da33e6 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -24,17 +24,22 @@ int playRockPaperScissors(int rounds){ void runGame(int *playerWins, int *computerWins){ int computerInput, playerInput; int roundwinner = NOWINNER; - + playerInput = getPlayerInput(); computerInput = getComputerInput(); roundwinner = findWinner(playerInput, computerInput); + setScore(roundwinner, playerWins, computerWins); + printResult(playerInput, computerInput, roundwinner, *playerWins, *computerWins); +} + + +void setScore(int roundwinner, int *playerWins, int *computerWins){ if (roundwinner == PLAYERWINSROUND){ *playerWins += 1; } else if (roundwinner == COMPUTERWINSROUND){ *computerWins += 1; } - printResult(playerInput, computerInput, roundwinner, *playerWins, *computerWins); } diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index fd6333f..224a20d 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -32,5 +32,6 @@ void printPrompt(int roundsToWin); void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); void printWinner(int winner); void runGame(int *playerWins, int *computerWins); +void setScore(int roundwinner, int *playerWins, int *computerWins); #endif \ No newline at end of file From 7f8d4445d881cb79d75ad5ad9d6595a7f8cf98a5 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 25 Jan 2023 10:50:33 +0100 Subject: [PATCH 23/23] refactoring: improved consistency and clarity --- src/c/rockPaperScissors.c | 50 ++++++------ src/c/rockPaperScissors.h | 18 ++--- test/c/test_rockPaperScissors.c | 134 ++++++++++++++++---------------- 3 files changed, 101 insertions(+), 101 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 1da33e6..c6123fa 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -21,6 +21,11 @@ int playRockPaperScissors(int rounds){ } +void printPrompt(int roundsToWin){ + printf("Hello NAME.\nLet us play a game, shall we? I assume you are familiar with Rock-Paper-Scissors?\nIf not, here are the rules:\nThe first one to win %d rounds wins the game. Rock beats scissors, scissors beats paper and paper beats rock.\nThey are quite simple, even you should understand. Got it?\n", roundsToWin); +} + + void runGame(int *playerWins, int *computerWins){ int computerInput, playerInput; int roundwinner = NOWINNER; @@ -33,16 +38,6 @@ void runGame(int *playerWins, int *computerWins){ } -void setScore(int roundwinner, int *playerWins, int *computerWins){ - if (roundwinner == PLAYERWINSROUND){ - *playerWins += 1; - } - else if (roundwinner == COMPUTERWINSROUND){ - *computerWins += 1; - } -} - - int getPlayerInput(){ bool inputValid = false; int playerInput; @@ -94,23 +89,13 @@ int findWinner(int playerInput, int computerInput){ } -int wasGameWon(int roundsToWin, int playerWins, int computerWins){ - int winner; - if (playerWins == roundsToWin){ - winner = PLAYERWINSGAME; - } - else if (computerWins == roundsToWin){ - winner = COMPUTERWINSGAME; +void setScore(int roundwinner, int *playerWins, int *computerWins){ + if (roundwinner == PLAYERWINSROUND){ + *playerWins += 1; } - else { - winner = NOTWONYET; + else if (roundwinner == COMPUTERWINSROUND){ + *computerWins += 1; } - return winner; -} - - -void printPrompt(int roundsToWin){ - printf("Hello NAME.\nLet us play a game, shall we? I assume you are familiar with Rock-Paper-Scissors?\nIf not, here are the rules:\nThe first one to win %d rounds wins the game. Rock beats scissors, scissors beats paper and paper beats rock.\nThey are quite simple, even you should understand. Got it?\n", roundsToWin); } @@ -139,6 +124,21 @@ void printResult(int playerInput, int computerInput, int roundWinner, int player } +int wasGameWon(int roundsToWin, int playerWins, int computerWins){ + int winner; + if (playerWins == roundsToWin){ + winner = PLAYERWINSGAME; + } + else if (computerWins == roundsToWin){ + winner = COMPUTERWINSGAME; + } + else { + winner = NOTWONYET; + } + return winner; +} + + void printWinner(int winner){ if (winner == PLAYERWINSGAME){ printf("Damn you, you beat me! You actually won! Oh well, I stand by my word. You may pass.\n"); diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 224a20d..092d2e6 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -10,28 +10,28 @@ enum InputOptions{ }; enum RoundWinner{ - PLAYERWINSROUND, + PLAYERWINSROUND = 3, COMPUTERWINSROUND, NOWINNER }; enum GameWinner{ - PLAYERWINSGAME, + PLAYERWINSGAME = 6, COMPUTERWINSGAME, NOTWONYET }; -int findWinner(int playerInput, int computerInput); -int getComputerInput(); -int wasGameWon(int roundsToWin, int playerWins, int computerWins); -bool validatePlayerInput(int playerInput); -int getPlayerInput(); int playRockPaperScissors(int rounds); void printPrompt(int roundsToWin); -void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); -void printWinner(int winner); void runGame(int *playerWins, int *computerWins); +int getPlayerInput(); +bool validatePlayerInput(int playerInput); +int getComputerInput(); +int findWinner(int playerInput, int computerInput); void setScore(int roundwinner, int *playerWins, int *computerWins); +void printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins); +int wasGameWon(int roundsToWin, int playerWins, int computerWins); +void printWinner(int winner); #endif \ No newline at end of file diff --git a/test/c/test_rockPaperScissors.c b/test/c/test_rockPaperScissors.c index b6ba580..cada978 100644 --- a/test/c/test_rockPaperScissors.c +++ b/test/c/test_rockPaperScissors.c @@ -12,136 +12,134 @@ void tearDown(void) } -void test_rockPaperScissors_sameResult(void) +void test_rockPaperScissors_validPlayerInput(void) { /* arrange */ - int result; - int inputPlayer = ROCK; - int inputComputer = inputPlayer; + bool result; + int playerInput = 2; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = validatePlayerInput(playerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(NOWINNER, result); + TEST_ASSERT_EQUAL_INT(true, result); } -void test_rockPaperScissors_playerWinsRound(void) +void test_rockPaperScissors_invalidPlayerInput(void) { /* arrange */ - int result; - - int inputPlayer = ROCK; - int inputComputer = SCISSORS; + bool result; + int playerInput = 5; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = validatePlayerInput(playerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); + TEST_ASSERT_EQUAL_INT(false, result); +} +void test_rockPaperScissors_generateComputerInput(void) +{ /* arrange */ - inputPlayer = PAPER; - inputComputer = ROCK; + int result; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = getComputerInput(); /* assert */ - TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); + TEST_ASSERT_INT_WITHIN(1, 1, result); +} - + +void test_rockPaperScissors_sameResult(void) +{ /* arrange */ - inputPlayer = SCISSORS; - inputComputer = PAPER; + int result; + int playerInput = ROCK; + int computerInput = playerInput; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); + TEST_ASSERT_EQUAL_INT(NOWINNER, result); } -void test_rockPaperScissors_computerWinsRound(void) +void test_rockPaperScissors_playerWinsRound(void) { /* arrange */ int result; - int inputPlayer = ROCK; - int inputComputer = PAPER; + + int playerInput = ROCK; + int computerInput = SCISSORS; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); + - /* arrange */ - inputPlayer = PAPER; - inputComputer = SCISSORS; + playerInput = PAPER; + computerInput = ROCK; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); /* arrange */ - inputPlayer = SCISSORS; - inputComputer = ROCK; + playerInput = SCISSORS; + computerInput = PAPER; /* act */ - result = findWinner(inputPlayer, inputComputer); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result); } -void test_rockPaperScissors_generateComputerInput(void) +void test_rockPaperScissors_computerWinsRound(void) { /* arrange */ int result; + int playerInput = ROCK; + int computerInput = PAPER; /* act */ - result = getComputerInput(); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_INT_WITHIN(1, 1, result); -} - + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); -void test_rockPaperScissors_playerWinsGame(void) -{ + /* arrange */ - int result; - int roundsToWin = 2; - int playerWins = 2, computerWins = 1; + playerInput = PAPER; + computerInput = SCISSORS; /* act */ - result = wasGameWon(roundsToWin, playerWins, computerWins); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result); -} - + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); -void test_rockPaperScissors_computerWinsGame(void) -{ + /* arrange */ - int result; - int roundsToWin = 2; - int playerWins = 1, computerWins = 2; + playerInput = SCISSORS; + computerInput = ROCK; /* act */ - result = wasGameWon(roundsToWin, playerWins, computerWins); + result = findWinner(playerInput, computerInput); /* assert */ - TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result); + TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result); } @@ -160,31 +158,33 @@ void test_rockPaperScissors_gameWasNotWon(void) } -void test_rockPaperScissors_validPlayerInput(void) +void test_rockPaperScissors_playerWinsGame(void) { /* arrange */ - bool result; - int playerInput = 2; + int result; + int roundsToWin = 2; + int playerWins = 2, computerWins = 1; /* act */ - result = validatePlayerInput(playerInput); + result = wasGameWon(roundsToWin, playerWins, computerWins); /* assert */ - TEST_ASSERT_EQUAL_INT(true, result); + TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result); } -void test_rockPaperScissors_invalidPlayerInput(void) +void test_rockPaperScissors_computerWinsGame(void) { /* arrange */ - bool result; - int playerInput = 5; + int result; + int roundsToWin = 2; + int playerWins = 1, computerWins = 2; /* act */ - result = validatePlayerInput(playerInput); + result = wasGameWon(roundsToWin, playerWins, computerWins); /* assert */ - TEST_ASSERT_EQUAL_INT(false, result); + TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result); } #endif // TEST \ No newline at end of file