Sign in to follow this  
hothead

how do i make this loop?

Recommended Posts

can someone help me make this game loop?
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
    const int rock = 1;
    const int sissors = 2;
    const int paper = 3;
    
    int MyChoice;
    srand(time(0));//seed the random generater
    int Choice = rand() % 3;
    cout<<"rock(1), paper(2), sissors(3)\n";
    cin>>MyChoice;
    
    if((MyChoice == rock) && (Choice == paper))
    {
         cout<<"you lose";
    }
    if((MyChoice == rock) && (Choice == sissors))
    {
        cout<<"you win";
    }
    if((MyChoice == paper) && (Choice == sissors))
    {
        cout<<"you lose";
    }
    if((MyChoice == paper) && (Choice == rock))
    {
        cout<<"you win";
    }
    if((MyChoice == sissors) && (Choice == rock))
    {
        cout<<"you lose";
    }
    if((MyChoice == sissors) && (Choice == paper))
    {
        cout<<"you win";
    }
    if((MyChoice == rock) && (Choice == rock))
    {
        cout<<"tie";
    }
    if((MyChoice == paper) && (Choice == paper))
    {
        cout<<"tie";
    }
    if((MyChoice == sissors) && (Choice == sissors))
    {
        cout<<"tie";
    }
    
    cin>>Choice;
    return 0;
}

Share this post


Link to post
Share on other sites
Make a new bool variable called done. Seperate from the point where you want the loop to begin to where you want it to end (or create a whole new function, which I recommend) and put all of that in a while(!done) {} loop.

Ask if you have ?s.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no offense, but i believe that i have seen you post before about the same game. then too you misspelled "scissors".

again, just giving a heads up to save some possible embarassment.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
no offense, but i believe that i have seen you post before about the same game. then too you misspelled "scissors".


I know I like to spell scissors like that .

Share this post


Link to post
Share on other sites
You'll also want to set the bool done to false before entering the loop, and to true somewhere inside the loop when the user wants it. So you might add another option after scissors, say 4. Then add a if (MyChoice == 4) done = true;

Also, even if you prefer to spell it sissors, it's still wrong. [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by nagromo
Also, rand()%3 will give you a number from 0 to 2. You should use rand()%3+1.

It must not of pasted the +1 or something?.

Share this post


Link to post
Share on other sites

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

enum Hand
{
Rock,
Paper,
Scissors
};

Hand winHand[] = { Paper, Scissors, Rock };

int main()
{
srand(time(0));
int playChoice;
while (std::cout << "rock(1), paper(2), scissors(3)\n", std::cin >> playChoice && playChoice >= 1 && playChoice <= 3)
{
Hand compHand = Hand(rand() % 3);
Hand playHand = Hand(playChoice - 1);
if (compHand == playHand)
std::cout << "tie\n";
else if (winHand[compHand] == playHand)
std::cout << "you win\n";
else
std::cout << "you lose\n";
}
std::cout << "bye\n";
}

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