Skip to content

Instantly share code, notes, and snippets.

@jaskaran1989
Created January 23, 2020 06:16
Show Gist options
  • Save jaskaran1989/75d9bd643a9a0e1a536f361c96a8be36 to your computer and use it in GitHub Desktop.
Save jaskaran1989/75d9bd643a9a0e1a536f361c96a8be36 to your computer and use it in GitHub Desktop.
Build a Deck of Cards with OO Python
import random
class Card:
def __init__(self,suit,value):
self.suit=suit
self.value=value
def show(self):
print("{} of {}".format(self.value,self.suit))
class Deck:
def __init__(self):
self.cards=[]
self.build()
def build(self):
for s in ["Spades","Clubs","Diamonds","Hearts"]:
for v in range(1,14):
self.cards.append(Card(v,s))
def shuffle(self):
for i in range(1,len(self.cards)):
r= random.randint(0,i)
self.cards[i],self.cards[r]=self.cards[r],self.cards[i]
def show(self):
for c in self.cards:
c.show()
def drawCard(self):
return self.cards.pop()
class Player:
def __init__(self,name):
self.name = name
self.hand=[]
def draw(self,deck):
self.hand.append(deck.drawCard())
return self
def showHand(self):
for card in self.hand:
card.show()
deck =Deck()
deck.shuffle()
jas = Player("Jas")
jas.draw(deck)
jas.draw(deck)
jas.draw(deck)
jas.showHand()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment