Browse Source

refactoring: naming improved

remotes/origin/SchereSteinPapier
Aimee Reincke 2 years ago
parent
commit
c00b4b86f9
  1. 109
      src/c/rockPaperScissors.c
  2. 10
      src/c/rockPaperScissors.h

109
src/c/rockPaperScissors.c

@ -5,22 +5,73 @@
#include "rockPaperScissors.h" #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 getComputerInput(){
int input = rand() % 3; int input = rand() % 3;
return input; return input;
} }
int findWinner(int inputPlayer, int inputComputer){
if (inputPlayer == inputComputer){
int findWinner(int playerInput, int computerInput){
if (playerInput == computerInput){
return NOWINNER; return NOWINNER;
} }
else if (inputPlayer == ROCK && inputComputer == SCISSORS) {
else if (playerInput == ROCK && computerInput == SCISSORS) {
return PLAYERWINSROUND; return PLAYERWINSROUND;
} }
else if (inputPlayer == PAPER && inputComputer == ROCK) {
else if (playerInput == PAPER && computerInput == ROCK) {
return PLAYERWINSROUND; return PLAYERWINSROUND;
} }
else if (inputPlayer == SCISSORS && inputComputer == PAPER) {
else if (playerInput == SCISSORS && computerInput == PAPER) {
return PLAYERWINSROUND; return PLAYERWINSROUND;
} }
else { else {
@ -28,6 +79,7 @@ int findWinner(int inputPlayer, int inputComputer){
} }
} }
int wasGameWon(int roundsToWin, int playerWins, int computerWins){ int wasGameWon(int roundsToWin, int playerWins, int computerWins){
int winner; int winner;
if (playerWins == roundsToWin){ if (playerWins == roundsToWin){
@ -40,51 +92,4 @@ int wasGameWon(int roundsToWin, int playerWins, int computerWins){
winner = NOTWONYET; winner = NOTWONYET;
} }
return winner; 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");
} }

10
src/c/rockPaperScissors.h

@ -3,30 +3,30 @@
#include <stdbool.h> #include <stdbool.h>
enum inputOptions{
enum InputOptions{
ROCK, ROCK,
PAPER, PAPER,
SCISSORS SCISSORS
}; };
enum roundWinner{
enum RoundWinner{
PLAYERWINSROUND, PLAYERWINSROUND,
COMPUTERWINSROUND, COMPUTERWINSROUND,
NOWINNER NOWINNER
}; };
enum gameWinner{
enum GameWinner{
PLAYERWINSGAME, PLAYERWINSGAME,
COMPUTERWINSGAME, COMPUTERWINSGAME,
NOTWONYET NOTWONYET
}; };
int findWinner(int inputPlayer, int inputComputer);
int findWinner(int playerInput, int computerInput);
int getComputerInput(); int getComputerInput();
int wasGameWon(int roundsToWin, int playerWins, int computerWins); int wasGameWon(int roundsToWin, int playerWins, int computerWins);
bool validatePlayerInput(int playerInput); bool validatePlayerInput(int playerInput);
int getPlayerInput(); int getPlayerInput();
int play(int rounds);
int playRockPaperScissors(int rounds);
#endif #endif
Loading…
Cancel
Save