[ create a new paste ] login | about

Link: http://codepad.org/JjwunAxa    [ raw code | fork ]

Python, pasted on Jun 14:
import copy
import random

class Card:
	suitList= ["Clubs", "Diamonds", "Hearts", "Spades"]
	rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", \
				"8", "9", "10", "Jack", "Queen", "King"]
				
	def __init__(self, suit= 0, rank= 2):
		self.suit= suit
		self.rank= rank
		
	def __str__(self):
		return (self.rankList[self.rank] + " of " + self.suitList[self.suit])
		
	def __cmp__(self, other):
		# Check the suits
		if self.suit > other.suit: return 1
		if self.suit < other.suit: return -1
		
		# Make aces rank higher than kings
		if self.rank == 1: self_rank= 14
		else: self_rank= 1
		if other.rank == 1: other_rank= 14
		else: other_rank= 1
		
		# Suits are the same, check the ranks
		if self_rank > other_rank: return 1
		if self_rank < other_rank: return -1
		# Suits and ranks are the same
		return 0

		
class Deck:
	def __init__(self):
		self.cards= []
		
		for suit in range(4):
			for rank in range(1, 14):
				self.cards.append(Card(suit, rank))
	
	def	__str__(self):
		s= ""
		for i in range(len(self.cards)):
			s= s + " "*i + str(self.cards[i]) + "\n"
		
		return s
	
	def	shuffle(self):
		num_cards= len(self.cards)
		
		for i in range(num_cards):
			j= random.randrange(i, num_cards)
			self.cards[i], self.cards[j]= self.cards[j], self.cards[i]
			
	def remove_card(self, card):
		if card in self.cards:
			self.cards.remove(card)
			return True
		else:
			return False
			
	def deal_card(self):
		return self.cards.pop()

	def is_empty(self):
		return len(self.cards) == 0
	
	def deal(self, hands, num_cards= 999):
		num_hands= len(hands)
		
		for i in range(num_cards):
			if self.is_empty():
				break
			
			card= self.deal_card()
			hand= hands[i%num_hands]
			hand.add_card(card)

			
class Hand(Deck):
	def __init__(self, name= ""):
		self.cards= []
		self.name= name
	
	def __str__(self):
		front= "Hand " + self.name
		if self.is_empty():
			return front + " is empty\n"
		else:
			return front + " contains\n" + Deck.__str__(self)
	
	def add_card(self, card):
		self.cards.append(card)
		

class CardGame:
	def __init__(self):
		self.deck= Deck()
		self.deck.shuffle()

		
class OldMaidHand(Hand):
	def remove_matches(self):
		count= 0
		original_cards= self.cards[:]
		
		for card in original_cards:
			match= Card(3-card.suit, card.rank)
			
			if match in self.cards:
				self.cards.remove(card)
				self.cards.remove(match)
				print "Hand %s: %s matches %s" % (self.name, card, match)
				count += 1
		
		return count

				
if __name__ == "__main__":
	game = CardGame()
	hand = OldMaidHand("frank")
	game.deck.deal([hand], 13)
	print hand
	hand.remove_matches()


Create a new paste based on this one


Comments: