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