Committed skeleton classes needed for Gin Rummy Game

This commit is contained in:
Donald Calloway 2025-09-24 18:58:52 -07:00
parent 28d2083a03
commit b4e659e9e0

100
Gin_Rummy_Card_Game.py Normal file
View File

@ -0,0 +1,100 @@
import random
class Card:
suits = ['', '', '', '']
ranks = list(range(1, 11)) + ['J', 'Q', 'K']
RANK_ORDER = {str(n): n for n in range(2, 11)}
RANK_ORDER.update({"A": 1, "J": 11, "Q": 12, "K": 13})
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
@property
def value(self):
if isinstance(self.rank, int):
return self.rank
return 10 if self.rank in ['J', 'Q', 'K'] else 1 # Ace low
def __repr__(self):
return f"{self.rank}{self.suit}"
class Deck:
def __init__(self):
self.cards = [Card(rank, suit) for suit in Card.suits for rank in Card.ranks]
random.shuffle(self.cards)
def draw(self):
return self.cards.pop() if self.cards else None
class DiscardPile:
def __init__(self):
self.cards = []
def top(self):
return self.cards[-1] if self.cards else None
def discard(self, card):
self.cards.append(card)
class Hand:
def __init__(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def remove(self, card):
self.cards.remove(card)
def deadwood_points(self):
return sum(c.value for c in self.cards) # placeholder
def __repr__(self):
return f"Hand({self.cards})"
class Player:
def __init__(self, name):
self.name = name
self.hand = Hand()
def draw(self, deck, discard_pile, from_discard=False):
card = discard_pile.top() if from_discard else deck.draw()
if card:
self.hand.add(card)
if from_discard:
discard_pile.cards.pop()
return card
def discard(self, discard_pile, card):
self.hand.remove(card)
discard_pile.discard(card)
class Game:
def __init__(self, players):
self.deck = Deck()
self.discard_pile = DiscardPile()
self.players = players
def deal(self):
for _ in range(10):
for player in self.players:
player.hand.add(self.deck.draw())
self.discard_pile.discard(self.deck.draw())
def play_round(self):
# placeholder loop
self.deal()
print("Initial hands:")
for p in self.players:
print(p.name, p.hand)