Jump to content
  • Advertisement
Sign in to follow this  
Just_starting

Brand new

This topic is 5460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First and foremost I would like to say hello. This is my first post. I am a C++ student at Southern Illinois Univrsity (EDWSV). I have been programming for 4 weeks now and this is my first taste of any langauge. With that said I think this is a GREAT site and plan on being an active member. As a C++ student my first assingment due last friday was a Video Craps game. I have not received a grade yet but I was confident submitting it. Here is the code (Any and all feedback will be taken as constructive so please be as brutal as needed) On a side note if I wanted to add a sound effect instead of the laim ding "\a" any advice would be appreciated
// CS140 section: 003 
// File: P1.cpp
#include <iostream>
#include <cstdlib> 
#include <ctime>
using namespace std;


void main()
{
	int value1,
		value2,
		sum1,
		pointvalue;
	char yorn;
	cout << "      Welcome to hours of nonstop fun for the whole family!\n\n";
	cout << "                  SUPER-FANTASTIC CRAPS\n\n"; 
	do 
	{
		srand((unsigned)time(NULL));
		cout << " The game is about to begin, pressing any key will roll the dice.\n\n";
		system ("pause");
		value1 = (rand() % 6 + 1);
		value2 = (rand() % 6 + 1);
		sum1 = value1 + value2;
		cout << "\nYou Roll a:  " << sum1 << endl;
		if ((sum1 == 7) || (sum1 == 11))
			cout << "\nHey Hey it is Your Lucky Day You Win\a\n";
		else if ((sum1 <= 3) || (sum1 == 12))
			cout << "\nYou Lose\n";
		else if (((sum1 >= 4) && (sum1 <= 6)) || ((sum1 >= 8) && (sum1 <= 10)))
		{
			pointvalue = value1 + value2;
			cout << "\nRoll Again\n\n";
			system ("pause");
			value1 = (rand() % 6 + 1);
			value2 = (rand() % 6 + 1);
			sum1 = value1 + value2;
			while ((sum1 != pointvalue) && (sum1 != 7))
			{
				value1 = (rand() % 6 + 1);
				value2 = (rand() % 6 + 1);
				sum1 = value1 + value2;
				cout << "\nYou Roll a: " << sum1 << endl << endl;
				system ("pause");
			}
			if (sum1 == pointvalue)
			cout <<"\nYou Win!\a\n";
			if (sum1 == 7)
			cout <<"\nYou Lose!\n";
		}
		cout << "\nDo you want to play again?\nEnter a Y or N:";
		cin >> yorn;
		while (((yorn != 'y') && (yorn != 'Y')) && ((yorn != 'n') && (yorn != 'N')))
		{
			cout << "\a\nThat is not a Y or a N, please try again.";
			cin >> yorn;
		}	
	}while ((yorn == 'y') || (yorn == 'Y'));
	
	

}

