Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions RockPaperScissor/Makefile
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 7 additions & 2 deletions RockPaperScissor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 5 additions & 106 deletions RockPaperScissor/main.cpp
Original file line number Diff line number Diff line change
@@ -1,109 +1,8 @@
#include<iostream>
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: "<<b[uc-1]<<"\n";
cout<<"Computer choose: "<<b[cc]<<"\n";

if(a[uc-1] == 1)
{
if(a[cc] == 3)
{
w = 1;
}
else if(a[cc] == 2)
{
w = 0;
}
else
{
w = -1;
}
}
else if(a[uc-1] == 2)
{
if(a[cc] == 1)
{
w = 1;
}
else if(a[cc] == 3)
{
w = 0;
}
else
{
w = -1;
}
}
else
{
if(a[cc] == 2)
{
w = 1;
}
else if(a[cc] == 1)
{
w = 0;
}
else
{
w = -1;
}
}

if(w == 1)
{
cout<<"You won!\n";
}
else if(w == 0)
{
cout<<"You lose!\n";
}
else
{
cout<<"Tie!\n";
}

cout<<"Do you want to play again?\n";
cout<<"Enter 1 for YES.\n";
cout<<"Enter 2 for NO.\n";
cin>>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 <iostream>
#include "rockpaperscissor.hpp"

int main() {
RockPaperScissor game;
game.run();
return 0;
}
108 changes: 108 additions & 0 deletions RockPaperScissor/rockpaperscissor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "rockpaperscissor.hpp"

using namespace std;

RockPaperScissor::RockPaperScissor() {
// Seed RNG so computer choice differs across runs
std::srand(static_cast<unsigned int>(std::time(nullptr)));
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: "<<names[uc-1]<<"\n";
cout<<"Computer chose: "<<names[cc]<<"\n";

w = determineWinner(uc, cc);

if(w == 1) {
cout<<"You won!"<<"\n";
}
else if(w == 0) {
cout<<"You lose!"<<"\n";
}
else {
cout<<"Tie!"<<"\n";
}

cout<<"Do you want to play again?"<<"\n";
cout<<"Enter 1 for YES."<<"\n";
cout<<"Enter 2 for NO."<<"\n";
if(!(cin>>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;
}
}
}
26 changes: 26 additions & 0 deletions RockPaperScissor/rockpaperscissor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ROCKPAPERSCISSOR_HPP
#define ROCKPAPERSCISSOR_HPP

#include <string>

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