Hello,

Please take a look at the below code -

#include<iostream>
#include<ctime>      // for the time() function
#include<cstdlib>    // for the srand() and rand() functions
using namespace std;


int compInput;
int userInput;
int die1 = 0;
int die2 = 0;

int Dice ()
{
    // set the seed
    srand(time(0));

    // roll the first die
    die1 = (rand() % 6 ) + 1;

    // roll the second die
    die2 = (rand() % 6 ) + 1;
}

int compGame()

{
    Dice ();
    cout << "The computer rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
    cout << "Total = " << die1 + die2 << endl;
}


int userGame()
{
    cout << " User turn --- Press 2 to roll";
    cin >> userInput;

    if ( userInput == 2 )
    {
        Dice ();

        cout << "The user rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
        cout << "Total = " << die1 + die2 << endl;
    }

    else {
        cout << "Wrong input. Try again";
        userGame();
    }
}




int main ()
{
    compGame();
    userGame();

    if (compGame() == 7 || compGame() == 11)
    {
        cout <<"Computer won";
    }

    else if (compGame() == 2)
    {
        cout << "Computer lost";
    }

    else if (userGame() == 7 || userGame() == 11)
    {
        cout <<"User won";
    }

    else if (userGame() == 2)
    {
        cout << "User lost";
    }

    else
    {
        main();
    }


    return 0;

}

I know the main() is not right there. What I need to have there is, if computer rolled 7 or 11 (die1 + die 2) then computer wins and the same for the user. And if either of them rolls 2 (die1 + die 2), the respective player rules.

So what kind of loop should go in main() to make the program right?

Recommended Answers

All 18 Replies

So what kind of loop should go in main() to make the program right?

any kind of loop will do, that's mostly user preference though I suppose what you meant was
" what condition is to be used for a loop? " right?

which you need to post more details for us to know when should the program end

now for the program.
after looking at the program, notice that your functions are not returning any values

Hi,

yes thats right. I need to know what condition needs to go. The reason, there te function are not returning values is i cant figure what loop condition should be there. That being said, a program should end once there is a winner or a looser. If none of those, then the program should keep runnign untill there is a winner or looser

you could use a boolean and change it's value if it falls under any of the conditions found in main otherwise by the end of the loop if the value did not change then it will continue to iterate again

also at theUserGame function don't use a recursive call to check if the input is correct use a loop instead

hi,

can you give an example of the boolean loop you mentioned above ?

Thanks

like this ?

int main ()
{
    compGame();
    userGame();


    bool value = true;
    while (value)
    {

        if (compGame() == 7 || compGame() == 11)
        {
            cout <<"Computer won";
            break;
        }
        else if (compGame() == 2)
        {
            cout << "Computer lost";
            break;
        }
        else if (userGame() == 7 || userGame() == 11)
        {
            cout <<"User won";
            break;
        }
        else if (userGame() == 2)
        {
            cout << "User lost";
            break;
        }
        else
        {
            main();

        }

    }

remove the else main(), no need for recursive call
and instead of a break statement change the value of the boolean to stop the loop

Let me see if I understand this.

Player 1 rolls the dice.
-- If he win (7/11) game ends.
-- If he looses (2) game ends.
If neither, Player 2 rolls.
They just keep passing the dice after one roll.
Is that about it?

Some current problems:
1) srand() can only be called once and expect it to work correctly. Move it to the beginning of main()
2) Never NEVER NEVER call main(). Use a loop to repeat the code.
3) If the user enters the wrong input, don't call the function (that's recursion -- look it up if you care). Just use a loop to ask again.
4) In dice() you roll the dice but never do anything with them. The values are lost. Don't you want to return the total value?
5) You don't need global values for die1 & die2.

That should hold you for a while. There's more to do, but let's see how far you get with this...

Ok here is the updated code now -

#include<iostream>
#include<ctime>      // for the time() function
#include<cstdlib>    // for the srand() and rand() functions
using namespace std;
int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int Dice ()
{
    // set the seed
    srand(time(0));
    // roll the first die
    die1 = (rand() % 6 ) + 1;
    // roll the second die
    die2 = (rand() % 6 ) + 1;
}
int compGame()
{
    Dice ();
    cout << "The computer rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
    cout << "Total = " << die1 + die2 << endl;
    return (die1 + die2);
}
int userGame()
{
    cout << " User turn --- Press 2 to roll";
    cin >> userInput;
    if ( userInput == 2 )
    {
        Dice ();
        cout << "The user rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
        cout << "Total = " << die1 + die2 << endl;
    }
    else {
        cout << "Wrong input. Try again";
        //userGame();
    }

    return (die1 + die2);
}
int main ()
{
    compGame();
    userGame();


    bool value = true;
    while (value)
    {

        if (compGame() == 7  || compGame() == 11)
        {
            cout <<"Computer won" << endl;
            value = false;
        }
        else if (compGame() == 2)
        {
            cout << "Computer lost" << endl;
            value = false;
        }
        else if (userGame() == 7 || userGame() == 11)
        {
            cout <<"User won" << endl;
            value = false;
        }
        else if (userGame() == 2)
        {
            cout << "User lost" << endl;
            value = false;
        }
        else
        {
            cout << " Temp text goes here. Will think late" << endl;

        }

    }

    return 0;
}