[Edit: Did the source code formatting for you. Click the "Edit" button in the upper-right corner of your post to see the difference. - Oluseyi] Thanks a bunch! [Edited by - Oluseyi on October 5, 2004 11:55:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
To format code "nicely", put it between [ source ] and [ /source ] tags (remove the spaces).
Unfortunately, you can't add sound to "plain" C/C++. You may be able to get some libraries to do it reasonably easily, but I don't know of any.
If you're using windows, you can do #include <windows.h> and do: PlaySound("MySound.wav",NULL,SND_FILENAME | SND_ASYNC);

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
If you're using windows, you can do #include <windows.h> and do: PlaySound("MySound.wav",NULL,SND_FILENAME | SND_ASYNC);


I am very new would anyone mind explaing this a little. I understand #include <windows.h> includes windows in my program.

I am assuming Playsound is a function using the information inside the brackets. But I do not know what that means or does.

Again Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
To format code "nicely", put it between [ source ] and [ /source ] tags (remove the spaces).
Use the HTML Latin-1 character code &#91; (as in &#91;source]) to write [source] without actually formatting your code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Just_starting
I am very new would anyone mind explaing this a little. I understand #include <windows.h> includes windows in my program.

I am assuming Playsound is a function using the information inside the brackets. But I do not know what that means or does.

Again Thanks for the help.
First off, welcome.

The majority of programming languages originated in character console environments, meaning that they have no native concept of things like mouse pointers, loudspeakers and so forth. To interact with such devices, one has to use Application Programming Interfaces (APIs) provided by either operating system vendors (eg with Windows) or device vendors (less common) to invoke functionality.

This is actually a complex subject that is dependent on a lot of other foundational issues. My recommendation at this point (as far as multimedia and C or C++) is to be patient and learn how to program first. You're already off to a good start: the code you posted was well-formed, elegant and functional. As you become more comfortable with language constructs, take advantage of the many, many resources available here to learn about different aspects of programming and game development.

Above all, have fun!

Share this post


Link to post
Share on other sites
Thanks for the input. I am currently working on a game called Snowball Fight. Each player gets 10 hitpoints and the ability to block and aim. This is a simple exercise in using functions, I will post for any crit. here after I submit it on Friday.

Having patience is a virtue! Having a new toy is pure anxiety!

Have a good one and thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by Just_starting
Quote:
Original post by Evil Steve
If you're using windows, you can do #include <windows.h> and do: PlaySound("MySound.wav",NULL,SND_FILENAME | SND_ASYNC);


I am very new would anyone mind explaing this a little. I understand #include <windows.h> includes windows in my program.

I am assuming Playsound is a function using the information inside the brackets. But I do not know what that means or does.

Again Thanks for the help.
Yup, more or less. The #include tells the compiler to read the information in that header file. The windows.h header contains most of the core stuff you need for windows programming.
As for the parameters to PlaySound(), the first is the filename of the sound effect to play, and don't worry about the other two, they're specific to the command (If you want to know, the second parameter is a handle to the program containing the sound to play (NULL means its in a file, not a program), and the third is a set of flags telling PlaySound() what to do with the sound (Load it as a file, and play it in the background (asynchronously))). As Oluseyi said, its probably a bit advanced anyway.

Quote:
Original post by Oluseyi
Use the HTML Latin-1 character code [ (as in [source]) to write [source] without actually formatting your code.
Nice, thanks

Share this post


Link to post
Share on other sites
My first reaction was to chew you out for posting homework... of course this is something you've already completed so that's fine :)

So, let's see what we have here...


// CS140 section: 003
// File: P1.cpp


I assume you are stuck with that file name :)


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


All good so far. Give your prof a pat on the back, and/or your textbook author a positive review on Amazon, or something.


void main()


Nope. main() is supposed to return int (although as a special case, you don't actually have to tell it what to return). Your compiler may accept this, but it's technically not supposed to.
(In case you are wondering, the value returned from main is supposed to be a status code indicating what happened on that program run; it defaults to 0, for "successful run, no error". This is used primarily for batch file scripting or other forms of "gluing" programs together.)


int value1, value2, sum1, pointvalue;


You definitely don't need that many temporaries, but beginner's code usually looks like that... you'll get the hang of it with experience and that's all there really is to it.


char yorn;


Lol. I had to stare at that for a while to figure out what a yorn was. ;) Using capitalization, or even underscores in a variable name is not a sin.


cout << " Welcome to hours of nonstop fun for the whole family!\n\n";
cout << " SUPER-FANTASTIC CRAPS\n\n";


I'm not so sure you should be encouraging little Junior to gamble. But more importantly ;) , '\n's don't by themselves really guarantee that your output will appear.

The iostream header defines a special object 'endl', which when output (via that << operator) will (a) append a newline and (b) flush the output. You can also use another object 'flush', to just ensure the output appears without a newline.

