https://programmingclues.blogspot.com/2013/08/rock-paper-and-scissor-gameusing.html#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////
//fuction prototype
void gameRules();
int userSelectionChoice();
int choice;
int userSelection;
int cpuChoice();
int choiceComputer;
void gameResult();
///////////////////////////////////////////////////////////////////////////////////////////
int main()
{
char again = 'y';
do
{
gameRules();
cout<<'\n'<<endl;
userSelection= userSelectionChoice();
int cpuChoice();
choiceComputer=cpuChoice();
gameResult();
cout<<"The coumputer choice was:"<<choiceComputer<<endl;
cout << "Would you like to play Again? Y/N ";
cin >> again;
system("cls");
}
while (again == 'y' || again == 'Y');
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////
void gameRules()
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<"***Welcome to the Rock, Paper and Scissor Game***"<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<'\t'<<"Game's Rule"<<'\n'<<endl;
cout<<"a.If one player chooses rock and the other player chooses scissors,"<<'\n'<<
"then rock wins.(The rock smashes the scissors.)"<<endl;
cout<<'\n'<<endl;
cout<<"b.If one player chooses scissors and the other player chooses paper"<<'\n'<<
" then scissors wins.(Scissors cuts paper.)"<<endl;
cout<<'\n'<<endl;
cout<<"c.If one player chooses paper and the other player chooses rock,"<<'\n'<<
"then paper wins.(Paper wraps rock.)"<<endl;
cout<<'\n'<<endl;
cout<<"d.If both players make the same choice, the game must be played" <<'\n'<<
"again to determine the winner."<<endl;
////code to clear the starting menu
//cout<<"Now you read the rules press 1 to start game:"<<endl;
//system("cls");
}
///////////////////////////////////////////////////////////////////////////////////////////////
int userSelectionChoice()
{
//srand(time(0));
cout<<"Please select your choice"<<endl;
cout<<'\n'<<endl;
cout<<"1.To select Rock."<<endl;
cout<<"2.To select Paper."<<endl;
cout<<"3.To select Scissor."<<endl;
cin>>choice;
while(choice<1||choice>3)
{
if(choice<1||choice>3)
{
cout<<"Wrong Input!!!!" <<endl;
cout<<'\n'<<endl;
cout<<"Please check your choice"<<endl;
cout<<'\n'<<endl;
cout<<"1.To select Rock."<<endl;
cout<<"2.To select Paper."<<endl;
cout<<"3.To select Scissor."<<endl;
cin>>choice;
}
}
return choice;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
int cpuChoice()
{
srand(time(0));
int choiceCPU ;
choiceCPU = rand()%3+1;
return choiceCPU;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void gameResult()
{
//void winner()
{
if (userSelection == 1 && choiceComputer == 1)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<""<<"*"<<" "<<" Tie " << " *"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 1 && choiceComputer == 3)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<" "<<" User Wins " <<" *"<< endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 1 && choiceComputer == 2)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout <<'\t'<<'\t'<< '\t'<<"*"<<"Computer Wins*" << endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 2 && choiceComputer == 2)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<" "<<"Tie " <<" *"<< endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 2 && choiceComputer == 3)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<"Computer Wins*" << endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 2 && choiceComputer == 1)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"****************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<" User Wins " <<" *"<< endl;
cout<<'\t'<<'\t'<<'\t'<<"****************"<<endl;
}
else if (userSelection == 3 && choiceComputer == 3)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout <<'\t'<<'\t'<< '\t'<<"*"<<" "<<"Tie " <<" *"<< endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 3 && choiceComputer == 2)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<" User Wins " <<"*"<< endl;
cout<<'\t'<<'\t'<<'\t'<<"***************"<<endl;
}
else if (userSelection == 3 && choiceComputer == 1)
{
cout<<'\n'<<endl;
cout<<'\t'<<'\t'<<'\t'<<"****************"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"*"<<" "<<"Game Result"<<" *"<<endl;
cout<<'\n'<<endl;
cout << '\t'<<'\t'<<'\t'<<"*"<<"Computer Wins " << "*"<<endl;
cout<<'\t'<<'\t'<<'\t'<<"****************"<<endl;
}
}
}
No comments:
Post a Comment