From 03e14ca157a9968a456d17f7574c3ed83f7f3367 Mon Sep 17 00:00:00 2001 From: Aimee Reincke Date: Thu, 12 Jan 2023 11:46:04 +0100 Subject: [PATCH] refactoring: findWinner duplications removed --- src/c/rockPaperScissors.c | 33 +++++++++++---------------------- src/c/rockPaperScissors.h | 15 +++++++++++---- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/c/rockPaperScissors.c b/src/c/rockPaperScissors.c index 40a00fe..0171e87 100644 --- a/src/c/rockPaperScissors.c +++ b/src/c/rockPaperScissors.c @@ -6,31 +6,20 @@ char findWinner(int inputPlayer, int inputComputer){ + if (inputPlayer == inputComputer){ - return 'n'; + return NOWINNER; + } + else if (inputPlayer == ROCK && inputComputer == SCISSORS) { + return PLAYERWINSROUND; } - else if (inputPlayer == ROCK){ - if (inputComputer == SCISSORS){ - return 'p'; - } - else if (inputComputer == PAPER){ - return 'c'; - } + else if (inputPlayer == PAPER && inputComputer == ROCK) { + return PLAYERWINSROUND; } - else if (inputPlayer == PAPER){ - if (inputComputer == ROCK){ - return 'p'; - } - else if (inputComputer == SCISSORS){ - return 'c'; - } + else if (inputPlayer == SCISSORS && inputComputer == PAPER) { + return PLAYERWINSROUND; } - else if (inputPlayer == SCISSORS){ - if (inputComputer == PAPER){ - return 'p'; - } - else if (inputComputer == ROCK){ - return 'c'; - } + else { + return COMPUTERWINSROUND; } } \ No newline at end of file diff --git a/src/c/rockPaperScissors.h b/src/c/rockPaperScissors.h index 7ce0730..cda1894 100644 --- a/src/c/rockPaperScissors.h +++ b/src/c/rockPaperScissors.h @@ -2,11 +2,18 @@ #define ROCKPAPERSCISSORS_H enum inputOptions{ - ROCK = 1, - PAPER = 2, - SCISSORS = 3 - }; + ROCK, + PAPER, + SCISSORS +}; +enum roundWinner{ + PLAYERWINSROUND = 'p', + COMPUTERWINSROUND = 'c', + NOWINNER = 'n' +}; + + char findWinner(int inputPlayer, int inputComputer); #endif \ No newline at end of file