Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

First attempt at object oriented language using the card game War.

View war.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
import random
 
"""class Card(object):
rank = "Ace"
suit = "Hearts"
def __str__(self):
return "%s of %s" % (self.rank, self.suit)
 
ace_of_hearts = Card()
 
print ace_of_hearts"""
 
class Card(object):
''' Implementation of a card class '''
ranks = [None, 'Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight',
'Nine', 'Ten', 'Jack', 'Queen', 'King']
suits = ["Hearts", "Clubs", "Diamonds", "Spades"]
def __init__(self, rank=2, suit=0):
self.rank = rank
self.suit = suit
 
def __str__(self):
return "%s of %s" % (self.ranks[self.rank], self.suits[self.suit])
def __gt__(self, other):
if self.rank > other.rank:
return True
else:
return False
def __lt__(self, other):
if self.rank < other.rank:
return True
else:
return False
def __eq__(self, other):
if self.rank == other.rank:
return True
else:
return False
class Deck(object):
'''Implementation of a deck of cards. '''
def __init__(self):
self.cards = []
for s in range(4):
for r in range(1, 14):
card = Card(r, s)
self.cards.append(card)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
'''prints what's in your hand, mostly for debugging'''
def deal(self):
return self.cards.pop(0)
def add_card(self, card):
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
class Hand(Deck):
pass
def __init__(self):
self.cards = []
if __name__ == '__main__':
d = Deck()
d.shuffle()
print d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.