C++ and tic tac toe

Started by
15 comments, last by doyleman 18 years, 11 months ago
Hehe, randomize a set variable. Read that again carefully. ;)

Now then, here's what you need to do to create random integers, if that's what you're asking.

#include <ctime> //For time()
#include <cstdlib> //For //rand()

Now, make this function call ONCE at the beginning of your program.
srand(time(NULL));
This seeds the random number generator. You can read up more on this, but this is basically all you need to get the most "random" numbers possible.

Now, here's how you can get a random integer:

int a = rand() % 5;
This set a to a random number between 0 and 4.

int a = 1+ rand() % 5;
This will set a to a random number between 1 and 5.

So you need:
int a = 1+ rand() % 3 to get you a random number between 1 and 3.
There are some things so stupid that only an intellect could believe them.
Advertisement
Yeah, i make things way more confuzing then they have to be and make awkward sentances (so my english teacher sais also lol).. sorry bout that :)

So the % sign does the randomization, i remember doing a tutorial on a odd/even thing with numbers, where it asked for a number input and told whether it was odd or even, and it dealt with using the % key for getting a remainder, and checking the number.
thank you for the quick reply!
Not a problem, the % is called the modulus operator and finding out even/odd is a common use of it. Good luck. By the way, I made a console based tic tac toe game early in my adventures in C++. It does use a class, so it's a little object-oriented, but don't take it for expert coding, because it's not. It doesn't have AI so it's only two player, but I reckon the 1 player mode could be added easily. Let me know if you need the code to help you out.
There are some things so stupid that only an intellect could believe them.
The % sign is the modulus operator. It is used to get the remainder of integer division. With that, you could tell if a number is odd or even.
I'll try for atleast a day on tictactoe, *atleast, i often have hard determination to try harder*, but when it gets to bother me, I guess I could use your game as a source for help, thanks :)
Try for more than that. I remember when it was difficult to make these relatively simple programs with C++ when I was learning. Be creative in how you solve the problems you encounter, there's no right or wrong way, even though there is probably a best. Given the current situation, you're not looking for the best method.
There are some things so stupid that only an intellect could believe them.
ha, yeah, ok. 'with practice, comes perfection...'~not sure who said that at one point in time.
I'll work on this *right now actually*
to be completely honest...i start C++ on saturday, around 9 at night, so i've spent a total of around 6 or seven hours with it. I say that because I guess I need to realize to myself that i am infact new to the language and need to do a 'latter' method of working off little, and work myself up later.
I'm glad that my friend who actually got me into C++ (and helped set up Borland's free compiler, which was a pain to do) directed me here if i need help, though I'll try doing things for awhile before asking for help.
well, i got to go program 'tic tac toe' or something now ;), thanks to everyone who helped *i like thanking people...*

This topic is closed to new replies.

Advertisement