Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Card:
- suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
- ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
- "8", "9", "10", "Jack", "Queen", "King"]
- def __init__(self, suit=0, rank=2):
- """ Initializes a card """
- self.suit = suit
- self.rank = rank
- def __str__(self):
- """ String Representation """
- return (self.ranks[self.rank] + " of " + self.suits[self.suit])
- def __cmp__(self, other):
- """ Compares cards, returns 1 if greater, -1 if lesser, 0 if equal """
- # check the suits
- if self.suit > other.suit: return 1
- if self.suit < other.suit: return -1
- # suits are the same... check ranks
- # check for aces first.
- if self.rank == 1 and other.rank == 1: return 0
- if self.rank == 1 and other.rank != 1: return 1
- if self.rank != 1 and other.rank == 1: return -1
- # check for non-aces.
- if self.rank > other.rank: return 1
- if self.rank < other.rank: return -1
- # ranks are the same... it's a tie
- return 0
- class Deck:
- def __init__(self):
- """ Initializes the deck """
- self.cards = []
- for suit in range(4):
- for rank in range(1, 14):
- self.cards.append(Card(suit, rank))
- def __str__(self):
- """ String Representation """
- s = ""
- for i in range(len(self.cards)):
- s = s + " " * i + str(self.cards[i]) + "\n"
- return s
- def print_deck(self):
- """ Prints the deck """
- for card in self.cards:
- print (card)
- def shuffle(self):
- """ Shuffles the deck """
- # Modifier!
- import random
- rng = random.Random() # create a random generator
- rng.shuffle(self.cards) # use its shuffle method
- ## def removeOriginal(self, card):
- ## """ Removes the card from the deck, returns true if successful """
- ## if card in self.cards:
- ## self.cards.remove(card)
- ## return True
- ## else:
- ## return False
- ##
- ## def remove2(self, card):
- ## """ Removes the card from the deck, returns true if successful """
- ## for lol in self.cards:
- ## if lol == card:
- ## self.cards.remove(lol)
- ## return True
- ## return False
- def remove(self, card):
- """ Removes the card from the deck, returns true if successful """
- for lol in self.cards:
- if lol.__cmp__(card) == 0:
- self.cards.remove(lol)
- return True
- return False
- def pop(self):
- """ Removes and returns the card at the bottom """
- # Bottom deal ftw!
- return self.cards.pop()
- def is_empty(self):
- """ Checks if the deck is empty """
- return (len(self.cards) == 0)
- def deal(self, hands, num_cards=999):
- """ Deals the deck to hands """
- num_hands = len(hands)
- for i in range(num_cards):
- if self.is_empty(): break # break if out of cards
- card = self.pop() # take the top card
- hand = hands[i % num_hands] # whose turn is next?
- hand.add(card) # add the card to the hand
- class Hand(Deck):
- def __init__(self, name=""):
- """ Initializes the hand """
- self.cards = []
- self.name = name
- def add(self,card):
- """ Adds a card to the hand """
- self.cards.append(card)
- def __str__(self):
- """ String Representation """
- s = "Hand " + self.name
- if self.is_empty():
- s = s + " is empty\n"
- else:
- s = s + " contains\n"
- return s + Deck.__str__(self)
- class CardGame:
- def __init__(self):
- """ Initializes the game. Creates a deck and shuffles it """
- 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)
- for lol in self.cards:
- if lol.__cmp__(match) == 0:
- self.cards.remove(card)
- self.cards.remove(match)
- print("Hand {0}: {1} matches {2}".format(self.name, card, match))
- count = count + 1
- return count
- ## def remove_matchesOriginal(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 {0}: {1} matches {2}".format(self.name, card, match))
- ## count = count + 1
- ## return count
- class OldMaidGame(CardGame):
- def play(self, names):
- # remove Queen of Clubs
- self.deck.print_deck()
- self.deck.remove(Card(0, 12))
- print("---------- {0} has been removed from the deck".format(str(Card(0, 12))))
- # make a hand for each player
- self.hands = []
- for name in names:
- self.hands.append(OldMaidHand(name))
- # deal the cards
- self.deck.deal(self.hands)
- print("---------- Cards have been dealt")
- self.printHands()
- # remove initial matches
- matches = self.remove_all_matches()
- print("---------- Matches discarded, play begins")
- self.printHands()
- # play until all 50 cards are matched
- turn = 0
- numHands = len(self.hands)
- while matches < 25:
- matches = matches + self.play_one_turn(turn)
- turn = (turn + 1) % numHands
- print("---------- Game is Over")
- self.printHands()
- def remove_all_matches(self):
- count = 0
- for hand in self.hands:
- count = count + hand.remove_matches()
- return count
- def play_one_turn(self, i):
- if self.hands[i].is_empty():
- return 0
- neighbor = self.find_neighbor(i)
- pickedCard = self.hands[neighbor].pop()
- self.hands[i].add(pickedCard)
- print("Hand", self.hands[i].name, "picked", pickedCard)
- count = self.hands[i].remove_matches()
- self.hands[i].shuffle()
- return count
- def find_neighbor(self, i):
- numHands = len(self.hands)
- for next in range(1,numHands):
- neighbor = (i + next) % numHands
- if not self.hands[neighbor].is_empty():
- return neighbor
- def printHands(self):
- for hand in self.hands:
- print(hand)
- ##game = CardGame()
- ##hand = OldMaidHand("frank")
- ##game.deck.deal([hand], 13)
- ##print(hand)
- ##hand.remove_matches()
- ##print(hand)
- ##game = OldMaidGame()
- ##game.play(["Allen","Jeff","Chris"])
- # THIS IS NOT WORKING. Remove functions do not work because it cant match any
- # searched card to any other card in the deck. Something to do with Card being
- # a totally seperate object.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement