Sign in to follow this  
hothead

need help with rock,paper,sissors code

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
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
Don't you have to pick a certain variable to be enumerated? The way you have it right now, wouldn't 0, 1 or 2 just evaluate to those numbers? Or because you didn't state a certain variable would that apply to all variables (never heard that before, just guessing).

Share this post


Link to post
Share on other sites
Quote:
Original post by DaemonMagus
I think he means you ruined his fun. He wanted to see hothead devise other methods of doing this (incorrectly).

Yup, pretty much :P. Handing out an answer to him like that won't help him understand C++ anyway, particularly if this is a homework question (which it may or may not be).
And I was having quite a giggle with his versions [wink]

Share this post


Link to post
Share on other sites
*g*
Hothead, don't worry, it's all konfusing in the beginning. Sorry if they are making jokes :)
You should perhaps buy a beginner C++ book.

Good luck :) And remember, don't be afraid to make errors (even if that means to be punned by gamedevs), because thats the best way to learn.

Thermo/Konfu

DANG! Again 1 point dropped! WTF am I doing wrong? Please, ppl, just tell me if I should stop posting in this forum.
If you don't, and my rating drops more, I have to commit suicide, because 1 601 n0 1!ph

Share this post


Link to post
Share on other sites
Quote:
Original post by DerAnged
Ok thats just mean, at least hes trying instead of posting "IWANT RPC(youKnowWHaT TH4444T IS!!!) codeFixoredN0WW!!!11~~"

Yeah, sorry if I upset anyone, my ratings already dropped 7 points since I started posting in this topic [cry]. I didn't intend to be mean to him, I just thought some of his versions were quite funny. However, he obviously doesn't understand the basics of C++ very well, and I think that reading up on some stuff would be better than trying to get this program working. And this is a reasonably simple homewory style question you could well be offered, which is why I didn't want to give him a complete solution...

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
*g*
Hothead, don't worry, it's all konfusing in the beginning. Sorry if they are making jokes :)
I found a good intro to C++, which you should read thoroughly, as it will clear up most of the issues that came up with that game.
Good luck :) And remember, don't be afraid to make errors (even if that means to be punned by gamedevs), because thats the best way to learn.

Thermo/Konfu


the tut your link points to seems to be incompleat

http://www.cplusplus.com/doc/tutorial/ is more compleat

and http://www.cplusplus.com/ also has some sources to look at

Share this post


Link to post
Share on other sites
int myChoice has to be a number, it can't be a string and it deffinately can't be 3 strings... Try this int mychoice
then create a randon number 1-3 for mychoice and then use a switch for what the 3 numbers would mean.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this