From c00b4b86f92552bb97cd4eb4fd21313ce5b5ec7d Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Tue, 24 Jan 2023 12:10:23 +0100 Subject: [PATCH] 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