The output is coming out fine. Though, what I expect it to be is. first the computer rolls, then the user..and then again computer and then user and so on, untill there is a winner or looser.

What am i doing wrong?

Thanks for all the help.

@WaltP

Actually, it should be

Player 1 rolls the dice.
Output shown as - Dice1 = , Dice2 = and Total (Dice1 + Dice2) =
Player 2 rolls the dice
Output shown as - Dice1 = , Dice2 = and Total (Dice1 + Dice2) =

Now the comparison starts, if either of them won (by getting 7 or 11 as the total). The game stops. If either of them loose by getting a 2 as total, the same stops. if not the game continues.

And no the values are not lost. the int varibales are define outside of Dice and I am returning it in the userGame and compGame.

Is there something wrong am i doing?

1) srand() can only be called once and expect it to work correctly. Move it to the beginning of main()

Ah I knew I was forgetting something :)

first the computer rolls, then the user..and then again computer and then user and so >on, untill there is a winner or looser.

include the functions inside the loop with after each checking the results...
then break the loop if the winner is found

and instead of a break statement change the value of the boolean to stop the loop

then again nothing's wrong in using a break ( sorry for letting my preference cloud my judgement)

include the functions inside the loop with after each checking the results...
then break the loop if the winner is found

you mean like this

while ( compGame(), userGame() ) 

?

no, not in the condition but in the body

but first things first resolve all of the issues numbered by WaitP

it should look something like

while no winner
    compturn()
    check if computer wins
    userturn()
    check if user wins
end while

but before that you need to resolve the issue with srand() position, variable scope, and returning values for the program to properly work

which body ? inside main or in the loop?

In both the cases, the output is not showing up fine. It needs to be turn by turn. But for some reason its not.

Try running the code.

Well, here is the updated code now

#include<iostream>
#include<ctime>      // for the time() function
#include<cstdlib>    // for the srand() and rand() functions
using namespace std;
int compInput;
int userInput;
int die1 = 0;
int die2 = 0;
int Dice ()
{

    // roll the first die
    die1 = (rand() % 6 ) + 1;
    // roll the second die
    die2 = (rand() % 6 ) + 1;


}
int compGame()
{
    Dice ();
    cout << "The computer rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
    cout << "Total = " << die1 + die2 << endl;
    return (die1 + die2);
}
int userGame()
{
    cout << " User turn --- Press 2 to roll" << endl;
    cin >> userInput;
    if ( userInput == 2 )
    {
        Dice ();
        cout << "The user rolled        Dice 1 =" << die1 << " and Dice 2 = " << die2 << endl;
        cout << "Total = " << die1 + die2 << endl;
    }
    else {
        cout << "Wrong input. Try again";
        //userGame();
    }

    return (die1 + die2);
}
int main ()
{
    // set the seed
    srand(time(0));

    bool value = true;

    while (value)
    {
        compGame();
        userGame();


        if (compGame() == 7  || compGame() == 11)
        {
            cout <<"Computer won" << endl;
            value = false;
        }
        else if (compGame() == 2)
        {
            cout << "Computer lost" << endl;
            value = false;
        }
        else if (userGame() == 7 || userGame() == 11)
        {
            cout <<"User won" << endl;
            value = false;
        }
        else if (userGame() == 2)
        {
            cout << "User lost" << endl;
            value = false;
        }
        else
        {


        }

    }

    return 0;
}

I am returning the value, have placed the srand as mentioned ( i now know why). But now if I put int die1 and die2 in Dice(), how can I call it or get the value in compGame and userGame ?

Also, they way you mentione to do the while loop. I think there is a problem, both the players (computer and user) should roll there chances and only after the winner/looser should be checked. If not satisfied, then the round is played again.

Thanks

But now if I put int die1 and die2 in Dice(), how can I call it or get the value in >compGame and userGame ?

declare the die1 and 2 variables in compGame and userGame then pass by reference it to the Dice function

Is there a reason the user must enter 2 to roll? What's wrong with 1 or R? Or even ENTER? Better yet why can't the program just roll?

Let check the code. Doesn't the function compgame() actually roll the dice? So...

{
    compGame();                 // roll the dice once.
    userGame();

    if (compGame() == 7 || compGame() == 11)
    {                           // Dice rolled 2 more times
        cout <<"Computer won" << endl;
        value = false;
    }
    else if (compGame() == 2)   // dice rolled another time
    {
        cout << "Computer lost" << endl;
        value = false;

How many times is one player supposed to roll the dice in his turn? I count 4 for player comp... He cheats! :o)
Looks like you need to rethink the game play.

Yeah, i didnt know that. New to c++ :P.

Fixed it now.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.