Jump to content
  • Advertisement
Sign in to follow this  
hothead

need help with rock,paper,sissors code

This topic is 5037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a simple game of rock,paper,sissors my code won't work here it is
//game of rock paper sissors

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
    srand(time(3));//seed the random generater
    int mychoice= ,rock,paper,sissors;
        cout<<"enter rock,paper or sissors";
                cin>>mychoice;
    int choice = rand(3%) && rock,paper,sissors; 
        if(mychoice = rock && choice = paper)
    cout<<"\nyou lose";
        if(mychoice = paper && choice = rock)
    cout<<"\nyou win";
        if(mychoice = sissors && choice = rock)
    cout<<"\nyou lose";
        if(mychoice = rock && choice = sissors)
    cout<<"\nyou win";
        if(mychoice = paper && choice = sissors)
    cout<<"\nyou lose";
        if(mychoice = sissors && choice = paper)
    cout<<"\nyou win";
        else
            cout<<"\nits a tie";
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
What do you mean by "doesn't work"? Problems I can see:
  • In your if statements, use ==, not =. = means assignment, == means comparison.
  • srand(time(3)) <-- That'll crash. Use NULL, not 3
  • int mychoice= ,rock,paper,sissors; <-- What does that mean? Whats it supposed to do?
  • int choice = rand(3%) && rock,paper,sissors; <-- Again, whats that supposed to do?

If thats C++, its very messed up...

Share this post


Link to post
Share on other sites
i think i fixed it

#include<iostream>
#include<cstdlib>
#include<ctime>

#define rock 0
#define paper 1
#define sissors 2

using namespace std;
int main()
{
srand(time(3));//seed the random generater
int mychoice=0;
cout<<"enter rock(1),paper(2) or sissors(3)";
cin>>mychoice;
mychoice--;
int choice = rand()%3;
if((mychoice == rock )&& (choice = paper))
{
cout<<"\nyou lose";
}
else if((mychoice == paper)(choice = rock))
{
cout<<"\nyou win";
}
else if((mychoice == sissors))choice = rock))
{
cout<<"\nyou lose";
}
else if((mychoice == rock)(choice = sissors))
{
cout<<"\nyou win";
}
else if((mychoice == paper)(choice = sissors))
{
cout<<"\nyou lose";
}
else if((mychoice == sissors) && (choice = paper))
{
cout<<"\nyou win";
}
else
{
cout<<"\nits a tie";
}
return 0;
}

googl for a c++ tut

Share this post


Link to post
Share on other sites
Thats still not fixed.
On if((mychoice == rock )&& (choice = paper)) and the other similar lines, the second = still needs to be ==, and the srand(time(3)); should still crash, or at least not compile - time() takes a pointer to a structure to fill in, not an integer.

Share this post


Link to post
Share on other sites
Thanks but I've still got errorsfile
//game of rock paper sissors

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;
int main()
{
srand(null(3));//seed the random generater
int mychoice= rock||paper||sissors;
cout<<"enter rock,paper or sissors";
cin>>mychoice;
int choice = rand(3%);
string choice = rock||paper||sissors;
if(mychoice == rock && choice == paper)
cout<<"\nyou lose";
if(mychoice == paper && choice == rock)
cout<<"\nyou win";
if(mychoice == sissors && choice == rock)
cout<<"\nyou lose";
if(mychoice == rock && choice == sissors)
cout<<"\nyou win";
if(mychoice == paper && choice == sissors)
cout<<"\nyou lose";
if(mychoice == sissors && choice ==paper)
cout<<"\nyou win";
else
cout<<"\nits a tie";
return 0;
}

Share this post


Link to post
Share on other sites

  • srand(null(3)); should be srand(time(NULL)); (sorry for my rubbish explanation :P)
  • int mychoice= rock||paper||sissors; evaluates to 1. You can change this line just to int mychoice;
  • int choice = rand(3%); This also makes no sense. You probably want int choice = rand()%3
  • What happened to the #define lines? They were correct.
  • string choice = rock||paper||sissors; Also makes no sense

The thing about string, is probably because you typo'd the #include filename. It should be string, not sring.

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
this should work

*** Source Snippet Removed ***

edit: frogot to seed

Aww, I wanted to see what other amusing versions of C++ he could come up with [crying]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!