Search
 
SCRIPT & CODE EXAMPLE
 

CPP

rock paper scissor c++

#include <iostream>
 
// Constant variables
const char ROCK = 'r';
const char PAPER = 'p';
const char SCISSORS = 's';
 
using namespace std;
 
char getComputerOption() {
    srand(time(0));
    // Random number
    int num = rand() % 3 + 1;
 
    if(num==1) return 'r';
    if(num==2) return 'p';
    if(num==3) return 's';
}
 
char getUserOption() {
    char c;
    cout << "Rock, Paper and Scissors Game!" << endl;
    cout << "Choose one of the following options"  << endl;
    cout << "-----------------------------------"  << endl;
    cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
    cin >> c;
    
    while (c!='r' && c!='p' && c!='s' )
    {
        cout << "Please enter one of the following options only. " << endl;
        cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
        cin >> c;
    }
 
    return c;
}
 
void showSelectedOption(char option) {
    if (option == 'r') cout << "Rock" << endl;
    if (option == 'p') cout << "Paper" << endl;
    if (option == 's') cout << "Scissors" << endl;
}
 
void chooseWinner(char uChoice, char cChoice) {
    if (uChoice == ROCK && cChoice == PAPER) {
        cout << "Computer Wins! Paper wraps Rock."<< endl;
    }
    else if (uChoice == PAPER && cChoice == SCISSORS) {
        cout << "Computer Wins! Scissors cut Paper."<< endl;
        
    }
    else if (uChoice == SCISSORS && cChoice == ROCK) {
        cout << "Computer Wins! Rock smashes Scissors."<< endl;
        
    }
    else if (uChoice == ROCK && cChoice == SCISSORS) {
        cout << "You Win! Paper wraps Rock."<< endl;
        
    }
    else if (uChoice == PAPER && cChoice == ROCK) {
        cout << "You Win! Paper wraps Rock."<< endl;
        
    }
    else if (uChoice == SCISSORS && cChoice == PAPER) {
        cout << "You Win! Scissors cut Paper."<< endl;
    }
    else{
        cout << "Tie. Play again win the Game." << endl;
    }
}
 
int main() {
    //User's choice
    char uChoice; 
    //Compter's choice
    char cChoice;
    
    uChoice = getUserOption();
    cout << "Your choice is: "<< endl;
    showSelectedOption(uChoice);
    
    cout << "Computer's choice is: "<< endl;
    cChoice = getComputerOption();
    showSelectedOption(cChoice);
    
    chooseWinner(uChoice, cChoice);
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to copy vector to another vector in c++ 
Cpp :: how to empty a std vector 
Cpp :: how to change the value of a key in hashmp in c++ 
Cpp :: Translation codeforces in c++ 
Cpp :: get std string from file 
Cpp :: fstream read write mode 
Cpp :: onoverlapbegin ue4 c++ 
Cpp :: resharper fold if statement c+ 
Cpp :: print reverse number 
Cpp :: power in c++ 
Cpp :: programs using vectors in c++ 
Cpp :: balanced parentheses 
Cpp :: c++ define constant in class header 
Cpp :: c++ custom hash 
Cpp :: how to get last element of set 
Cpp :: move assignment operator c++ 
Cpp :: pow without math.h 
Cpp :: int to string C++ Using stringstream class 
Cpp :: how to convert char to int in c++ 
Cpp :: nested conditional operator 
Cpp :: C++ function inside main 
Cpp :: Array Rotate in c++ 
Cpp :: use declaration to define a variable 
Cpp :: vector of vectors c++ 
Cpp :: book allocation problem in c++ 
Cpp :: string class in c++ 
Cpp :: how to run cpp in visual studio 
Cpp :: finding nth most rare element code in c++ 
Cpp :: How to remove the % in zsh that show after running c++ file 
Cpp :: c++ localtime unsafe 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =