Pig in C++

Hi, having issues with my Pig game. Had a search of other user's work and theirs is completely different from mine, so... here it is.

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

int main ( void )
{
	using std::cout;
	using std::cin;
	using std::endl;
	using std::rand;
	using std::srand;
	using std::time;
	using std::string;

	string player1;
	string player2;

	int p1score = 0;
	int p2score = 0;
	int dieResult;
	int roll = 1;
	int stand = 2;

	srand(time(0));
	dieResult = rand() % (6 - 1 + 1) + 1;


	cout << "Welcome to Pigs!  The simple rule of this game is to roll"	<< endl
		<< "the die and finish on a total score of 100 or more points."	<< endl	<< endl;


	cout << "Player 1, please enter your name: ";
	cin >> player1;
	cout << "Welcome, " << player1 << "."<< endl << endl;
	cout << "Player 2, please enter your name: "; 
	cin >> player2;
	cout << "Welcome, " << player2 << "."<< endl << endl;
	cout << "Let's begin!" << endl << endl;
	
	
	cout << "Your turn, "	 << player1 << ".  Enter 1 to roll" << endl;
	cin >> roll;
	cout << dieResult << endl;

	while ( p1score < 100 || p2score < 100 )
	if ( dieResult == 1 )
	{
		cout << "You rolled a 1.  Bad luck " << player1 <<", it's " << player2 << "'s turn.";
	}
	else if (dieResult > 1)
	{
		cout << "You rolled a " <<dieResult <<".  Press 1 to roll again" << endl
			<< " or 2 to stand.";
		cin >> roll;
		cout << dieResult << endl;
	}
	return 0;
}


The main issue I'm having is that when the die is rolled and the user is prompted to roll again, it doesn't reroll a brand new number, but states the previously rolled 'dieResult'. For example, if you roll a 2 and roll again, it continuously rolls 2.
You need to reassign the value of dieResult to the random value above.

The way rand() works is takes a bunch of seemingly random integers (based upon the seed you input). Since an int can be rather large, it doesn't do us any good, we specify the range we want. This is where the modulus operator, %, comes in handy. It does integer division on the "random" number and returns the remainder.

Looking at your example, we're taking a random number, dividing it by 6 and returning a number 0-5. You add the 1 to simulate a real dice roll. This value then get's assigned to dieResult. It never changes since you never change it's value, so you need to "roll" the die again.

Typically, this is going to be placed in the while loop.

I hope this helps.
Perhaps the compiler has something against pigs :D

No really, what you did is that you rolled your dice at the beginning of the game (inputted the random number in dieResult in the beginning of main()), but you didn't roll the dice again on other turns (you didn't modify the variable dieResult!)
I'm a little confused. How would I go abouts modifying the dieResult variable to generate a brand new number after each roll?

EDIT: The code for dieResult > 1, I changed to:

1
2
3
4
5
6
7
8
	else if (dieResult > 1)
	{
		cout << "You rolled a " <<dieResult <<".  Press 1 to roll again" << endl
			<< " or 2 to stand.";
		cin >> roll;
		dieResult = rand() % (6 - 1 + 1) + 1;
		cout << dieResult << endl;
	}


And that seems to be working perfectly, but when I roll a 1, it constantly just continues to say "bad luck" endlessly until I close the console. Any ideas?
Last edited on
The same exact way you assigned it before. This line:
dieResult = rand() % (6 - 1 + 1) + 1;

Which can be simplified to:
dieResult = rand() % 6 + 1;
I think it makes him understand how framing numbers work, I used to that for some things until I get the hang of doing them on the fly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	cout << "Your turn, "	 << player1 << ".  Enter 1 to roll" << endl;
	cin >> roll;
	cout << dieResult << endl;

	while ( p1score < 100 || p2score < 100 )
	if ( dieResult == 1 )
	{
		dieResult = rand() % 6 + 1;
		cout << endl;
		cout << "You rolled a 1.  Bad luck " << player1 <<", it's " << player2 << "'s turn." << endl <<endl;
	}
	else if (dieResult > 1)
	{
		cout << "You rolled a " <<dieResult <<".  Press 1 to roll again" << endl
			<< " or 2 to stand.";
		cin >> roll;
		dieResult = rand() % 6 + 1;
		cout << dieResult << endl;
	}
	return 0;
}


Ok thanks for the help guys and now it looks like that and everyting seems to be working fine...
But when player 1 rolls a 1 and turns to player 2's turn, it auto-rolls for them...
Topic archived. No new replies allowed.