Created
December 9, 2015 03:54
-
-
Save jdecuirm/82951ba7e4aa9e571819 to your computer and use it in GitHub Desktop.
Importing and inheritance problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Card: | |
| """ Attributes and methods for the Card class """ | |
| 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 = 0): | |
| self.suit = suit | |
| self.rank = rank | |
| def __str__(self): | |
| # self.ranks is refering to the class attribute ranks. It could be called also with Card.ranks | |
| return (self.ranks[self.rank] + " of " + self.suits[self.suit]) | |
| # def cmp(self, other): | |
| # # Check the suits | |
| # if self.suit > other.suit: | |
| # return 1 | |
| # if self.suit < other.suit: | |
| # return -1 | |
| # if self.rank > other.rank: | |
| # return 1 | |
| # if self.rank < other.rank: | |
| # return -1 | |
| # return 0 | |
| # Make the ace bigger than king | |
| def cmp(self, other): | |
| if self.suit > other.suit: | |
| return 1 | |
| if self.suit < other.suit: | |
| return -1 | |
| if self.rank < other.rank: | |
| return 1 | |
| if self.rank > other.rank: | |
| return -1 | |
| return 0 | |
| def __eq__(self, other): | |
| return self.cmp(other) == 0 | |
| def __le__(self, other): | |
| return self.cmp(other) <= 0 | |
| def __ge__(self, other): | |
| return self.cmp(other) >= 0 | |
| def __gt__(self, other): | |
| return self.cmp(other) > 0 | |
| def __lt__(self, other): | |
| return self.cmp(other) < 0 | |
| def __ne__(self, other): | |
| return self.cmp(other) != 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from hand import Hand | |
| from card import Card | |
| import random | |
| class Deck: | |
| """ Deck class """ | |
| def __init__(self): | |
| self.cards = [] | |
| for suit in range(4): | |
| for rank in range(1, 14): | |
| self.cards.append(Card(suit, rank)) | |
| def print_deck(self): | |
| for card in self.cards: | |
| print(card) | |
| def __str__(self): | |
| s = "" | |
| for i in range(len(self.cards)): | |
| s = s + " " * i + str(self.cards[i]) + "\n" | |
| return s | |
| def shufflev1(self): | |
| rng = random.Random() | |
| num_cards = len(self.cards) | |
| for i in range(num_cards): | |
| j = rng.randrange(i, num_cards) | |
| (self.cards[i], self.cards[j])= (self.cards[j], self.cards[i]) # Good method to shuffle the deck without using shuffle random method | |
| def shufflev2(self): | |
| rng = random.Random() # Create a random generator | |
| rng.shuffle(self.cards) # Use its shuffle method | |
| def remove(self, card): | |
| if card in self.cards: | |
| self.cards.remove(card) | |
| return True | |
| else: | |
| return False | |
| def pop(self): | |
| # pop removes last card on the deck, so we are dealing from the bottom of it. | |
| return self.cards.pop() | |
| def is_empty(self): | |
| return self.cards == [] | |
| #23.3 Dealing cards | |
| def deal(self, hands, num_cards = 999): | |
| 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 | |
| deck = Deck() | |
| deck.shufflev2() | |
| hand = Hand("Frank") | |
| deck.deal([hand], 5) | |
| print(hand) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from deck import Deck | |
| class Hand(Deck): | |
| """ Main class for Hand """ | |
| def __init__(self, name=""): | |
| self.cards = [] | |
| self.name = name | |
| def add(self, card): | |
| self.cards.append(card) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment