Speed up development with full-stack environments for every branch.

Learn More

Rock Paper Scissor [C++]

Forked from Hello World C++ Example.

4 Runs 26 Views 5 Copies
Saved

Saved

jfs200027 1

jfs200027
published 12 days ago

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main() {
  srand(time(NULL));
  int numplayer, p, c, cc = 0, pc = 0, p1c = 0, p2c = 0, p1 = 0, p2 = 0; //Declare variables
	cout<<"How many player? "; //Instruction
	cin >> numplayer; //Store input
	if (numplayer == 1) { //1 player
	 do {
	     cout<<"1. Rock    2. Paper    3. Scissor    4. Quit"<<endl; //Information
       int c = (rand() % 3) + 1; //Computer's guess
       cout<<"Select your guess "; //Instruction
       cin>>p; //Store input
          if (p == c) {
            cout<<"Tie"<<endl; //Information
		    	} else if ((p == 1) && (c == 2)) {
				    cout<<"Computer win!"<<endl; //Information
			    	cc++; //Count computer score
		    	} else if ((p == 1) && (c == 3)) {
				    cout<<"Player win!"<<endl; //Information
				    pc++; //Count player score
		    	} else if ((p == 2) && (c == 1)) {
			    	cout<<"Player win!"<<endl; //Information
			    	pc++; //Count player score
		    	} else if ((p == 2) && (c == 3)) {
			    	cout<<"Computer win!"<<endl; //Information
			    	cc++; //Count computer score
	    		} else if ((p == 3) && (c == 1)) {
			    	cout<<"Computer win!"<<endl; //Information
			    	cc++; //Count computer score
		    	} else if ((p == 3) && (c == 2)) {
		    		cout<<"Player win!"<<endl; //Information
			    	pc++; //Count player score
		    	}
		  	} while (p != 4); //End loop
		  	cout<<"Computer wins "<<cc; //Information
		  	cout<<""<<endl;
		  	cout<<"Player wins "<<pc; //Information
	    } else if (numplayer == 2) { //2 player
	      while ((p1c != 4) && (p2c != 4)) {
	      	cout<<"1. Rock    2. Paper    3. Scissor    4. Quit"<<endl;
          cout<<"Player 1, Select your guess "; //Instruction
          cin>>p1c; //Store input
          cout<<"Player 2, Select your guess "; //Instruction
          cin>>p2c; //Store input
          if (p1c == p2c) { //Check who won
		    		cout<<"Tie"<<endl; //Information
		    	} else if ((p1c == 1) && (p2c == 2)) {
			    	cout<<"Player 2 win!"<<endl; //Information
			    	p2++; //Count player2 score
		    	} else if ((p1c == 1) && (p2c == 3)) {
		    		cout<<"Player1 win!"<<endl; //Information
		    		p1++; //count player1 score
		    	} else if ((p1c == 2) && (p2c == 1)) {
	    	  	cout<<"Player1 win!"<<endl; //Information
	    			p1++; //count player1 score
	    		} else if ((p1c == 2) && (p2c == 3)) {
	    			cout<<"Player 2 win!"<<endl; //Information
	    			p2++; //Count player2 score
	    		} else if ((p1c == 3) && (p2c == 1)) {
	    	  	cout<<"Player 2 win!"<<endl; //Information
	    			p2++;  //Count player2 score
	    		} else if ((p1c == 3) && (p2c == 2)) {
		      	cout<<"Player1 win!"<<endl; //Information
		    		p1++; //count player1 score
	    		}
		  	}
		  	cout<<"Player 1 win "<<p1; //Information
		  	cout<<""<<endl;
		  	cout<<"Player 2 win "<<p2; //Information
	    } else {
	      cout<<"This game only support 1-2 players"<<endl;
	    }
	  }
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).