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"
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");
}

10
src/c/rockPaperScissors.h

@ -3,30 +3,30 @@
#include <stdbool.h>
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
Loading…
Cancel
Save