Sign in to follow this  

Poker Coding

This topic is 4196 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

I hope this is the rightr forum i barely have any time and i want an answer when i get back(hopefully) im creating a poker game can anyone lead me or help me with coding the part where the game can read a straight hand 1,2,3,4,5 10,jack,queen,king,ace 3,6,5,4,2 queen,10,jack,ace,king know what i mean? thx

Share this post


Link to post
Share on other sites
Well, i understand that and i am using numerical values for the cards.
and +1 from the previous card will only read it as a straight if the numbers are in a sequential order(1,2,3,4,5).
i need to know how to read (2,5,4,1,3) as a straight..



[Edited by - Bluseed on June 23, 2006 8:38:55 AM]

Share this post


Link to post
Share on other sites
So nevermind my last post i figured it out, i hate when i waste space, i shoulda just stared at it for another hour!! LOL

thx

[Edited by - Bluseed on June 23, 2006 8:51:38 AM]

Share this post


Link to post
Share on other sites
I had same problem with recognizing poker hands. What I did was use some logic...
I checked for flush based hands first, then 4 of a kind, then full house, then 3 of a kind, then 2 pair then pair. After all i basically sorted the array and said if the players hand was none of the above and hand[5] - hand[1] = 4 it was a straight.
This works for every straight except for the wheel (Ace thru 5).
So then I said if none of the above is true, and hand[5] is an ace and hand[4] - hand[1] = 3 then u have the wheel.

If you would like I can paste the code for you...I just have to find it.
If you can think of a better method for doin this let me know.

Share this post


Link to post
Share on other sites
Damn ok i c what you mean by sorting them, can u lead me somewhere that has some examples of how to sort numbers from lowest to highest, i just tried it on my own and i know im going about it the wrong way...

I would love to see some code plz....:)


The method i want to use:
I get random values for CardA,CardB,CardC,CardD,CardE then
i can sort the values from lowest to highest
(sounds so damn easy, im embarrassed) that way i can easily say,

[script]

SortCards(A,B,C,D,E);

if (E == D + 1 && D == C + 1 && C == B + 1 && B == A + 1) {
printf("You got a straight")
}

[/script]

Share this post


Link to post
Share on other sites
You can find sorting code by searching google for things like bubble sort, selection sort, insertion sort etc (or an easier solution is to use the qsort function where the quick sort algorithm is implemented for you with you only needing to give a 'comparison' function... see google).
The problem I'm seeing with your approach and sorting is that you're not using an array (or some other collection structure) to hold your cards.

I'm not sure what level of coding you're up to so I'm not sure if anything we've posted so far makes any sense. And at this point am assuming you're in C or C++ environment.

I suggest you read up on Loops, Arrays and Sorting (in that order). Google should turn up a trillion results for these :)

P.S. You may also want to take a look at Structs coz that'd be a nicer way to store your card data (either structs or classes, depending on if you're in c or c++)

Share this post


Link to post
Share on other sites
Well yah im not too far advanced and im using c++ i guess u can say im trying to figure this all out.
I know im running into a lot of questions myself knowing im doing some things wrong or the hard way, and nopw that you mention it , LOLOLOL , an array would be a lot better wouldnt it then i could just use a FOR statement. I was wondering how you would use a FOR statement with my code but i think that makes sense now. Trial and Error Trial and Error, thx for the tips and help appreciate it... im not totally noob, i use a lot of directX gfx, but really never got down and dirty into the logic portions which im trying to really dive into now.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluseed
Well yah im not too far advanced and im using c++ i guess u can say im trying to figure this all out.
I know im running into a lot of questions myself knowing im doing some things wrong or the hard way, and nopw that you mention it , LOLOLOL , an array would be a lot better wouldnt it then i could just use a FOR statement. I was wondering how you would use a FOR statement with my code but i think that makes sense now. Trial and Error Trial and Error, thx for the tips and help appreciate it... im not totally noob, i use a lot of directX gfx, but really never got down and dirty into the logic portions which im trying to really dive into now.


1) put your cards in a vector; since these cards are numerical values, a vector of int should be enough
2) sort the vector
3) verify is you have a valid combination:
#include <vector>
#include <algorithm>

std::vector<int> hand;
std::sort(hand.begin(), hand.end());
bool straight = true;

for (size_t i=1; i<hand.size(); i++) {
if (hand[i] != hand[i-1]+1) {
straight = false;
break;
}
}


BTW, I know that I'm rather impolite to ask, but can you try to write entire and correct words instead of painfull stuff like 'u' (you), 'im' (I'm) or 'i' (I)? English is not my mother tongue, thus I can have some problems to decipher posts that use such rather annoying AOL-style kind-of-English. Thanks a lot.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
#include <vector>
#include <algorithm>

std::vector<int> hand;
std::sort(hand.begin(), hand.end());
bool straight = true;

for (size_t i=1; i<hand.size(); i++) {
if (hand[i] != hand[i-1]+1) {
straight = false;
break;
}
}



Remember though that aces can be high or low. I'd recommend defaulting them to a value of one and have some special case code such as:


std::vector<int> hand;
std::sort(hand.begin(), hand.end());
bool straight = true;

int startIndex = (hand.back() == 13 && hand.front() == 1) ? 2 : 1;

for(int i=startIndex; i<hand.size(); ++i)
{
if (hand[i] != hand[i-1]+1)
{
straight = false;
break;
}
}

Share this post


