Help with random dice
May 2, 2017 at 9:10pm UTC
Basically, I am making a snakes and ladders game
Every time I run the code, the Rolls and Temp-Spot roll is different, the temp spot should be the addition of the roll, but the dice roll is different for each one, how can I make them the same?
Thank you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int dice()
{
return rand()%6+1;
}
string check(int tempspot, string &message)
{
if (tempspot == 11 || tempspot == 22 || tempspot == 33 || tempspot == 44 || tempspot == 55 || tempspot == 66 || tempspot == 77 || tempspot == 88 || tempspot == 99) {
message = "go to jail" ;
return message;
}
if (sqrt(tempspot) * sqrt(tempspot) == tempspot) {
message = "perfect square" ;
return message;
}
else
return "" ;
}
int main()
{
int sum=0;
string message;
srand(time(NULL));
cout.precision(ios::right);
cout<<"Rolls " ;
cout.precision(ios::left);
cout.width(10);
cout<<"Temp-Spot" ;
cout.precision(ios::right);
cout.width(10);
cout<<"Prize" ;
cout.width(10);
cout<<"Message" ;
do {
cout<<"\n" <<dice();
cout.precision(ios::left);
sum +=dice();
cout.width(10);
cout<<sum<<"\n" ;
cout.width(25);
check(sum, message);
if (message == "perfect square" ) {
cout<<"+10" ;
sum += 10;
}
else if (message == "go to jail" ) {
cout<<"10" ;
sum = 10;
}
cout.width(20);
cout<<message;
}while (sum<=100);
return 0;
}
Last edited on May 2, 2017 at 9:45pm UTC
May 2, 2017 at 9:46pm UTC
> I need help with that if loop
`if' does not a loop.
> but once it works the first time it keeps doing it for the other numbers too,
> is there a way to make this not happen?
1 2 3 4 5 6 7 8 9 10 11
if (tempspot == 11 || tempspot == 22 || tempspot == 33 || tempspot == 44 || tempspot == 55 || tempspot == 66 || tempspot == 77 || tempspot == 88 || tempspot == 99) {
message = "go to jail" ; //compare this
return message;
}
if (sqrt(tempspot) * sqrt(tempspot) == tempspot) {
message = "perfect square" ; //and this
return message;
}
else
return "" ; //against this
¿see the difference?
> I just realized that the dice rolls for temp-spot do not correspond with the
> original roll, how can I change that?
each time you call `dice()' you've got another result.
you call it once to show the value and another time to add it to `sum'. It would be quite weird to expect having the same result both times always.
1 2 3
int dice_roll = dice();
std::cout << dice_roll;
sum += dice_roll;
Topic archived. No new replies allowed.