Craps game homework assignment

Dear Cplusplus forums:

First and foremost, I did read the stickied post at the beginning, and I will NOT be asking for the answer to this homework problem. I enrolled in a programming class because I genuinely want to learn the material. That being said, any hints as to which sections, subjects, topics, or any specifics I should be looking at at, focusing on, or re-reading in my book would be appreciated. Unfortunately, my professor has been handing out homework assignments and lab exercises without going over the relevant material during the class lectures, so I am more than just a little confused and overwhelmed. I've got through the previous 5 homework assignments by asking friends, searching forums, and watching video tutorials, however they proved to be very time consuming, and they didn't always answer my questions directly. I managed through the previous 5 assignments, but judging by the complexity of this assignment, I'd like to take a more direct approach and get some helpful hints and suggestions from people who really know what they are doing, as well as hopefully understand my specific problem. I will include a link to a scanned image of the homework assignment for reference.

link to the homework assignment image: http://i39.tinypic.com/r720yt.jpg

My specific questions are which elements of intro-C++ should I be using in the program? I realize this is a broad question, but the professor hasn't provided the chapter that this homework assignment references, nor the specific element we should be learning how to use by completing this assignment (i.e., last homework assignment was about using arrays, but it never said anything about it in the assignment prompt).

Forgive me for the lack of a precise request or question, but as you can see, I am at a bit of a loss when it comes to where to start. I figured that if I started broad, and I had a few responses that asked for more specifics, I could reply with those specifics, and we could narrow it down from there.

Once again, I am not looking for someone to complete my homework assignment for me, I want to be able to accomplish this task on my own, however I am in desperate need of a push or nudge in the right direction, and maybe some hints along the way.

Thank you in advance,

Spectre
Thank you so much for the extremely fast response! I will be checking those links out, and I will post back here with my progress, or if I run into any other problems!

Thanks again!
Welcome to the forum! By craps I assume you're talking about the dice game. I would look into using <time.h> and rand.
http://cplusplus.com/reference/clibrary/cstdlib/srand/
That's my two cents.

edit: Archaic you beat me by, like, 2 seonds. lol
Last edited on
2 seconds? 3 minutes...

@spectre
+1 for not expecting a full answer.
+1 for repeating that.
+1 for joining these forums.
+1 for recognizing the potential ambiguity of the question.
+1 for extra credit.
-5 for joining these forums. :D

In case you have trouble formulating your problem, then here's a general structure I recommend:
One function to generate the random number.
One function to check the random number's value.
Main.

Also, you will want to #include <ctime> and plug time() into a function called srand() in your main. This makes your random numbers really random. ;)
http://cplusplus.com/reference/clibrary/ctime/time/
Don't worry about the type, time() returns an int, regardless of what the docs say. You'll worry about what the docs say about that later.

EDIT: if you do not do the above, you will get a set of random numbers that is identical each time. Use srand() only once, by the way: there's no need to do it several times.

-Albatross

Last edited on
@Albatross:

Thanks for the <ctime> library idea and the srand() function. That's exactly what was happening, I couldn't figure out why I was rolling 10 every single time!!

After looking through all the material you guys (and girls, who knows) suggested, I believe I came up with something "correct." I use air-quotes for correct because my Professor's grading values are not always logical. However, the program I wrote builds in Visual Studio without errors, and when I start it, it functions the way the test data at the bottom of the homework prompt appears. I'm going to post it, and if you could possibly take a look and tell me what you think, or comment if I made some errors or anything like that, I would appreciate it!
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
// CS 120 
// Assignment 6
// Purpose: To play a variation of the game Craps.

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

using namespace std;

int rollDice();

int die1;
int die2;
int rollSum;
int sum;
int rollPoint;
string status;

int main()
{
	sum = rollDice();

	switch (sum)
	{
	case 2:
	case 3:
	case 12:
		status = "lose";
		break;
	case 7:
	case 11:
		status = "win";
		break;
	default:
		status = "point";
		rollPoint = sum;
		break;
	}
	
	cout << "You rolled " << die1 << " + " << die2 << " = " << sum << endl;
	
	while (status == "point")
	{
		cout << "point is " << rollPoint << endl;

		sum = rollDice();

		cout << "You rolled " << die1 << " + " << die2 << " = " << sum << endl; 
		
		if (sum == rollPoint)
			status = "win";
		else if (sum == 7)
			status = "lose";
		else
			status = "point";
	}
	
	if (status == "win")
		cout << "You win!" << endl;
	if (status == "lose")
		cout << "You lose!" << endl;


	return 0;
}
int rollDice()
{
	die1 = (rand() + time(0)) % 6 + 1;
	die2 = (rand() + time(0)) % 6 + 1;
	rollSum = (die1 + die2);
	
	return rollSum;
}
@Albatross
2 seconds, 3 minutes, meh. :D
btw, I'm disappointed I missed the messaging in code int the lounge. Gave me a good laugh.
For spectre: if you don't know what we're talking about:
http://cplusplus.com/forum/lounge/23263/page5.html

