Poker Game
I''m attempting to make a poker game but I have a few problems. I know I need to use a LIFO stack for the cards and dynamic arrays for each person''s hand but I don''t know how to check if someone has a royal flush, straight, or any other combo. I''m also not sure how to compare it to the other player''s hand to see who wins. All I know right now is that a switch-case might work. Any tips you have would be very helpful.
"The Key To Creation Is Destruction"
You might be able to assign different values to each card, and have a running total of the sum of all the cards in the players hand. Build a CheckHand function, and compare the current scores to say a Royal Flush''s score. I''d recommend valuing the cards like this:
1 (2^0) - 2 (2^1) - 3 (2^2) - 4 (2^3) - 5 (2^4) - 6 (2^5) - 7 (2^6) - 8 (2^7) - 9 (2^8) - 10 (2^9) - J (2^10) - Q (2^11) - K (2^12) - A (2^13)
This is a fast method, and I''ve applied it to varying games where a count matters. I used it to develop artificial intelligence in a tic tac toe game, and it was used as far back in pacman.
1 (2^0) - 2 (2^1) - 3 (2^2) - 4 (2^3) - 5 (2^4) - 6 (2^5) - 7 (2^6) - 8 (2^7) - 9 (2^8) - 10 (2^9) - J (2^10) - Q (2^11) - K (2^12) - A (2^13)
This is a fast method, and I''ve applied it to varying games where a count matters. I used it to develop artificial intelligence in a tic tac toe game, and it was used as far back in pacman.
"The time has come", the Walrus said, "To speak of many things."
Not speaking from experience, but maybe I can help with theory.
First, you should probably build a card structure which contains enumerated values for suits as well as a numeric face value for the cards.
As far as checking for the hands, start from the highest hand, royal flush, and work your way down, using whatever methods necessary to check for the particular hand. The logic behind checking for a couple of them are easy, such as the straight or flush, or any combination of the two. Flushes you can check by seeing if they are all the same suit, and straights are just checking the values of the cards, sorted by face value, to see if they are in a contiguous sequence.
As far as seeing who wins, for each case that you would use to check for a different hand, assign a point value to it. Since there are ten possible hands, if they get a royal flush, they get 10 points for that hand, etc...
It would be my opinion that just doing a small five card stud version of poker would be advantageous since there wouldn''t be any AI to program, but you could make sure that the hands are recognized first, before moving on to more complicated things.
As far as recognizing the more complicated hands, such as full houses, I can probably come up with some sort of algorithm to check for those if needbe, but I hope this helped somewhat.
Ken
First, you should probably build a card structure which contains enumerated values for suits as well as a numeric face value for the cards.
As far as checking for the hands, start from the highest hand, royal flush, and work your way down, using whatever methods necessary to check for the particular hand. The logic behind checking for a couple of them are easy, such as the straight or flush, or any combination of the two. Flushes you can check by seeing if they are all the same suit, and straights are just checking the values of the cards, sorted by face value, to see if they are in a contiguous sequence.
As far as seeing who wins, for each case that you would use to check for a different hand, assign a point value to it. Since there are ten possible hands, if they get a royal flush, they get 10 points for that hand, etc...
It would be my opinion that just doing a small five card stud version of poker would be advantageous since there wouldn''t be any AI to program, but you could make sure that the hands are recognized first, before moving on to more complicated things.
As far as recognizing the more complicated hands, such as full houses, I can probably come up with some sort of algorithm to check for those if needbe, but I hope this helped somewhat.
Ken
quote: Original post by CoolMiker
I''m attempting to make a poker game but I have a few problems. I know I need to use a LIFO stack for the cards and dynamic arrays for each person''s hand but I don''t know how to check if someone has a royal flush, straight, or any other combo. I''m also not sure how to compare it to the other player''s hand to see who wins. All I know right now is that a switch-case might work. Any tips you have would be very helpful.
I don''t know if anybody''s replied to this--there are two replies listed, but it looks like there''s an error with the new forums.
Anyway, here''s how I would do it:
You need a function to compare two hands. If you have a class representing each hand, you just need bool Hand::compare (const Hand &other). If you aren''t using classes, create a function that will compare two hands: bool compareHand (const Hand &lhs, const Hand &rhs). (you could even overload operator < if you''re using classes).
This function should return true if the first hand (or the member hand if using classes) is beaten by the other hand, like a less-than operation. To use this function to find who wins, it should be as simple as finding the hand that is not less than any other hands. You could sort the hands as you go along by score as well. Since this is poker, there can''t be "equal" hands, so the compare will either be true or false.
The math of figuring out which hand loses is pretty simple, just start with the highest scoring type of hand (straight flush), see if either has it, continue down the line, etc. If both players have the same type of hand, you go to the high card, and then if they have the same high card, you use the suit.
This could lead to wasteful recalculation, so you might want to make it so that each hand is evaluated once, or make a seperate score class that''s attached to each hand. I think if you had an ordered enum with each hand type (straight flush, etc...down to nothing in the order of their score), the high card, and the high suit, you should have enough information to form a number that would always be ordered correctly.
Tons of ways to do it, hope that helps give you some idea.
There is no need to use more than a byte to store card type.
Cards = 0 through 51.
#define HEARTS 0
#define DIAMONDS 1
#define CLUBS 2
#define SPADES 3
// returns 0 through 3
#define GetSuit(c) ( (c) % 13 )
// returns 1 through 13; Aces = 1
#define GetValueAceLow(c) ( (c) - (GetSuit (c) * 13) + 1 )
// returns 2 through 14; Aces = 14
#define GetValueAceHigh(c) ( (c) - (GetSuit (c) * 13) + 2 )
Edited by - bishop_pass on January 2, 2001 2:30:30 PM
Cards = 0 through 51.
#define HEARTS 0
#define DIAMONDS 1
#define CLUBS 2
#define SPADES 3
// returns 0 through 3
#define GetSuit(c) ( (c) % 13 )
// returns 1 through 13; Aces = 1
#define GetValueAceLow(c) ( (c) - (GetSuit (c) * 13) + 1 )
// returns 2 through 14; Aces = 14
#define GetValueAceHigh(c) ( (c) - (GetSuit (c) * 13) + 2 )
Edited by - bishop_pass on January 2, 2001 2:30:30 PM
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement