Home‎ > ‎Assignments‎ > ‎Hour of Code‎ > ‎Solutions‎ > ‎

A Solution to the Rock, Paper, Scissors Problem (Uses Functions)

Solution to Programming Assignment (see sample output below)

Rock, Paper, Scissors

//The Rock, Paper, Scissors Problem

//Author: Adina Weisel


#include <iostream>

#include <stdlib.h>

#include <string>

using namespace std;

char morph(int);

char rock(char);

char paper(char);

char scissors(char);


int main()

{

string name;

char w,x,cw,outcome;

cout<<"My name is MEGATRON, what is your name? ";

cin>> name;

cout<<endl<<name<<", we will now be playing Rock, Paper, Scissors. Type R for rock, P for \npaper and S for scissors.\nWhen you want to stop, type E for End.\n\n";

cout<<"CHOOSE YOUR WEAPON: ";

cin>>w;

while (w!='E'){

cout<<endl;

x=rand()%3; 

cw=morph (x);

cout<<"You picked "<<w<<". I picked "<<cw<<".\n";

if (w=='R') outcome=rock(cw);

else if (w=='P') outcome=paper(cw);

else if (w=='S') outcome=scissors(cw);

if (outcome=='T') cout<<"We have tied, I DEMAND REMATCH!";

else if (outcome=='W') cout<<"AHAHAHAHHAHA!! I WIN! I AM THE CHAMPION OF THE WORLD!";

else if (outcome=='L') cout<<name<<", you win. therefore, you must have cheated because I am ALWAYS better than you.";

cout<<endl<<endl;

cout<<"CHOOSE YOUR WEAPON: ";

cin>>w;

}

cout<<"\nWe have proved 1 thing today: I am way awesomer than you, farewell "<<name<<"!!!!!!!!!!!"<<endl<<endl;

return 0;

}


char morph(int x) {

if (x==0) return 'R';

if (x==1) return 'P';

if (x==2) return 'S';

}

char rock(char cw){

if (cw=='R') return 'T';

if (cw=='P') return 'W';

if (cw=='S') return 'L';

}


char paper(char cw){

if (cw=='P') return 'T';

if (cw=='S') return 'W';

if (cw=='P') return 'L';

}


char scissors(char cw){

if (cw=='S') return 'T';

if (cw=='R') return 'W';

if (cw=='P') return 'L';

}


Output:


Comments