Browse Source

refactoring: improved consistency and clarity

remotes/origin/SchereSteinPapier
Aimee Reincke 2 years ago
parent
commit
7f8d4445d8
  1. 50
      src/c/rockPaperScissors.c
  2. 18
      src/c/rockPaperScissors.h
  3. 134
      test/c/test_rockPaperScissors.c

50
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){ void runGame(int *playerWins, int *computerWins){
int computerInput, playerInput; int computerInput, playerInput;
int roundwinner = NOWINNER; 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(){ int getPlayerInput(){
bool inputValid = false; bool inputValid = false;
int playerInput; 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){ void printWinner(int winner){
if (winner == PLAYERWINSGAME){ if (winner == PLAYERWINSGAME){
printf("Damn you, you beat me! You actually won! Oh well, I stand by my word. You may pass.\n"); printf("Damn you, you beat me! You actually won! Oh well, I stand by my word. You may pass.\n");

18
src/c/rockPaperScissors.h

@ -10,28 +10,28 @@ enum InputOptions{
}; };
enum RoundWinner{ enum RoundWinner{
PLAYERWINSROUND,
PLAYERWINSROUND = 3,
COMPUTERWINSROUND, COMPUTERWINSROUND,
NOWINNER NOWINNER
}; };
enum GameWinner{ enum GameWinner{
PLAYERWINSGAME,
PLAYERWINSGAME = 6,
COMPUTERWINSGAME, COMPUTERWINSGAME,
NOTWONYET 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); int playRockPaperScissors(int rounds);
void printPrompt(int roundsToWin); 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 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 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 #endif

134
test/c/test_rockPaperScissors.c

@ -12,136 +12,134 @@ void tearDown(void)
} }
void test_rockPaperScissors_sameResult(void)
void test_rockPaperScissors_validPlayerInput(void)
{ {
/* arrange */ /* arrange */
int result;
int inputPlayer = ROCK;
int inputComputer = inputPlayer;
bool result;
int playerInput = 2;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = validatePlayerInput(playerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(NOWINNER, result);
TEST_ASSERT_EQUAL_INT(true, result);
} }
void test_rockPaperScissors_playerWinsRound(void)
void test_rockPaperScissors_invalidPlayerInput(void)
{ {
/* arrange */ /* arrange */
int result;
int inputPlayer = ROCK;
int inputComputer = SCISSORS;
bool result;
int playerInput = 5;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = validatePlayerInput(playerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
TEST_ASSERT_EQUAL_INT(false, result);
}
void test_rockPaperScissors_generateComputerInput(void)
{
/* arrange */ /* arrange */
inputPlayer = PAPER;
inputComputer = ROCK;
int result;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = getComputerInput();
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
TEST_ASSERT_INT_WITHIN(1, 1, result);
}
void test_rockPaperScissors_sameResult(void)
{
/* arrange */ /* arrange */
inputPlayer = SCISSORS;
inputComputer = PAPER;
int result;
int playerInput = ROCK;
int computerInput = playerInput;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
TEST_ASSERT_EQUAL_INT(NOWINNER, result);
} }
void test_rockPaperScissors_computerWinsRound(void)
void test_rockPaperScissors_playerWinsRound(void)
{ {
/* arrange */ /* arrange */
int result; int result;
int inputPlayer = ROCK;
int inputComputer = PAPER;
int playerInput = ROCK;
int computerInput = SCISSORS;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result);
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
/* arrange */ /* arrange */
inputPlayer = PAPER;
inputComputer = SCISSORS;
playerInput = PAPER;
computerInput = ROCK;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result);
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
/* arrange */ /* arrange */
inputPlayer = SCISSORS;
inputComputer = ROCK;
playerInput = SCISSORS;
computerInput = PAPER;
/* act */ /* act */
result = findWinner(inputPlayer, inputComputer);
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result);
TEST_ASSERT_EQUAL_INT(PLAYERWINSROUND, result);
} }
void test_rockPaperScissors_generateComputerInput(void)
void test_rockPaperScissors_computerWinsRound(void)
{ {
/* arrange */ /* arrange */
int result; int result;
int playerInput = ROCK;
int computerInput = PAPER;
/* act */ /* act */
result = getComputerInput();
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_INT_WITHIN(1, 1, result);
}
TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result);
void test_rockPaperScissors_playerWinsGame(void)
{
/* arrange */ /* arrange */
int result;
int roundsToWin = 2;
int playerWins = 2, computerWins = 1;
playerInput = PAPER;
computerInput = SCISSORS;
/* act */ /* act */
result = wasGameWon(roundsToWin, playerWins, computerWins);
result = findWinner(playerInput, computerInput);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result);
}
TEST_ASSERT_EQUAL_INT(COMPUTERWINSROUND, result);
void test_rockPaperScissors_computerWinsGame(void)
{
/* arrange */ /* arrange */
int result;
int roundsToWin = 2;
int playerWins = 1, computerWins = 2;
playerInput = SCISSORS;
computerInput = ROCK;
/* act */ /* act */
result = wasGameWon(roundsToWin, playerWins, computerWins);
result = findWinner(playerInput, computerInput);
/* assert */ /* 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 */ /* arrange */
bool result;
int playerInput = 2;
int result;
int roundsToWin = 2;
int playerWins = 2, computerWins = 1;
/* act */ /* act */
result = validatePlayerInput(playerInput);
result = wasGameWon(roundsToWin, playerWins, computerWins);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(true, result);
TEST_ASSERT_EQUAL_INT(PLAYERWINSGAME, result);
} }
void test_rockPaperScissors_invalidPlayerInput(void)
void test_rockPaperScissors_computerWinsGame(void)
{ {
/* arrange */ /* arrange */
bool result;
int playerInput = 5;
int result;
int roundsToWin = 2;
int playerWins = 1, computerWins = 2;
/* act */ /* act */
result = validatePlayerInput(playerInput);
result = wasGameWon(roundsToWin, playerWins, computerWins);
/* assert */ /* assert */
TEST_ASSERT_EQUAL_INT(false, result);
TEST_ASSERT_EQUAL_INT(COMPUTERWINSGAME, result);
} }
#endif // TEST #endif // TEST
Loading…
Cancel
Save