import random import time deck=['2h','3h','4h','5h','6h','7h','8h','9h','10h','11h','12h','13h','14h', '2d','3d','4d','5d','6d','7d','8d','9d','10d','11d','12d','13d','14d', '2s','3s','4s','5s','6s','7s','8s','9s','10s','11s','12s','13s','14s', '2c','3c','4c','5c','6c','7c','8c','9c','10c','11c','12c','13c','14c'] class hand(object): def __init__(self): ''' initialize a hand ''' self.hand=[] self.newdeck=deck[:] #the newdeck is the deck - cards already played (like a deck in a game) def deck(self): return self.newdeck def removefromdeck(self,element): ''' removes an element from deck ''' #print element #print element in self.newdeck if element in self.newdeck: self.newdeck.remove(element) def removefromhand(self,card): ''' removes one card from hand assumes card is in hand ''' self.hand.remove(card) def addtohand(self,card): ''' adds card to hand ''' self.hand.append(card) def dealhand(self): ''' deals a hand of 13 cards ''' count=0 while count<13: self.hand.append(random.choice(self.newdeck)) count+=1 for x in self.hand: self.removefromdeck(x) def printhand(self): ''' prints sorted hand ''' print 'hand contains: ' for x in self.hand: if x[-1]=='c': if x[-1]>10: if x[-1]==11: print 'Jack of clubs' if x[-1]==12: print 'Queen of clubs' if x[-1]==13: print 'King of clubs' if x[-1]==14: print 'Ace of clubs' else: print x[:-1],' of clubs ' for x in self.hand: if x[-1]=='s': if x[-1]>10: if x[-1]==11: print 'Jack of spades' if x[-1]==12: print 'Queen of spades' if x[-1]==13: print 'King of spades' if x[-1]==14: print 'Ace of spades' else: print x[:-1],' of spades ' for x in self.hand: if x[-1]=='d': if x[-1]>10: if x[-1]==11: print 'Jack of diamonds' if x[-1]==12: print 'Queen of diamonds' if x[-1]==13: print 'King of diamonds' if x[-1]==14: print 'Ace of diamonds' else: print x[:-1],' of diamonds ' for x in self.hand: if x[-1]=='h': if x[-1]>10: if x[-1]==11: print 'Jack of hearts' if x[-1]==12: print 'Queen of hearts' if x[-1]==13: print 'King of hearts' if x[-1]==14: print 'Ace of hearts' else: print x[:-1],' of hearts ' h=hand() h.dealhand() h.printhand() class player(object): def __init__(self, hand, identification): ''' initialize a player ''' self.hand=hand self.identification=identification def passcards(self, card1, card2, card3): ''' pass 3 cards input three cards in hand to pass will return ''' assert card1 in hand, 'Not in hand' assert card2 in hand, 'Not in hand' assert card3 in hand, 'Not in hand' self.hand.removefromhand(card1) self.hand.removefromhand(card2) self.hand.removefromhand(card3) self.pass1=card1 self.pass2=card2 self.pass3=card3 def getcards(self, otherplayer): ''' takes the pass of another player ''' self.hand.addtohand(otherplayer.pass1) self.hand.addtohand(otherplayer.pass2) self.hand.addtohand(otherplayer.pass3) def playcard(card): ''' plays a card ''' return card #p=player(h,1) class turnphase(object): def __init__(self, cardsplayed, cardlead): ''' initializes the gameboard cardsplayed is a dictionary with the keys being the cards and the values being the player who played each card ''' self.cardsplayed=cardsplayed self.cardlead=cardlead def whowins(self): ''' returns who wins a hand ''' best=self.cardlead for key in self.cardsplayed: if key[-1]==cardlead[-1]: if key[:-1]>best[:-1]: key=best return self.cardsplayed[best] def getpoints(self): ''' returns amount of points for single trick ''' points=0 for key in self.cardsplayed: if key[-1]==h: points+=1 if key=='12s': points+=13 return points
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question