Python project using classes and funtions to create Texas Hold’em Poker?. LINK:
ID: 3906161 • Letter: P
Question
Python project using classes and funtions to create Texas Hold’em Poker?.
LINK: https://www.cse.msu.edu/~cse231/Online/Projects/Project06/project06.pdf (PDF)
LINK2: https://www.cse.msu.edu/~cse231/Online/Projects/Project06/ (files)
Assignment Background
The goal of this project is to gain practice with use of classes and creating functions. You will design and implement a Python program which plays simplified Texas Hold’em Poker.
The program should deal two cards to two players (one card to each player, then a second card to each player), and then five community cards which players share to make their hands. A poker hand is the best five cards from the community cards plus the player’s cards (i.e., best 5 out of 7 cards total). The goal of this assignment is to find the category of each player’s hand and determine the winner.
The rules of this game are relatively simple and you can find information about the game and about the poker hands in the links below. Keep in mind that you will only find the category of the hands and all nine cards can be dealt at once (a real poker game deals cards in stages to allow for betting, but we aren’t betting).
http://en.wikipedia.org/wiki/Texas_holdem http://en.wikipedia.org/wiki/Poker_hands
The categories in order from lowest to highest are: High card, 1 pair, 2 pair, 3 of a kind, straight, flush, full house, 4 of a kind, straight flush.
You will not find a player’s hand’s value, but just the category of the hand.
It is a tie if both players have hands in the same category. That is, if both of them have straights, it is considered a tie no matter who has the higher straight. Our game is simplified in two ways:
1. 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.
2. 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
1. 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.
2. 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.
3. 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.
4. 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.
5. 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
1. 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.
2. 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.)
3. 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].
4. I found dictionaries useful within functions. Sometimes using the rank as key worked best; other times using the suit as key was best.
5. 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.)
6. Mirmir testing: these are arbitrary to match the tests in Mirmir.
a. For ties print hand1.
b. Dealing: common hand first, then hand 1, then hand 2 (I used list comprehension)
c. I provided a function named connanical that orders hands to match Test 1 and Test 2 in Mirmir (individual Function Tests don’t care about order). If more than five cards are in a category, e.g. all seven cards are the same suit, I applied cannonical() before slicing off the first 5 cards.
d. Since Mimir cannot handle the Unicode suit symbols, the suit_list was changed to characters. You don’t need to do anything—this is built into the cards.py used for testing on Mimir. When you look at the Mirmir output you will see characters rather than symbols for suits: suit_list = ['x','c','d','h','s']
Sample Output:
Test 1
----------------------------------------
Let's play poker!
Community cards: [9?, 7?, 6?, 6?, 4?]
Player 1: [4?, 10?]
Player 2: [7?, 10?]
TIE with two pairs: [4?, 4?, 6?, 6?]
Do you wish to play another hand?(Y or N) y
----------------------------------------
Let's play poker!
Community cards: [Q?, K?, K?, K?, 7?]
Player 1: [K?, 10?]
Player 2: [Q?, J?]
Player 1 wins with four of a kind: [K?, K?, K?, K?]
Do you wish to play another hand?(Y or N) n
Test 2
----------------------------------------
Let's play poker!
Community cards: [9?, 7?, 6?, 6?, 4?]
Player 1: [4?, 10?]
Player 2: [7?, 10?]
TIE with two pairs: [4?, 4?, 6?, 6?]
Do you wish to play another hand?(Y or N) y
----------------------------------------
Let's play poker!
Community cards: [Q?, K?, K?, K?, 7?]
Player 1: [K?, 10?]
Player 2: [Q?, J?]
Player 1 wins with four of a kind: [K?, K?, K?, K?]
Do you wish to play another hand?(Y or N) y
----------------------------------------
Let's play poker!
Community cards: [3?, 6?, A?, 8?, 2?]
Player 1: [9?, 5?]
Player 2: [10?, 4?]
TIE with a flush: [A?, 2?, 3?, 5?, 8?]
Do you wish to play another hand?(Y or N) y
----------------------------------------
Let's play poker!
Community cards: [9?, 8?, 5?, Q?, Q?]
Player 1: [J?, J?]
Player 2: [4?, A?]
TIE with a flush: [5?, 8?, 9?, J?, Q?]
Do you wish to play another hand?(Y or N) y
----------------------------------------
Let's play poker!
Community cards: [2?, 3?, A?, 2?, 3?]
Player 1: [2?, A?]
Player 2: [8?, A?]
Player 1 wins with a full house: [A?, A?, 2?, 2?, 2?]
Deck has too few cards so game is done.
Code Given:
?proj06.py
cards.py
Explanation / Answer
here is your program : ----------->>>>>>>>>>>
import cards
import cards
def less_than(c1,c2):
'''Return
True if c1 is smaller in rank,
True if ranks are equal and c1 has a 'smaller' suit
False otherwise'''
if c1.rank() < c2.rank():
return True
elif c1.rank() == c2.rank() and c1.suit() < c2.suit():
return True
return False
def min_in_list(L):
'''Return the index of the mininmum card in L'''
min_card = L[0] # first card
min_index = 0
for i,c in enumerate(L):
if less_than(c,min_card): # found a smaller card, c
min_card = c
min_index = i
return min_index
def cannonical(H):
''' Selection Sort: find smallest and swap with first in H,
then find second smallest (smallest of rest) and swap with second in H,
and so on...'''
for i,c in enumerate(H):
# get smallest of rest; +i to account for indexing within slice
min_index = min_in_list(H[i:]) + i
H[i], H[min_index] = H[min_index], c # swap
return H
def flush_7(H):
'''Return a list of 5 cards forming a flush,
if at least 5 of 7 cards form a flush in H, a list of 7 cards,
False otherwise.'''
l1 = []
l2 = []
l3 = []
l4 = []
l = []
for i in range(0,len(H)):
if H[i].suit == 'c':
l1.append(H[i])
elif H[i].suit == 'd':
l2.append(H[i])
elif H[i].suit == 'h':
l3.append(H[i])
elif H[i].suit == 's':
l4.append(H[i])
if len(l1) == 5:
return l1;
elif len(l2) == 5:
return l2
elif len(l3) == 5:
return l3;
elif len(l4) == 5:
return l4;
return l;
def straight_7(H):
'''Return a list of 5 cards forming a straight,
if at least 5 of 7 cards form a straight in H, a list of 7 cards,
False otherwise.'''
l = cannonical(H)
l1 = []
if len(l) < 1:
return l1
rn = l[1].value()
for i in range(0,len(l)):
if rn == l[i].value():
l1.append()
else:
l1.clear()
if rn < 10:
rn = rn + 1;
if len(l1) == 5:
return l1
l1.clear()
return l1
def straight_flush_7(H):
'''Return a list of 5 cards forming a straight flush,
if at least 5 of 7 cards form a straight flush in H, a list of 7 cards,
False otherwise.'''
l = flush_7(H)
l1 = []
l2 = []
if l != None:
l1 = straight_7(l)
if l1 != None:
return l1;
return l2
def four_7(H):
'''Return a list of 4 cards with the same rank,
if 4 of the 7 cards have the same rank in H, a list of 7 cards,
False otherwise.'''
l1 = cannonical(H)
l2 = []
rnk = l1[0].rank()
for i in range(0,len(l1)):
if rnk == l1[i].rank():
l2.append(l1[i])
else:
if len(l2) == 4:
return l2;
l2.clear()
rnk = l1[i].rank()
l2.append(l1[i])
if len(l2) == 4:
return l2;
l2.clear()
return l2
def three_7(H):
'''Return a list of 3 cards with the same rank,
if 3 of the 7 cards have the same rank in H, a list of 7 cards,
False otherwise.
You may assume that four_7(H) is False.'''
l1 = cannonical(H)
l2 = []
rnk = l1[0].rank()
for i in range(0,len(l1)):
if rnk == l1[i].rank():
l2.append(l1[i])
else:
if len(l2) == 3:
return l2;
l2.clear()
rnk = l1[i].rank()
l2.append(l1[i])
if len(l2) == 3:
return l2;
l2.clear()
return l2
def two_pair_7(H):
'''Return a list of 4 cards that form 2 pairs,
if there exist two pairs in H, a list of 7 cards,
False otherwise.
You may assume that four_7(H) and three_7(H) are both False.'''
l1 = cannonical(H)
l2 = []
rnk = l1[0].rank()
for i in range(0,len(l1)):
if rnk == l1[i].rank():
l2.append(l1[i])
else:
if len(l2) == 2:
rnk = l1[i].rank()
break;
l2.clear()
rnk = l1[i].rank()
l2.append(l1[i])
for i in range(0,len(l1)):
if rnk == l1[i].rank():
l2.append(l1[i])
else:
if len(l2) == 4:
return l2;
l2.remove(-1)
rnk = l1[i].rank()
l2.append(l1[i])
if len(l2) == 4:
return l2;
l2.clear()
return l2
def one_pair_7(H):
'''Return a list of 2 cards that form a pair,
if there exists exactly one pair in H, a list of 7 cards,
False otherwise.
You may assume that four_7(H), three_7(H) and two_pair(H) are False.'''
l1 = cannonical(H)
l2 = []
rnk = l1[0].rank()
for i in range(0,len(l1)):
if rnk == l1[i].rank():
l2.append(l1[i])
else:
if len(l2) == 2:
return l2;
l2.clear()
rnk = l1[i].rank()
l2.append(l1[i])
if len(l2) == 2:
return l2;
l2.clear()
return l2
def full_house_7(H):
'''Return a list of 5 cards forming a full house,
if 5 of the 7 cards form a full house in H, a list of 7 cards,
False otherwise.
You may assume that four_7(H) is False.'''
l1 = three_7(H)
if l1 == None:
return l1;
l2 = cannonical(H)
rnk = l2[0].rank()
for i in range(0,len(l2)):
if rnk == l2[i].rank():
l1.append(l2[i])
else:
if len(l1) == 5:
return l1;
l1.remove(-1)
rnk = l2[i].rank()
l1.append(l2[i])
if len(l1) == 5:
return l1;
l1.clear()
return l1
def countScore(l1,l2,l3):
l = []
for i in range(0,len(l1)):
l.append(l1[i])
l.append(l2)
l4 = straight_flush_7(l);
l.pop(-1);
l.append(l3)
l5 = straight_flush_7(l)
if l4 != None and l5 != None:
print("TIE with Straight Flush ",l4)
return
elif l4 == None:
print("Player 2 Wins with Straight Flush ",l5)
return;
elif l5 == None:
print("Player 1 Wins with Straight Flush ",l4)
return;
l5.clear()
l5 = four_7(l)
l.remove(-1)
l.append(l2)
l4.clear()
l4 = four_7(l)
if l4 == None and l5 != None:
print("Player 2 Wins with Straight Flush ",l5)
return;
elif l5 == None and l4 != None:
print("Player 1 Wins with Straight Flush ",l4)
return;
l4.clear()
l4 = full_house_7(l)
l.remove(-1)
l.append(l3)
l5.clear()
l5 = full_house_7(l)
if l4 != None and l5 != None:
print("TIE with Full House ",l4)
return
elif l4 == None:
print("Player 2 Wins with Full House ",l5)
return;
elif l5 == None:
print("Player 1 Wins with Full House ",l4)
return;
l5.clear()
l5 = three_7(l)
l.remove(-1)
l.append(l2)
l4.clear()
l4 = three_7(l)
if l4 != None and l5 != None:
print("TIE with Three Of A KInd ",l4)
return
elif l4 == None:
print("Player 2 Wins with Three Of A Kind ",l5)
return;
elif l5 == None:
print("Player 1 Wins with Three Of A Kind ",l4)
return;
l4.clear()
l4 = two_pair_7(l)
l.remove(-1)
l.append(l3)
l5.clear()
l5 = two_pair_7(l)
if l4 != None and l5 != None:
print("TIE with Two Pair ",l4)
return
elif l4 == None:
print("Player 2 Wins with Two Pair ",l5)
return;
elif l5 == None:
print("Player 1 Wins with Two Pair ",l4)
return;
l5.clear()
l5 = one_pair_7(l)
l.remove(-1)
l.append(l2)
l4.clear()
l4 = one_pair_7(l)
if l4 != None and l5 != None:
print("TIE with One Pair ",l4)
return
elif l4 == None:
print("Player 2 Wins with One Pair ",l5)
return;
elif l5 == None:
print("Player 1 Wins with One Pair ",l4)
return;
l4.clear()
l4 = flush_7(l)
l.remove(-1)
l.append(l3)
l5.clear()
l5 = flush_7(l)
if l4 != None and l5 != None:
print("TIE with flush ",l4)
return
elif l4 == None:
print("Player 2 Wins with flush ",l5)
return;
elif l5 == None:
print("Player 1 Wins with flush ",l4)
return;
l5.clear()
l5 = straight_7(l)
l.remove(-1)
l.append(l2)
l4.clear()
l4 = straight_7(l)
if l4 != None and l5 != None:
print("TIE with Straight ",l4)
return
elif l4 == None:
print("Player 2 Wins with Straight ",l5)
return;
elif l5 == None:
print("Player 1 Wins with Straight ",l4)
return;
def main():
D = cards.Deck()
D.shuffle()
while True:
if len(D) < 9:
return
# create community cards
community_list = []
for i in range(0,5):
community_list.append(D.deal())
# create Player 1 hand
hand_1_list = D.deal()
# create Player 2 hand
hand_2_list = D.deal()
print("-"*40)
print("Let's play poker! ")
print("Community cards:",community_list)
print("Player 1:",hand_1_list)
print("Player 2:",hand_2_list)
countScore(community_list,hand_1_list,hand_2_list)
y = input("do you wish to play another hand?(Y or N)")
if y[0] != 'Y':
return
if __name__ == "__main__":
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.