diff --git a/RockPaperScissor/main.cpp b/RockPaperScissor/main.cpp index 21a46cc..b93f1bc 100644 --- a/RockPaperScissor/main.cpp +++ b/RockPaperScissor/main.cpp @@ -1,109 +1,8 @@ -#include -using namespace std; - -int main() -{ - int a[3] = {1, 2, 3}; - int w; - string b[3] = {"Rock", "Paper", "Scissor"}; - int uc, cc, c; - - while(true) - { - cout<<"Enter 1 for Rock.\n"; - cout<<"Enter 2 for Paper.\n"; - cout<<"Enter 3 for Scissor.\n"; - cout<<"Enter your choice:\n"; - cin>>uc; - - cc = a[(rand() % 3)]-1; - - cout<<"You chose: "<>c; - - if(c == 2) - { - cout<<"Session being terminated...\n"; - exit(0); - } - else if(c != 1) - { - cout<<"Wrong choice! Session being terminated...\n"; - exit(0); - } - else - { - cout<<"\e[1;1H\e[2J"; - cc=0; - uc=0; - } - } - - +#include +#include "rockpaperscissor.hpp" +int main() { + RockPaperScissor game; + game.run(); return 0; } diff --git a/RockPaperScissor/rockpaperscissor.cpp b/RockPaperScissor/rockpaperscissor.cpp new file mode 100644 index 0000000..8862652 --- /dev/null +++ b/RockPaperScissor/rockpaperscissor.cpp @@ -0,0 +1,105 @@ +#include +#include +#include "rockpaperscissor.hpp" + +using namespace std; + +RockPaperScissor::RockPaperScissor() { + choices[0] = 1; + choices[1] = 2; + choices[2] = 3; + names[0] = "Rock"; + names[1] = "Paper"; + names[2] = "Scissor"; +} + +int RockPaperScissor::getUserChoice() { + int uc; + cout<<"Enter 1 for Rock."<<"\n"; + cout<<"Enter 2 for Paper."<<"\n"; + cout<<"Enter 3 for Scissor."<<"\n"; + cout<<"Enter your choice:"<<"\n"; + if(!(cin>>uc)) { + // invalid input, clear and return -1 + cin.clear(); + string dummy; + getline(cin, dummy); + return -1; + } + if(uc < 1 || uc > 3) return -1; + return uc; +} + +int RockPaperScissor::getComputerChoice() { + return (rand() % 3); // returns 0..2 +} + +int RockPaperScissor::determineWinner(int userChoiceIndex, int computerChoiceIndex) { + // Convert userChoiceIndex (1..3) to 0-based + int u = userChoiceIndex - 1; + int c = computerChoiceIndex; + if(u == c) return -1; // tie + + // rock(0) beats scissor(2) + if(u == 0 && c == 2) return 1; + if(u == 1 && c == 0) return 1; // paper beats rock + if(u == 2 && c == 1) return 1; // scissor beats paper + + return 0; // user loses +} + +void RockPaperScissor::clearScreen() { + // Best-effort clear screen for many terminals + cout << "\e[1;1H\e[2J"; +} + +void RockPaperScissor::run() { + int w; + int uc, cc, c; + while(true) { + uc = getUserChoice(); + if(uc == -1) { + cout<<"Invalid choice. Session being terminated..."<<"\n"; + return; + } + + cc = getComputerChoice(); + + cout<<"You chose: "<>c)) { + cout<<"Invalid input. Session being terminated..."<<"\n"; + return; + } + + if(c == 2) { + cout<<"Session being terminated..."<<"\n"; + return; + } + else if(c != 1) { + cout<<"Wrong choice! Session being terminated..."<<"\n"; + return; + } + else { + clearScreen(); + cc=0; + uc=0; + } + } +} diff --git a/RockPaperScissor/rockpaperscissor.hpp b/RockPaperScissor/rockpaperscissor.hpp new file mode 100644 index 0000000..f419d35 --- /dev/null +++ b/RockPaperScissor/rockpaperscissor.hpp @@ -0,0 +1,26 @@ +#ifndef ROCKPAPERSCISSOR_HPP +#define ROCKPAPERSCISSOR_HPP + +#include + +class RockPaperScissor { +public: + RockPaperScissor(); + // Run the main game loop + void run(); + +private: + int choices[3]; + std::string names[3]; + + // Get the user's choice (1..3) + int getUserChoice(); + // Get the computer's choice (0..2) + int getComputerChoice(); + // Determine winner: returns 1 if user wins, 0 if user loses, -1 if tie + int determineWinner(int userChoiceIndex, int computerChoiceIndex); + // Clear the terminal screen (best-effort) + void clearScreen(); +}; + +#endif // ROCKPAPERSCISSOR_HPP