473,722 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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>N UL");
return 0;
}
Oct 15 '10 #1
2 2532
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Izkimar
2 New Member
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
8237
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 program that reads 10 numbers from the user and prints out the sum of those numbers. num0 = input("Enter a number: ")
3
1997
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 .Net Scenario? Thanks Manish --
1
3119
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 javax.swing.border.*; public class Assignment3 extends JFrame { private ImageIcon cross = new ImageIcon("Assignment3/cross.gif"); private ImageIcon not = new ImageIcon("Assignment3/not.gif"); int count = 0;
4
4808
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
2552
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 ss:Type=\"String\"> </Data><NamedCell ss:Name=\"Print_Area\"/>$i</Cell></Row>"; } everthing generate ok, but if i use for ($i=1; $i<11; $i++){
3
4097
by: aparna kulkarni | last post by:
i want to find out prime nos from 1 to 25 using do while loop.
1
4809
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
2425
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 of what i want Rock beats Scissor Paper beats rock Scissor beats paper msgbox 'Enter your choce' If txta.text = "Rock" then
1
3118
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 load up with the program? I am using JCreator for this project. I have created the Basic Java Application project, and then added in the 3 .java files that I need to run the program, but I just can not figure out how or where I need to upload the...
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.