Usually you will be ok (the output will appear eventually, either because the buffer fills or something else triggers it), but this sort of thing can lead to long, aggravating debugging sessions (especially if you like to use print statements to debug, as I do).


do
{
srand((unsigned)time(NULL));


No no no no no no no no no no.

Take your srand out of the loop. Otherwise you don't really get random values. The only reason you're getting different values each time, right now, is that your loop takes over a second to execute - and that's only because you're waiting for user input.


cout << " The game is about to begin, pressing any key will roll the dice.\n\n";
system ("pause");


Please don't use "system" if there's any other sane way of doing things. It depends on

a) the 'pause' program actually being there (in the user's path).
b) it actually doing what you want (it could be substituted for anything else).

Doing a cin into a dummy char value should be sufficient, although the user will have to hit return (because the input is line-buffered).


value1 = (rand() % 6 + 1);
value2 = (rand() % 6 + 1);
sum1 = value1 + value2;


This part could be a function. You do it a couple different places. But then, it's really just a single expression (you don't need value1 or value2, as I'll demonstrate), so whatever. Oh, since the user never sees the individual die rolls, you don't need to add 1 two separate times, you can just add 2 ;) (Of course, if you made a function to roll a *single* die, then you'd be stuck. And such a function might be useful for more complicated games.)


cout << "\nYou Roll a: " << sum1 << endl;
if ((sum1 == 7) || (sum1 == 11))
cout << "\nHey Hey it is Your Lucky Day You Win\a\n";
else if ((sum1 <= 3) || (sum1 == 12))
cout << "\nYou Lose\n";
else if (((sum1 >= 4) && (sum1 <= 6)) || ((sum1 >= 8) && (sum1 <= 10)))


Don't repeat yourself. For the last case, just use 'else', which implies all the other cases. If you really don't trust that you'll only ever get values 2-12 inclusive rolled, then you should use an assertion.


{
pointvalue = value1 + value2;


Here's why we don't need value1 or value2; this is the only place they're used except in the die rolls, and at this point I think you can convince yourself that the sum is always equal to 'sum'. (Oh, and out of 'sum', 'sum1' and 'pointvalue', you should only need two variables.)


cout << "\nRoll Again\n\n";
system ("pause");
value1 = (rand() % 6 + 1);
value2 = (rand() % 6 + 1);
sum1 = value1 + value2;
while ((sum1 != pointvalue) && (sum1 != 7))
{
value1 = (rand() % 6 + 1);
value2 = (rand() % 6 + 1);
sum1 = value1 + value2;
cout << "\nYou Roll a: " << sum1 << endl << endl;
system ("pause");
}


Oops, if you roll a 7 or the point on your first attempt to make the point (outside the loop), it will skip right by the loop, and the user won't see the "You Roll a: " message for that roll - just "Roll Again\nYou Win/Lose!". You really want another do-while loop here, because it's another situation where you know you want to roll at least once.


if (sum1 == pointvalue)
cout <<"\nYou Win!\a\n";
if (sum1 == 7)
cout <<"\nYou Lose!\n";
}
cout << "\nDo you want to play again?\nEnter a Y or N:";
cin >> yorn;
while (((yorn != 'y') && (yorn != 'Y')) && ((yorn != 'n') && (yorn != 'N')))
{
cout << "\a\nThat is not a Y or a N, please try again.";
cin >> yorn;
}
}while ((yorn == 'y') || (yorn == 'Y'));
}


Requiring the user to input something valid is perhaps a bit much - most people will allow garbage to 'default' to either yes or no (and indicate which via the prompt, e.g. "Play again? (Y/n)"). It's not really wrong though.

What is wrong: Try answering the prompt with "yyyyy", or "yes", and see what happens. Oops, the program can't proceed until it sees a newline, but it only reads one character of input - what happens with the rest? Why, it stays buffered, of course. Which is really really convenient for working with files, but needs to be taken care of for interactive input. Look up the 'ignore()' method of cin.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!