I'm having trouble figuring out how to make my program work in the way it is supposed to. I have quite a bit done this far but I'm not sure how to continue on in order to play Texas holdem. Here is some of the information from the assignment that might make helping me easier
"Our game is simplified in two ways:
-
We only compare categories to determine the winner, not the value of the hands. For example, in a real poker game the winner of hands with exactly one pair would be determined by which pair had the highest card rank. That is, a pair of 10s wins over a pair of 3s. In our game, two hands with exactly one pair is considered a tie with no consideration given to the card ranks.
-
The Card class ranks an Ace as the lowest card in a suit; whereas traditional poker ranks an Ace as highest. For simplicity, we will keep Aces as the lowest ranked card because we are only comparing categories not values. In particular, the cards 10h, Jh, Qh, Kh, Ah are not considered to make a straight in our game (they would under normal poker rules).
Assignment Specifications
-
Your program will use the Card and Deck classes found in the file named cards.py. You may not modify the contents of that file: we will test your project using our copy of cards.py. We have included a cardsDemo.py program to illustrate how to use the Card and Deck classes.
-
Your program will be subdivided into meaningful functions. Use a function for each category (except that “high-card” doesn’t need a function). See hints below.
-
Your program will display each player’s dealt cards, the community cards, and announce the winner of the hand. Also, your program will display the winning combination (or either of the winning combinations, if a tie) as well as the category of the winning hand (see sample output below). For example, if the winning combination is “four-of-a-kind”, those four cards will be displayed; the fifth card in the hand will not be displayed. The display will be appropriately labeled and formatted.
-
After each run, the program will prompt the user if they want to see another hand: “do you want to continue: y or n”. The program should continue if user enters “y” or “Y”; otherwise, the program should halt. Also, if there are fewer than 9 cards left in the deck, the program should halt. Do not shuffle between hands; only shuffle at the beginning of the program.
-
Using dictionaries in this assignment is very useful as you can find how many cards have the same suit, or how many of them have the same rank. Most of my functions used a dictionary that had rank as the key, so I created a separate function to build such a dictionary from a hand.
Hints
-
There are 9 categories of hands. For each category (except high-card), I wrote a function that would take as an argument a list of 7 cards and return either a sub-list of cards that satisfied that category or an empty list. That design let me use the functions in Boolean expressions since an empty list evaluates to False whereas a non-empty list evaluates to True. Returning a list of cards also allowed me to use functions within functions, e.g., a straight flush must be a flush. Returning a list of cards also made testing easier because I could see not only that the function had found a combination of that type, but I also could easily check that it was correct. By the way, the function to find a straight was the hardest function to write.
-
Finding a winner is simple: start with the highest category and work your way down the categories in a cascade of “if” and “elif” statements. The result is a long, but repetitive structure. (You do not need to check all possible combinations of hands because you are only trying to find the highest category that a hand fits.)
-
Think strategically for testing. Custom build a set of hands for testing by creating particular cards, e.g. H1 = [5c, 2c, 5h, 4s, 3d, 2h, 5d] can be used to test for a full house (I had to create each card first, e.g. c1 = cards.Card(5,1) to create the 5c card in H1. It took many lines to create the testing hands, but once built they proved useful.) Test each category function separately using the custom hands you created for testing. For example, result = full_house(H1) should yield a result [5c, 5h, 5d, 2c, 2h].
-
I found dictionaries useful within functions. Sometimes using the rank as key worked best; other times using the suit as key was best.
-
In order for sort() and sorted() to work on any data type the less-than operation must be defined. That operation is not defined in the Cards class so neither sort() nor sorted() work on Cards. (We tried to make the Cards class as generic as possible to work with a wide variety of card games, and the ordering of cards considering both rank and suit varies across card games.)"
import cards the_deck=cards.Deck() the_deck.shuffle() def deal_cards(): player1_list=[] player2_list=[] for i in range( 2 ): player1_card= the_deck.deal() player2_card= the_deck.deal() player1_list.append(player1_card) player2_list.append(player2_card) print("=======Player 1=======") print(player1_list) print() print("=======Player 2=======") print(player2_list) print() community_list=[] for j in range(5): community_cards=the_deck.deal() community_list.append(community_cards) print("=======Community Cards=======") print(community_list) print() deal_cards() class poker_hand(): def __init__(self, card_list): self.card_list = card_list def __repr__(self): short_desc = "Nothing." numeral_dict = cards.defaultdict(int) suit_dict = cards.defaultdict(int) for my_card in self.card_list: numeral_dict[my_card.numeral] += 1 suit_dict[my_card.suit] += 1 # Pair if len(numeral_dict) == 4: short_desc = "One pair." # Two pair or 3-of-a-kind elif len(numeral_dict) == 3: if 3 in numeral_dict.values(): short_desc ="Three-of-a-kind." else: short_desc ="Two pair." # Full house or 4-of-a-kind elif len(numeral_dict) == 2: if 2 in numeral_dict.values(): short_desc ="Full house." else: short_desc ="Four-of-a-kind." else: # Flushes and straights straight, flush = False, False if len(suit_dict) == 1: flush = True min_numeral = min([cards.index(x) for x in numeral_dict.keys()]) max_numeral = max([cards.index(x) for x in numeral_dict.keys()]) if int(max_numeral) - int(min_numeral) == 4: straight = True # Ace can be low low_straight = set(("Ace", "2", "3", "4", "5")) if not set(numeral_dict.keys()).difference(low_straight): straight = True if straight and not flush: short_desc ="Straight." elif flush and not straight: short_desc ="Flush." elif flush and straight: short_desc ="Straight flush." enumeration = "/".join([str(x) for x in self.card_list]) return "{enumeration} ({short_desc})".format(**locals())
Aucun commentaire:
Enregistrer un commentaire