Browse Source

refactoring: extracted runGame function

remotes/origin/SchereSteinPapier
Aimee Reincke 2 years ago
parent
commit
2914b8d985
  1. 36
      src/c/rockPaperScissors.c
  2. 1
      src/c/rockPaperScissors.h

36
src/c/rockPaperScissors.c

@ -7,27 +7,34 @@
#include "rockPaperScissors.h" #include "rockPaperScissors.h"
int playRockPaperScissors(int rounds){ int playRockPaperScissors(int rounds){
int playerWins = 0, computerWins = 0;
int roundsToWin = (rounds/2)+1; int roundsToWin = (rounds/2)+1;
int computerInput, playerInput;
int roundwinner = NOWINNER;
int winner = NOTWONYET; int winner = NOTWONYET;
int playerWins = 0, computerWins = 0, *playerPt = &playerWins, *computerPt = &computerWins;
printPrompt(roundsToWin); printPrompt(roundsToWin);
while (winner == NOTWONYET){ 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); winner = wasGameWon(roundsToWin, playerWins, computerWins);
printResult(playerInput, computerInput, roundwinner, playerWins, computerWins);
} }
printWinner(winner); 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(){ int getComputerInput(){
srand(rand() % 300);
srand(time(0));
int input = rand() % 3; int input = rand() % 3;
return input; 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); printf("With this, you are at %d wins and I am at %d.\n", playerWins, computerWins);
} }
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");

1
src/c/rockPaperScissors.h

@ -31,5 +31,6 @@ 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 printResult(int playerInput, int computerInput, int roundWinner, int playerWins, int computerWins);
void printWinner(int winner); void printWinner(int winner);
void runGame(int *playerWins, int *computerWins);
#endif #endif
Loading…
Cancel
Save