I’m trying to build a Rummy game using Python. let’s suppose that the hand of a player is made of the following cards: [(‘3’, ‘Spades’), (‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’), (‘7’, ‘Spades’),(‘5’, ‘Hearts’), (‘Q’, ‘Hearts’), (‘3’, ‘Clubs’), (‘5’, ‘Clubs’), (‘8’, ‘Clubs’), (‘Q’, ‘Clubs’),(‘6’, ‘Diamonds’), (‘Q’, ‘Diamonds’)]. I need a stratigy to follow in order to create a function that will allow to form valid melds from those 13 cards.
In this game, valid melds are:
- Pure set: is a group of 3 or 4 cards with the same rank but a different suite, and with no joker.
- Impure set: is a group of 3 or 4 cards with the same rank but a different suite and contains one joker.
- Pure sequence: a run of 3 or 4 successive cards of the same suit , and with no joker.
- Impure sequence: a run of 3 or 4 successive cards with one joker.
The game deck is 2 decks of 52 cards are combined, which mean each card will have a match.
I created functions to check if a group of cards make a valid meld or not:
is_valid_pure_set(cards), is_valid_impure_set(cards), is_valid_pure_sequence(cards), is_valid_impure_sequence(cards)
Next i created a function ‘possible_valid_melds()’, that first create all combinations of 3 and 4 cards using the 13 cards of the hand, then it returns only valid melds from those combinations.
In our example, possible_valid_melds(hand) returns: [(((‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’)), ‘pure sequence’), (((‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’)), ‘pure sequence’), (((‘5’, ‘Spades’), (‘5’, ‘Hearts’), (‘5’, ‘Clubs’)), ‘pure set’), (((‘Q’, ‘Hearts’), (‘Q’, ‘Clubs’), (‘Q’, ‘Diamonds’)), ‘pure set’)].
Based on the 13 cards of the hand and possible_valid_melds, we can only select two valid melds from possible_valid_melds. the first valid meld is (((‘Q’, ‘Hearts’), (‘Q’, ‘Clubs’), (‘Q’, ‘Diamonds’)), ‘pure set’), because none of its card is (an in common card) used by other possible melds. The problem rises when there are melds from possible melds that have in common card(s). i need to create a function that will return how much valid melds we can select from a list of possible valid melds that have in common card(s). In our example, we know that we can form the second meld, by selecting one meld from: (((‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’)), ‘pure sequence’), (((‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’)), ‘pure sequence’),(((‘5’, ‘Spades’), (‘5’, ‘Hearts’), (‘5’, ‘Clubs’)), ‘pure set’). we can for example select (((‘5’, ‘Spades’), (‘6’, ‘Spades’), (‘7’, ‘Spades’)), ‘pure sequence’) and (((‘5’, ‘Spades’), (‘5’, ‘Hearts’), (‘5’, ‘Clubs’)), ‘pure set’), because both melds has an in common card which is (‘5’, ‘Spades’), and this card has not another copy (a match) in the hand.