I wonder if I should start a completely new thread in which people can only speak in code. Probably I'll start of with some remark that's sure to get people talking. Hmm... I wonder...

@spectre's code:
You could use ints as your status, no? And you could remove the first switch statement and integrate it into your loop, and integrate all your printing into the switch statement as well. It makes your code shorter....

-Albatross
I would move your variables into main. In this scenario there really isn't any reason to have them all be global. Then pass die1 and die2 to rollDice() by reference.

Also, the link to your assignment asks for a second function which determines whether the play was a win, lose, or point. That's an easy enough fix.
@Albatross:

Ok lol I was wondering what you meant about the "messaging in code" in the lounge.

@Archaic:

Good call on the second function. I had just about filed that one as complete. Do you think that means just wrap up my switch statement which checks the case into a function, and run that below my first "sum = rollDice()" function int he main?

Also, do you mean pass the info by reference (die1 and die2 to rollDice()) by the nifty &-ampersand mechanic? My professor only spent a moment discussing it, so I will need to research it a little further.

Thanks!!
Regarding the second function, you can just copy your switch statements into the the new function and then have them return the current status as a string. Just remember to pass sum to the new function.

And yes, the ampersand is for pass by reference. Details can be found here:
http://cplusplus.com/doc/tutorial/functions2/
Oh boy...

So in an effort to reformat the code to use a second function (determineResults) and to have that pass an argument of the sum of the two dice, and have it determine if the play is a win, lose, etc, I broke it. It was going so well too..

Here's what it has mutated into, and I'm lost. I don't think I quite grasp the concept of functions passing arguments. Any help, hints, or guidance would be appreciated:

***EDIT***

I played around with the pass by reference concept, and I think I understand it. I seemed to have got it to build and run, however I am not 100% solid on the concept. If you could please take a look and give me some notes on what I've written, I'd appreciate it:

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
// Assignment 6
// Purpose: To play a variation of the game Craps.

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


using namespace std;

void rollDice(int& die1, int& die2, int& rollSum);
void determineResults(int& status, int& rollPoint, int& sum);

int main()
{
	int sum;
	int rollPoint;
	int status;
	int dieRoll1, dieRoll2;
	
	rollDice(dieRoll1, dieRoll2, sum);

	determineResults(status, rollPoint, sum);

	cout << "You rolled " << dieRoll1 << " + " << dieRoll2 << " = " << sum << endl;
	
	while (status == 3)
	{
		cout << "point is " << rollPoint << endl;

		rollDice(dieRoll1, dieRoll2, sum);

		cout << "You rolled " << dieRoll1 << " + " << dieRoll2 << " = " << sum << endl; 
		
		if (sum == rollPoint)
			status = 1;
		else if (sum == 7)
			status = 2;
		else
			status =3;
	}
	
	if (status == 1)
		cout << "You win!" << endl;
	if (status == 2)
		cout << "You lose!" << endl;


	return 0;
}
void rollDice(int& die1, int& die2, int& rollSum)
{
	die1 = (rand() + time(0)) % 6 + 1;
	die2 = (rand() + time(0)) % 6 + 1;
	rollSum = (die1 + die2);
	
}
void determineResults(int& status, int& rollPoint, int& sum)
{
	switch (sum)
	{
	case 2:
	case 3:
	case 12:
		status = 2;
		break;
	case 7:
	case 11:
		status = 1;
		break;
	default:
		status = 3;
		rollPoint = sum;
		break;
	}
}
Last edited on
There are a couple of things going wrong here.

1) Your function declarations and function definitions do not match. You have no arguments being passed in the definition, but have arguments being passed in the declaration. This will generate an error.

2) When you call the function you aren't passing any arguments, but you have the function taking arguments in the declaration.

3) When you call the functions they are returning a value, but nothing is being assigned that value. You need something like:
sum = rollDice();

4) You do not need to pass rollSum by reference to the rollDice function if that's the value you are returning.

5) Same goes for status in the determineResults function.

6) Since you want to use the sum and rollPoint variables from the main function in your determineResults function, they needed to be passed. The way you currently have them set up the sum and rollPoint in main are unaffected by what's being done to the sum and rollPoint in determineResults. Since you're not changing sum, you can just pass it by value. In regards to rollPoint, the values you are assigning to it you want to reflect to the rollPoint in main, so that must be passed by reference.



I want to say thank you, again (do I sound like a broken record yet?), to everyone who has chimed in. I don't even get this kind of input or response from my professor, so to have this sort of limited interactivity is very nice.

I updated the second code-post I made in effort to save on re-posting code over and over again, so if you'll notice, there's an edit on the post above Archaic's last post, and underneath contains updated code based on some of his suggestions, as well as after I read a section further along in my book with a decent example about argument passing.

Please take a look at it for me and any suggestions are appreciated!

Everything looks good, but there is still a small issue with determineResults. Since sum isn't being changed in the body of determineResults, there is no reason to pass it by reference. So you can simply just remove the ampersand for sum in both the definition and the declaration of determineResults.
Topic archived. No new replies allowed.