dice game

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;

class Dice
{
private:
int face_value;
public:
Dice(): face_value(0)
{ }

void roll_dice(int& score)
{
srand (time(NULL));
face_value = rand() % 6 + 1;
score += face_value;
}
void print_dice()
{
switch(face_value)
{
case 1:
cout <<" ----- " <<endl;
cout <<"| |" <<endl;
cout <<"| O |" <<endl;
cout <<"| |" <<endl;
cout <<" ----- " <<endl;
break;
case 2:
cout <<" ----- " <<endl;
cout <<"| O|" <<endl;
cout <<"| |" <<endl;
cout <<"|O |" <<endl;
cout <<" ----- " <<endl;
break;
case 3:
cout <<" ----- " <<endl;
cout <<"| O|" <<endl;
cout <<"| O |" <<endl;
cout <<"|O |" <<endl;
cout <<" ----- " <<endl;
break;
case 4:
cout <<" ----- " <<endl;
cout <<"|O O|" <<endl;
cout <<"| |" <<endl;
cout <<"|O O|" <<endl;
cout <<" ----- " <<endl;
break;
case 5:
cout <<" ----- " <<endl;
cout <<"|O O|" <<endl;
cout <<"| O |" <<endl;
cout <<"|O O|" <<endl;
cout <<" ----- " <<endl;
break;
case 6:
cout <<" ----- " <<endl;
cout <<"|O O O|" <<endl;
cout <<"| |" <<endl;
cout <<"|O O O|" <<endl;
cout <<" ----- " <<endl;
break;
}
}
};

void display_result (int user, int comp);

int main()
{
Dice user, computer;
int count = 0;
int user_score = 0;
int comp_score = 0;
char keypress;
cout << "Welcome to the C++ dice game!" <<endl;
cout << "You are palying against the computer. Each player gets to roll 2 dices a total " <<endl;
cout << "of 6 times. The player with the highest total of dice values will win! " <<endl;

do
{
cout << "Are you ready to roll?" << endl;
cout << "Press ENTER key to continue..." <<endl;
keypress = getche();

user.roll_dice(user_score);
user.print_dice();
user.roll_dice(user_score);
user.print_dice();

cout <<"Now the computer's turm." << endl;
cout <<"Press ENTER key to continue..." <<endl;
keypress = getche();

computer.roll_dice(comp_score);
computer.print_dice();
computer.roll_dice(comp_score);
computer.print_dice();

count += 1;

}
while (count <= 6);

display_result ( user_score, comp_score);
system ("pause");
return 0;
}
void display_result( int user, int comp)
{
if (user > comp)
cout <<"You are the winner!" <<endl;
else if (user < comp)
cout <<"You lose!" <<endl;
else if (user= comp)
cout <<"You have the same score as the computer!" <<endl;

cout <<"Your score is: " << user <<endl;
cout <<"The computer's score is: " << comp <<endl;
}





please help! every time the roll_dice function get call twice for the same object, i receive the same dice values.
Please help! By using code tags...

I think you do not want to set srand everytime that function is called. You need only set srand once in the entire program.
Pay attention to the statement

"Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand."

from the link http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand .

Even though time(NULL) create a distinctive number as seed passed to srand() that creates a random number generator, however, since your program runs so fast, the seed returned by time(NULL) doesn't
get changed in sequential calls to srand().

time(NULL) might get changed every other second, but your code runs much faster than that.

Therefore, two calls to rand() will give you the same random number for both dices in a throw, and that should explain why you always got the same dice value.

So in short, seed ( created by call to time(NULL) ) is the same to both srand() calls, therefore, two rand() calls will give you the same random number.











Therefore, two calls to rand() will give you the same random number for both dices in a throw, and that should explain why you always got the same dice value.

So in short, seed ( created by call to time(NULL) ) is the same to both srand() calls, therefore, two rand() calls will give you the same random number.


This is not entirely correct. As long as you don't change that seed, you will get a set of (fairly) random numbers. For example:
1
2
3
4
5
6
7
srand(time(0)); 
int x, r; 
for(x = 0; x < 10; ++x)
{
    r = rand() % 6; 
    std::cout << r << "\n"; 
}


This will give a selection of (fairly) random numbers. But if you keep resetting the seed.
1
2
3
4
5
6
7
8

int x, r; 
for(x = 0; x < 10; ++x)
{
    srand(time(0)); 
    r = rand() % 6; 
    std::cout << r << "\n"; 
}


This will not give you random numbers, but the same number each time.
Mats, this is the exactly what I have meant. What I said was in the context of beginnervn's example above, I probably should have said "the very first rand() call" after calling srand() with a fixed seed will always produce a fixed random number.

Your example should help beginnervn understand a pair of rand() and srand() better.






Topic archived. No new replies allowed.