Link to post
Share on other sites
- You don't need a vector to sort. std::sort works fine on an array.
- Straights are often allowed "around the corner", not just ace-low. I would keep ace high "normally" (i.e. represent it with the highest enumeration value), and make two attempts to make a straight: first "normally", and then if that fails, take the first card that "failed" and see if it's the top of a "wraparound" straight. (That would require that the top card is an ace and the "failed" card is a specific value from 2,3,4,5 depending on its position.)
- Sorting the cards by rank is also extremely useful for detecting matched cards.
- We don't need to check card by card. Check for pairs/triplets/quads *first*; if there are none, *and* the top and bottom cards differ by 4, we have a straight. Naturally we need a cyclic difference ;)


class Card {
int id;
public:
int rank() { return id % 13; } // mapped to 2..A by printing routine
int suit() { return id / 4; } // mapped to suits by printing routine
bool operator>(const Card& other) {
return (rank() != other.rank()) ? rank() > other.rank() : suit() > other.suit();
}
int wraparound_rank_difference(const Card& other) {
return (rank() - other.rank() + 13) % 13;
}
};
// somewhere else
Card hand[5];
// Usually we sort cards in descending order, which is not the default sorting
// according to std::sort. So we tell it:
std::sort(hand, hand + 5, std::greater<Card>);
bool straight = false;
bool flush = all_suits_match(hand);
matching_cards_result matches = matched_cards(hand);
// that would be written to exploit the sortedness
if (matches == NO_PAIR) {
bool straight = false;
for (int i = 0; i < 5; ++i) {
if (hand[i].wraparound_rank_difference(hand[(i+4) % 5]) == 4) {
straight = true;
break;
}
}
}

Share this post


Link to post
Share on other sites
Okay i see where your going, i tried this and am a little stuck.
I can sort the numbers out but only 1 through 5, maybe im being stupid but shouldnt i be able to plugin any number into Hand[array] and get the series of numbers sorted?


int int_sorter( const void *first_arg, const void *second_arg )
{
int first = *(int*)first_arg;
int second = *(int*)second_arg;

if ( first < second )
{
return -1;
}
else if ( first == second )
{
return 0;
}
else
{
return 1;
}

}

void SortHand( int HandA, int HandB, int HandC, int HandD, int HandE)
{

int Hand[5];
int i;

Hand[0] = HandA; // HandA = 1
Hand[1] = HandB; // HandB = 3
Hand[2] = HandC; // HandB = 2
Hand[3] = HandD; // HandD = 6 WHY NOT 6??
Hand[4] = HandE; // HandE = 4


qsort( Hand, 5 , sizeof( int ), int_sorter );

printf ("\n" );

for ( i = 0; i < 5; ++i )
{
printf ( "%d\n" ,Hand[ i ] );

if (Hand[i] != Hand[i - 1] + 1) {
StrBool = false;
break;
}

if (Hand[i] == Hand[i - 1] + 1) {
StrBool = true;

}

}

if (StrBool == true) {
printf("You got a Straight!");
}

}





OUTPUT:
1
3
2
<blank>
press any key to continue...

//**************
i cant put any number higher then 5 into the array, it looks like it has something to do with the array, but i REALLY dont understand why, shouldnt i be able to put any number into the array to sort them out??



***edited***

Weird when i plug in the values without any variables, straight numbers it works!!?
Hand[0] = 20
Hand[1] = 6
Hand[20] = 16
Hand[3] = 8
Hand[4] = 1

[Edited by - Bluseed on June 16, 2006 1:49:02 PM]

Share this post


Link to post
Share on other sites
I made my video poker game in c# so some of the code will be different but might give you an idea what to do...



private checkHand(int[] playerHand)
{
//Check for Royal Flush, Straight Flush, Flush
...

//Check for 4 of a Kind
...

//Check for Full House - 3 of a Kind
...

//Check for Pair - 2 Pair
...

//Check for Straight
array.sort(playerHand)
if (playerHand[4] - playerHand[0] = 4)
{
//Straight - put whatever code you want here.
}
else if (playerHand[4] == 14 && playerHand[3] == 5 &&
playerHand[3] - playerHand[0] = 3)
{
//Other way to make straight - put whatever code you want here.
}
}

A couple of things, I deleted all the other code about checking for other possible hands. each card is represented in the player hand as an integer... 2 = deuce and 14 = ace. In order for straight to work properly, all previous tests must be false. As for possibility of say a "k a 2 3 4" straight, add another else if ( I wouldn't recomend that straight being allowed, but its up to you).
I don't remember what sort you can use in c++ but once you have sorted the array this logic will work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluseed
... I can sort the numbers ...

Sorting is slow. There are much faster ways of evaluating hands that don't involve sorting. Don't sort.

Quote:
Original post by Ridge
Or use poker-eval: http://freshmeat.net/projects/poker-eval/

It's fast as a murf and it works well, but it's pretty ugly.

Your best option is to follow Ridge's suggestion and look at poker-eval. Like he says, it is very fast and pretty ugly -- lots of optimizing and not a lot of documentation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bluseedi cant put any number higher then 5 into the array, it looks like it has something to do with the array, but i REALLY dont understand why, shouldnt i be able to put any number into the array to sort them out??

You have a break statement in the loop that prints the numbers. It's inside the if statement, so no wonder it breaks the loop.

Quote:
Original post by JohnBolton
Quote:
Original post by Bluseed
... I can sort the numbers ...

Sorting is slow. There are much faster ways of evaluating hands that don't involve sorting. Don't sort.
Slow? In poker, you have 5 cards - that's five numbers to sort. How can it be slow? I understand that it could be slow, when you are sorting thousands of numbers, but five?

PS: and yes, i know that there are some variants of poker, where you get more or less than 5 cards, but as far as i know - number of cards is always from 3 to 7.

Share this post


Link to post
Share on other sites
Sorting is *relatively* slow.

In the time it takes to sort 7 cards, you could have evaluated several 7 card hands and compared them against each other to determine a winner.

Share this post


Link to post
Share on other sites

This topic is 4196 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this