By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

Stuck on using for loops to redesign this paper, scissors, rock game...

P: 2
I'm supposed to redesign this paper scissors rock game that first happened between 2 players, and for only one round. Yet, now I'm supposed to take for loops and ask them if they want to play against an A.I., or against just another player, or another player and an A.I.. Also, I am supposed to ask them how many rounds they want to play using the for loop. This is the jumble of mess I started, and I haven't been able to work around it so far.. A little bit of help to clear some things up in my code would be nice.

//4. Re-write the rock-paper-scissors program to first prompt the user for the number of rounds they would like to play.
//You only need to prompt the user for their name once. After all rounds are over, the program should display the win rate of both
//players and who was the total winner. Also prompt the user if they would like to play against a AI. If so the AIís player name is
//CP and just chooses a random option.
//Example additional output:
//<player1name> won x out of y games.
//<player2name> won z out of y games.
//<playerx> is the champion!

#include <iostream>
#include <string>
using namespace std;

int main()
{
const int ROCK = 1,
PAPER = 2,
SCISSORS = 3;

int numplayer;
int numround;
char player3;


string p1,
p2;

string ai = "CP";
string question;

int c1,
c2;
int c3;
cout << "How many rounds of Paper, Scissors, Rock would you like to play?\n";
cin >> numround;
cout << "How many players would you like to play with?\n";
cin >> numplayer;
cout << "Player 1, what is your name? ";
cin >> p1;

for (int i = 0; i < numround; i++)
{
if (numplayer == 1)
{
cout << p1 << ", please choose your weapon: \n"
<< "\t1) Rock\n"
<< "\t2) Paper\n"
<< "\t3) Scissors\n"
<< "Enter your choice: ";
cin >> c1;

srand(time(0));

int range = 3;
int min = 1;

int randNum = rand() % range + min;
c2 = randNum;
}



else if( c1 == c2 )
{
cout << p1 << " and " << ai << " tied!\n";
}
else if ((c1 == ROCK && c2 == PAPER ) ||
(c1 == PAPER && c2 == SCISSORS) ||
(c1 == SCISSORS && c2 == ROCK ))
{
cout << ai << " wins!\n";
}
else {
cout << p1 << " wins!\n";
}

}


if (numplayer == 2)
{
cout << "Would you like to play with A.I.?\n";
cin >> question;
}
if (question == "yes")
{
cout << "Player 2, please enter your name: ";
cin >> p2;
cout << p1 << ", please choose your weapon: \n"
<< "\t1) Rock\n"
<< "\t2) Paper\n"
<< "\t3) Scissors\n"
<< "Enter your choice: ";
cin >> c1;

system("cls");

cout << p2 << ", please choose your weapon: \n"
<< "\t1) Rock\n"
<< "\t2) Paper\n"
<< "\t3) Scissors\n"
<< "Enter your choice: ";
cin >> c2;
system("cls");

srand(time(0));

int range = 3;
int min = 1;

int randNum = rand() % range + min;
c3 = randNum;


if( c1 == c2 == c3 )
{
cout << p1 << " and" << p2 << " and" << ai << " tied!\n";
}
else if ( c1 == c2 && c2 != c3)
cout << p1 << " and" << p2 << " tied!\n" << "But" << ai << " loses!\n";
else if ( c1 != c2 && c2 == c3 )
cout << p2 << " and" << ai << " tied!\n" << "But" << p1 << " loses!\n";
else if ( c1 == c3 && c3 != c2 )
cout << p1 << " and" << ai << " tied!\n" << "But" << p2 << " loses1\n";
else if ((c1 == ROCK && c2 == PAPER && c3 == ROCK ) ||
(c1 == PAPER && c2 == SCISSORS && c3 == PAPER) ||
(c1 == SCISSORS && c2 == ROCK && c3 == SCISSORS ))
{
cout << p2 << " wins!\n";
}
else if ((c1 == ROCK && c2 == SCISSORS && c3 == SCISSORS ) ||
(c1 == PAPER && c2 == ROCK && c3 == ROCK) ||
(c1 == SCISSORS && c2 == PAPER && c3 == PAPER ))
{
cout << p1 << " wins!\n";
}
else if ((c1 == PAPER && c2 == PAPER && c3 == SCISSORS ) ||
(c1 == SCISSORS && c2 == SCISSORS && c3 == ROCK) ||
(c1 == ROCK && c2 == ROCK && c3 == PAPER ))
{
cout << ai << " wins!\n";
}
else if(question == "no")
{

cout << p1 << ", please choose your weapon: \n"
<< "\t1) Rock\n"
<< "\t2) Paper\n"
<< "\t3) Scissors\n"
<< "Enter your choice: ";
cin >> c1;

system("cls");

cout << p2 << ", please choose your weapon: \n"
<< "\t1) Rock\n"
<< "\t2) Paper\n"
<< "\t3) Scissors\n"
<< "Enter your choice: ";
cin >> c2;
system("cls");


if( c1 == c2 )
{
cout << p1 << " and " << p2 << " tied!\n";
}
else if ((c1 == ROCK && c2 == PAPER ) ||
(c1 == PAPER && c2 == SCISSORS) ||
(c1 == SCISSORS && c2 == ROCK ))
{
cout << p2 << " wins!\n";
}
else
cout << p1 << " wins!\n";
}
}

system("PAUSE>NUL");
return 0;
}
Oct 15 '10 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
The fundamental problem is lack of a function structure. What you have is a "stream of consiousness" coding.

I suggest you:
1) Write a function to determine the oppopent. The function can return the opponent.
2) write another function to detemine the number cycles.
The function should return the number of cycles.
3) write a function to play the game. It should have the opponent and the number of cycles as arguments.

By breaking you problem into smaller logical chunks you can focus on coding smaller pieces of logic.

Skilled developers have functtions calling functions until the code of the function beng called is obvious.
Oct 15 '10 #2

P: 2
The problem is at the time of this lab we had barely learned for loops, and that is all we could use on it. Also, I just learned functions this week, so I can't do any function calls or anything right now, just gotta do this using the for loops.
Oct 16 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.