473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
2 2496
weaknessforcats
9,208 Expert Mod 8TB
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
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

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

Similar topics

8
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a...
3
by: Manish Sawjiani | last post by:
Dear experts i have just migrated from asp to asp.net and i am missing the loop for creating tables. while in asp one could just start a loop anywhere withing <% %is this out of style in the...
1
by: Apirsun | last post by:
I've been trying to use loops to add labels to a frame. I was able to get the program to compile but the labels aren't showing up. import javax.swing.*; import java.awt.*; import...
4
by: janice3 | last post by:
hi.I am a new member in this site. today my question is how to make a diamond shape using loops? the shape is like: * * * * * * * * * * * * * *...
2
by: domka | last post by:
hello, I making xml to excel report using by php. I sow problem and can't fix it. when i use loop like this for ($i=1; $i<5; $i++){ echo"<Row><Cell ss:StyleID=\"s52\"><Data...
3
by: aparna kulkarni | last post by:
i want to find out prime nos from 1 to 25 using do while loop.
1
by: =?ISO-8859-1?Q?Christian_R=FChl?= | last post by:
hi @all! i have a little problem with xsd. is it possible to define a loop in a schema? the xml files i want to validate look like this: - product -- component --- sub-component
11
by: blackhacker | last post by:
Please can anyone help me to make a simple simple Java code to force the game scissor,paper,rock to work ? for example i know how to do it in Visual Basic but not in java,for example this is kind...
1
by: flg22 | last post by:
Hi I am working on this Rock, paper, scissors java game and the program works, but I can not figure out how to get the images to load onto the program. So my question is how do I get the images to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.