From 9973d26fd3d67a55552fe2b52f74363410a02340 Mon Sep 17 00:00:00 2001 From: David Aryasetia <99853366+davidaryasetia@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:42:19 +0700 Subject: [PATCH] Update RPS.cpp C++ codecademy, add logic conditional filter and fix the bug --- .../rock-paper-scissors-lizard-spock/RPS.cpp | 190 +++++++++--------- 1 file changed, 100 insertions(+), 90 deletions(-) diff --git a/3-conditionals-and-logic/rock-paper-scissors-lizard-spock/RPS.cpp b/3-conditionals-and-logic/rock-paper-scissors-lizard-spock/RPS.cpp index 13c6fe7..079d0bb 100644 --- a/3-conditionals-and-logic/rock-paper-scissors-lizard-spock/RPS.cpp +++ b/3-conditionals-and-logic/rock-paper-scissors-lizard-spock/RPS.cpp @@ -1,98 +1,108 @@ +/* +C++ codecademy project rock, paper, scissors +description : +1. Prompts the user to select either Rock, Paper, Scissors, Lizard, or Spock. +2. Insruct the computer to randomly select either Rock, Paper, Scissors, Lizard or Spock. +3. Compares the user's choice and the computer's choice and determine the winner. +4. Informs the user who the winner is + +Instruct logic to win the game : +1. Scissors cuts Paper. +2. Papers covers Rock. +3. Rock crushed Scissors +*/ #include -#include - -int main() { - - srand(time(NULL)); - - int computer = std::rand() % 3 + 1; - - int user; - - std::cout << "====================\n"; - std::cout << "rock paper scissors!\n"; - std::cout << "====================\n"; - - std::cout << "1) ✊\n"; - std::cout << "2) ✋\n"; - std::cout << "3) ✌️\n"; - - std::cout << "shoot! "; - - std::cin >> user; - - if (user == 1) - std::cout << "you choose: ✊\n"; - else if (user == 2) - std::cout << "you choose: ✋\n"; - else - std::cout << "you choose: ✌️\n"; - - if (computer == 1) - std::cout << "cpu choose: ✊\n"; - else if (computer == 2) - std::cout << "cpu choose: ✋\n"; - else - std::cout << "cpu choose: ✌️\n"; - - - if (user == computer) { - - std::cout << "it's a tie!\n"; - - } - - // user rock - - else if (user == 1) { - - if (computer == 2) { - - std::cout << "you lost! booooo!\n"; - - } - if (computer == 3) { - - std::cout << "you won! woohoo!\n"; - - } - - } - - // user paper - - else if (user == 2) { - - if (computer == 1) { - - std::cout << "you won! woohoo!\n"; - - } - if (computer == 3) { - - std::cout << "you lost! boo!\n"; - +#include +#include + +int main (){ + + // make random computer choice + srand (time(NULL)); + + int computer_input = rand() % 3 + 1; + int user_input = 0; + std::string computer_shoot; + std::string user_shoot; + std::string result_winner; + + std::cout << "================================================\n"; + std::cout << "Welcome to challenge game rock, paper, scissors \n\n"; + + std::cout << "1) ✊ Rock \n"; + std::cout << "2) ✋ Paper \n"; + std::cout << "3) ✌ Scissors \n"; + std::cout << "Choice your answer : "; + std::cin >> user_input; + std::cout << "\n"; + + std::cout << "Result Shoot : \n"; + // Make logic conditional for user shoot + if (user_input == 1){ + user_shoot = "✊ Rock"; + } else if (user_input == 2){ + user_shoot = "✋ Paper"; + } else if (user_input == 3){ + user_shoot = "✌ Scissors"; + } else { + user_shoot = "Error invalid input from user input (1 to 3)"; } - } - - // user scissors - - else if (user == 3) { - - if (computer == 1) { - - std::cout << "you won! woohoo!\n"; - + // Make logic conditional for computer shoot + if (computer_input == 1 && (user_input == 1 || user_input == 2 || user_input == 3)){ + computer_shoot = "✊ Rock"; + } else if (computer_input == 2 && (user_input == 1 || user_input == 2 || user_input == 3)){ + computer_shoot = "✋ Paper"; + } else if (computer_input == 3 && (user_input == 1 || user_input == 2 || user_input == 3)){ + computer_shoot = "✌ Scissors"; + } else if ((computer_input == 1 && computer_input == 2 || computer_input == 3) || (user_input != 1 || user_input != 2 || user_input !=3)){ + computer_shoot = "Error invalid computer shoot from user input"; + } else { + computer_shoot = "Error"; } - if (computer == 2) { - - std::cout << "you lost! booooo!\n"; + // Code to make conditional logic to win win result + if (computer_input == 1){ + if (user_input == 1){ + result_winner = "= Equal seri"; + } else if (user_input == 2){ + result_winner = "😎 User Input"; + } else if (user_input == 3){ + result_winner = "💻 Computer Input"; + } else { + result_winner = "Error - Invalid Input User"; + } + + } else if (computer_input == 2){ + if (user_input == 1){ + result_winner = "💻 Computer Input"; + } else if (user_input == 2){ + result_winner = "= Equal seri"; + } else if (user_input == 3){ + result_winner = "😎 User Input"; + } else { + result_winner = "Error - Invalid Input User"; + } + + } else if (computer_input == 3){ + if (user_input == 1){ + result_winner = "😎 User Input"; + } else if (user_input == 2){ + result_winner = "💻 Computer Input"; + } else if (user_input == 3){ + result_winner = "= Equal seri"; + } else { + result_winner = "Error - Invalid Input User"; + } + } else { + result_winner = "Error - Program"; } - } - - return 0; - + std::cout << "User choice : " << user_shoot << "\n"; + std::cout << "Computer choice : " << computer_shoot << "\n\n"; + std::cout << "Comparison war (user vs computer) : " << user_shoot << " vs " << computer_shoot << "\n"; + std::cout << "Result Winner Of : " << result_winner << "\n"; + std::cout << "Thank For Play this game \n"; + std::cout << "================================================\n"; + return 0; }