115 lines
3.8 KiB
Python
115 lines
3.8 KiB
Python
import random
|
|
|
|
def makedeck():
|
|
ranks = list(range(2, 11)) + ['J', 'Q', 'K', 'A']
|
|
suits = ['h', 'd', 'c', 's']
|
|
deck = [(rank, suit) for suit in suits for rank in ranks]
|
|
return deck
|
|
|
|
def shuffledeck(deck):
|
|
random.shuffle(deck)
|
|
return deck
|
|
|
|
def dealcard(deck):
|
|
return deck.pop()
|
|
|
|
def straight_flush(hand):
|
|
return flush(hand) and straight(hand)
|
|
|
|
def four_of_a_kind(hand):
|
|
ranks = [card[0] for card in hand]
|
|
return any(ranks.count(rank) == 4 for rank in ranks)
|
|
|
|
def fullhouse(hand):
|
|
ranks = [card[0] for card in hand]
|
|
rank_counts = {rank: ranks.count(rank) for rank in set(ranks)}
|
|
return 3 in rank_counts.values() and 2 in rank_counts.values()
|
|
|
|
def flush(hand):
|
|
suits = [card[1] for card in hand]
|
|
return len(set(suits)) == 1
|
|
|
|
def straight(hand):
|
|
rank_order = {str(k): k for k in range(2, 11)}
|
|
rank_order.update({'J': 11, 'Q': 12, 'K': 13, 'A': 14})
|
|
|
|
ranks = sorted(rank_order[str(card[0])] for card in hand)
|
|
return all(ranks[i + 1] - ranks[i] == 1 for i in range(len(ranks) - 1))
|
|
|
|
def three_of_a_kind(hand):
|
|
ranks = [card[0] for card in hand]
|
|
return any(ranks.count(rank) == 3 for rank in ranks)
|
|
|
|
def two_pair(hand):
|
|
ranks = [card[0] for card in hand]
|
|
unique_pairs = [rank for rank in set(ranks) if ranks.count(rank) == 2]
|
|
return len(unique_pairs) == 2
|
|
|
|
def pair(hand):
|
|
ranks = [card[0] for card in hand]
|
|
return any(ranks.count(rank) == 2 for rank in ranks)
|
|
|
|
def testhands():
|
|
hand1 = [(2, 'h'), (3, 'h'), (4, 'h'), (5, 'h'), (6, 'h')] # Straight flush
|
|
hand2 = [(10, 'c'), (10, 'd'), (10, 'h'), (10, 's'), (2, 'h')] # Four of a kind
|
|
hand3 = [(10, 'c'), (10, 'd'), (10, 'h'), (2, 's'), (2, 'h')] # Full house
|
|
hand4 = [(2, 'h'), (4, 'h'), (6, 'h'), (8, 'h'), (10, 'h')] # Flush
|
|
hand5 = [(2, 'h'), (3, 'd'), (4, 'c'), (5, 's'), (6, 'h')] # Straight
|
|
hand6 = [(10, 'c'), (10, 'd'), (10, 'h'), (2, 's'), (3, 'h')] # Three of a kind
|
|
hand7 = [(10, 'c'), (10, 'd'), (2, 's'), (2, 'h'), (3, 'h')] # Two pair
|
|
hand8 = [(10, 'c'), (10, 'd'), (2, 's'), (3, 'h'), (4, 'h')] # Pair
|
|
|
|
print("-------------------This is a test case -----------------------")
|
|
print(f"Hand 1 Straight Flush: {straight_flush(hand1)}") # True
|
|
print(f"Hand 2 Four of a Kind: {four_of_a_kind(hand2)}") # True
|
|
print(f"Hand 3 Full House: {fullhouse(hand3)}") # True
|
|
print(f"Hand 4 Flush: {flush(hand4)}") # True
|
|
print(f"Hand 5 Straight: {straight(hand5)}") # True
|
|
print(f"Hand 6 Three of a Kind: {three_of_a_kind(hand6)}") # True
|
|
print(f"Hand 7 Two Pair: {two_pair(hand7)}") # True
|
|
print(f"Hand 8 Pair: {pair(hand8)}") # True
|
|
|
|
def discussion1():
|
|
cards_per_hand = 5
|
|
|
|
#First assignment printout
|
|
fulldeck = makedeck()
|
|
print("Full Deck: ")
|
|
for card in fulldeck:
|
|
print(f"Card: {card}")
|
|
|
|
#Ask how many players
|
|
while True:
|
|
try:
|
|
num_hands = int(input('How many players are there? '))
|
|
if num_hands > 0:
|
|
print(f"The number of players is {num_hands} players")
|
|
break
|
|
else:
|
|
print('Please enter a positive number of players')
|
|
except ValueError:
|
|
print('Please enter a number of players instead.')
|
|
|
|
#Create a list of hand values
|
|
hands = [[] for _ in range(num_hands)]
|
|
|
|
#Deal the cards
|
|
for round_nm in range(1):
|
|
|
|
shuffled_deck = shuffledeck(fulldeck)
|
|
print(f"Round {round_nm + 1}:")
|
|
for hand_index in range(num_hands): # Iterate through each hand
|
|
print(f"Hand {hand_index + 1}:")
|
|
for card_num in range(cards_per_hand): # Deal cards to the current hand
|
|
card = shuffled_deck.pop()
|
|
hands[hand_index].append(card)
|
|
|
|
print(hands[hand_index])
|
|
|
|
def main():
|
|
discussion1()
|
|
testhands()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|