autopoker, looking good!

Ladies and Gentlemen, I’m now presenting the most precious invention of mankind ever!!!
ok, don’t take it seriously, I finally managed to gather all the code into one file ( it’s now 4 files! ). Now you can take a look and see what I did so far. If you have any questions or you don’t understand some parts, just comment and I’ll get to you :)

here’s the link to download a .txt file
change the extension to .cpp and use it :)

http://pastebin.com/f4e9f01ba

back to our bubbling!

For some time I wasn’t blogging due to some “things” that was going on in my life. Primarily in school! Anyway, the autopoker can now deal the cards, and also is able to detect what you have in your hand ( or each player ) except straight, straight flush, and royal flush. In fact, just the straight one is left. That being finished, the other two ( straight flush && royal flush ) are done.
Now, I wanted to tell you about an interesting thing I learned ( and keep up here and post comments, I will be posting stuff I learn ). It’s a timer. Apparently it’s in “iostream” library or ctime I think. What it does, is that it delays the execution of next command for N seconds/microseconds/nanoseconds depending on you.
This is how you use it:

sleep( N );

where “N” is the number of seconds.
you can also use it this way:

usleep( uN ); //uN is the number of microseconds
nanosleep( nanoN ); //I’m not sure how to actually use this one, but seems to follow other ones. nanoN is the number of nanoseconds.

also, if you want to take the current time for randomization purposes you do:
time( 0 );

which is good for:
srand( time( 0 ) );

I think they are in “ctime” library or “cstdlib” ( cstdlib for randomizing and ctime for time() )

rewriting everything, top to bottom

I finally know how to use classes :D yay, the only trick is that you should put it BEFORE int main() which I didn’t know and I used to put it AFTER int main(). The result? error: no “Something something1;” has not type! where Something is a class and something1 is an object. Anyway, I did it and therefore, I rewrote every single line of autopoker.cpp and also created a new header file player.h for it. Today, however, I was talking to my friend on the phone and he accidentally brought up a good point. He said that I can make a card class and instantiate ( create ) 52 objects of that class. Just think about it, FIFTY TWO OBJECTS!!! O_O that’s a lot and takes a lot of time! I might do that though, but first I try to fix the current one!

God bless OOP

I’m still having difficulty implementing OOP ( stands for Object-Oriented Programming ) into my sources. But if you think about it, OOP makes life much easier and faster! Well it doesn’t make life faster but programming at least. And that, in turn, makes life more “enjoyable” specially when you don’t have compile-time errors as I just had! I don’t know why it just doesn’t work! OOP I mean. And that is exactly why I’m passionately waiting for my book C++ How to Program ( a great book by the way ) to come. Its early classes approach is what I was looking for. I doesn’t really make you wait years ( figuratively ) to know what is a class and what is an object, which are, in fact, what make C++, a real C++. Anyway, I gave up on classes and OOP for now till my book comes and I can finally see how to work efficiently with objects. For now, I think the development of autopoker.cpp ( refer to previous posts && the same as poker.cpp just changed the name ) is in a soft freeze ;) because of the fact that I can’t think of ANY way to continue it without classification ( implementing OOP ). And because I don’t know how to work with objects, therefore, it’s stalled!

first bits of poker.cpp

It’s going well, I’m working on the first bits of it. Some starting points such as just shuffling and dealing a deck of cards.
This is what I’ve got so far:


//put random numbers in slots

#include iostream
#include ctime
#include cstdlib
#include iomanip
using namespace std;

int main ()
{
const int totalCards = 52;
const int deckRows = 4;
const int deckColumns = 13;
int deck[ deckRows ][ deckColumns ] = { 0 };
char *suite[] = { “Hearts”, “Clubs”, “Diamonds”, “Spades” };
char *face[] = { “Ace”, “Two”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”, “Queen”, “King” };
srand( time( 0 ) );

for( int card = 1; card <= totalCards; card++ ) {
int randrow = rand() % deckRows;
int randcolumn = rand() % deckColumns;

while( deck[randrow][randcolumn] != 0 ) {
randrow = rand() % 4;
randcolumn = rand() % 13;
}

deck[randrow][randcolumn] = card;
}

/*
//print the deck
for( int i = 0; i < deckRows; i++ ) {
for( int j = 0; j < deckColumns; j++ ) {
cout << setw( 5 ) << deck[ i ][ j ] << ” “;
}
cout << endl;
}

//check for wrong numbers
for( int x = 0; x < deckRows; x++ ) {
for( int y = 0; y 52 || deck[ x ][ y ] < 1 ) {
cout << “INVALID NUMBER DETECTED IN LOCATION: ” << x << “, ” << y << endl;
}
}
}
*/

for( int card = 1; card < totalCards; card++ ) {
for( int i = 0; i < deckRows; i++ ) {
for( int j = 0; j < deckColumns; j++ ) {
if( deck[ i ][ j ] == card ) {
cout << face[ j ] << ” of ” << suite[ i ] << endl;
}
}
}
}

}
again, add the indentation yourself!

It essentially shuffles a deck of cards and deals all 52 cards and says what they are in order!   If you don’t understand part of the code, just let me know by writing a comment!

put random numbers in slots!

I did it finally! a very easy thing but quite annoying because I didn’t know my mistake and I still don’t know it. Here is the code I was “working on” for the last 2 days:

//put random numbers in slots

#include iostream
#include cstdlib
#include ctime
#include iomanip
using namespace std;

int main ()
{
const int totalCards = 52;
const int arrayRows = 4;
const int arrayColumns = 13;
int array[ arrayRows ][ arrayColumns ] = { 0 };
srand( time( 0 ) );

for( int card = 1; card <= totalCards; card++ ) {
int randrow = rand() % arrayRows;
int randcolumn = rand() % arrayColumns;

while( array[randrow][randcolumn] != 0 ) {
randrow = rand() % 4;
randcolumn = rand() % 13;
}

array[randrow][randcolumn] = card;
}

//print the array
for( int i = 0; i < arrayRows; i++ ) {
for( int j = 0; j < arrayColumns; j++ ) {
cout << setw( 5 ) << array[ i ][ j ] << ” “;
}
cout << endl;
}

//check for wrong numbers
for( int x = 0; x < arrayRows; x++ ) {
for( int y = 0; y 52 || array[ x ][ y ] < 1 ) {
cout << “INVALID NUMBER DETECTED IN LOCATION: ” << x << “, ” << y << endl;
}
}
}
}

Anyway, this doesn’t have any mistakes but it uses a very very very poor algorithm of “retry if it’s taken.” Well, I know that, but I will try to work on a better algorithm for it! Or maybe make it into a bigger thing! Programming is fun, but up to when you don’t have any mistakes. Leave comments if you are thinking of a better algorithm :)
And by the way, I finished my math exam last Thursday, it was easy as always! Looking forward to next semester when I have Physics 12 :D must be better than Physics 11 I had this semester!
Also, indent this code yourself, this blog doesn’t show indentation! and put “” around cstdlib, iomanip, ctime, and iostream!
Don’t forget to comment!