From 1b6821774013d7fc05fd698d263ff5d5872ae848 Mon Sep 17 00:00:00 2001 From: Bhoomi Sharma Date: Fri, 31 Oct 2025 23:09:18 +0530 Subject: [PATCH 1/2] refactor(rps): declare RockPaperScissor class in header and implement in cpp --- RockPaperScissor/main.cpp | 111 ++------------------------ RockPaperScissor/rockpaperscissor.cpp | 105 ++++++++++++++++++++++++ RockPaperScissor/rockpaperscissor.hpp | 26 ++++++ 3 files changed, 136 insertions(+), 106 deletions(-) create mode 100644 RockPaperScissor/rockpaperscissor.cpp create mode 100644 RockPaperScissor/rockpaperscissor.hpp 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 From 3e89ca0a7b1bab1fb7bf31fbd2f7b1290c36b4a4 Mon Sep 17 00:00:00 2001 From: Bhoomi Sharma Date: Fri, 31 Oct 2025 23:11:39 +0530 Subject: [PATCH 2/2] chore(rps): seed RNG, add Makefile and README build instructions --- RockPaperScissor/Makefile | 18 ++++++++++++++++++ RockPaperScissor/README.md | 9 +++++++-- RockPaperScissor/rockpaperscissor.cpp | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 RockPaperScissor/Makefile diff --git a/RockPaperScissor/Makefile b/RockPaperScissor/Makefile new file mode 100644 index 0000000..1d8939a --- /dev/null +++ b/RockPaperScissor/Makefile @@ -0,0 +1,18 @@ +CC := g++ +CFLAGS := -std=c++11 -O2 +TARGET := rps +SRC := main.cpp rockpaperscissor.cpp + +.PHONY: all build run clean + +all: build + +build: + $(CC) $(CFLAGS) $(SRC) -o $(TARGET) + +run: build + @echo "Running Rock Paper Scissor..." + ./$(TARGET) + +clean: + rm -f $(TARGET) diff --git a/RockPaperScissor/README.md b/RockPaperScissor/README.md index b91ecfc..a6103fa 100644 --- a/RockPaperScissor/README.md +++ b/RockPaperScissor/README.md @@ -17,8 +17,13 @@ $ git clone https://github.com/VickyTheRocker/BeginnerProjects.git To run the game, navigate to the directory where the game is located and run the following command: ```bash -$ g++ main.cpp -o main -$ ./main +$ # Build with g++ +$ g++ main.cpp rockpaperscissor.cpp -o rps +$ ./rps + +# Or use the included Makefile (Unix/macOS / WSL / Git Bash) +make +make run ``` ## How to Play diff --git a/RockPaperScissor/rockpaperscissor.cpp b/RockPaperScissor/rockpaperscissor.cpp index 8862652..0c291d2 100644 --- a/RockPaperScissor/rockpaperscissor.cpp +++ b/RockPaperScissor/rockpaperscissor.cpp @@ -1,10 +1,13 @@ #include #include +#include #include "rockpaperscissor.hpp" using namespace std; RockPaperScissor::RockPaperScissor() { + // Seed RNG so computer choice differs across runs + std::srand(static_cast(std::time(nullptr))); choices[0] = 1; choices[1] = 2; choices[2] = 3;