From 7f8d4445d881cb79d75ad5ad9d6595a7f8cf98a5 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Wed, 25 Jan 2023 10:50:33 +0100 Subject: [PATCH] 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