Reputation: 11
I am writing a Poker program in Prolog and I your help. I want the computer to check, what kind of hands he could still get with the cards that are already lying on the table and in his hand. To do this, I need to fill in the remaining cards with cards from the deck.
Example: the flop (3 cards) is already on the table, 2 cards are in the players hands, 2 cards are left to draw. That means that the computer needs to check what hands he could get by combining his 2 hidden cards and the 3 open cards with 2 other cards, not yet drawn (including the other players cards, because he does not know them, this is no problem to do for me).
So, if he needs 2 cards for a straight, the computer should be able to ckeck if there are 2 cards left in that pool that fulfill the condition.
Since I already have the rules for the hands written down, all I have left to do is write a rule that returns to me the cards already in the hand of the computer and on the table, plus x cards from the pool. And this rule needs to be able to return EVERY possible combination of these cards (not all at once, though).
The list of possible remaining cards would look like this: list = [card(Color,Value),card(Color,Value),...]
Does anybode here have an Idea about how to do this?
Upvotes: 1
Views: 455
Reputation: 2162
I'm not sure I understand the subtleties of our problem, but overall, it sounds like you want to use set operations to figure out what's in the deck.
I strongly suggest using ordered sets lib if you're using swi-prolog, for performance. This will require that your inputs be sorted.
At start, make a set of 52 cards.
Each time the computer player sees a card, it removes it from the set.
Then have a rule
combination_fullfills_hand(HandSoFar, CardsRemaining, CardsINeed) :-
length(HandSoFar, HandSize),
NeedN is 5 - HandSize,
length(CardsDrawn, NeedN), % done first for speed
ord_subset(CardsDrawn, CardsRemaining),
ord_union(CardsDrawn, HandSoFar, CompleteHand),
hand_can_win(CompleteHand).
hand_can_win will be tried with every hand of size 5 that can be made from HandSoFar (the cards you already hold in your hand) and CardsRemaining (the cards that could be drawn)
Hey, where's the loops? you may be wondering. If so, the 'loop' is that ord_subset leaves choice points, and hand_can_win can fail. 8cD
Upvotes: 1