Guest User

Untitled

a guest
Jan 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. import random
  2.  
  3. class player():
  4.     def __init__(self, uno):
  5.         self.game = uno
  6.         self.hand = []
  7.         self.turn = 0
  8.  
  9.     def play(self, card):
  10.         #Play a card.
  11.         #Can only play if the discard is the same number or color.
  12.         if not card in self.hand:
  13.             #Doesn't have that card.
  14.             return 1
  15.  
  16.         if not self.turn:
  17.             #Not your turn.
  18.             return 2
  19.  
  20.         if card[0] == self.game.discard[0] or card[1] == self.game.discard[1]:
  21.             #LEGAL.
  22.             self.game.discard = card
  23.             self.hand.remove(card)
  24.            
  25.             if len(self.game.players.keys()) > self.game.turn + 1:
  26.                 self.game.turn += 1
  27.                 nextplayer = self.game.players.keys()[self.game.turn]
  28.                 self.game.players[nextplayer].turn = 1
  29.  
  30.             else:
  31.                 self.game.turn = 0
  32.  
  33.             self.turn = 0
  34.  
  35.         else:
  36.             #ILEGAL.
  37.             return 3
  38.  
  39. class UNO():
  40.     def __init__(self):
  41.         self.players = {}
  42.         self.gendeck()
  43.         self.discard = ''
  44.         self.turn = 0
  45.  
  46.     def gendeck(self):
  47.         colors = ['r', 'g', 'b', 'y']
  48.         cards = [str(num) for num in range(1,10)] #NUMBERS
  49.         cards += ['r', 's', '+2'] #SPECIALS
  50.         self.deck = [color+card for card in cards*2 for color in colors] + ['0'+color for color in colors] + ['w+4', 'w']*4
  51.         random.shuffle(self.deck)
  52.  
  53.     def draw(self, num=1):
  54.         #Draw some amount of cards.
  55.         return [self.deck.pop() for x in range(0,num)]
  56.  
  57.     def addplayer(self, nick):
  58.         #Add a player.
  59.         if nick in self.players:
  60.             return 1
  61.         else:
  62.             self.players[nick] = player(self)
  63.  
  64.     def start(self):
  65.         #Start the game.
  66.         if len(self.players) < 2:
  67.             return 1
  68.  
  69.         #Lay down the discard.
  70.         self.discard = self.draw()[0]
  71.  
  72.         #Deal the cards.
  73.         for player in self.players:
  74.             self.players[player].hand = self.draw(7)
  75.  
  76.         #Set the first players turn.
  77.         firstplayer = self.players.keys()[0]
  78.         self.players[firstplayer].turn = 1
  79.  
  80.     def currentplayer(self):
  81.         #Shortcut to see who's turn it is.
  82.         return self.players.keys()[self.turn]
  83.        
  84.  
  85.  
  86.            
  87.    
  88. if __name__ == '__main__':
  89.     print "Making a new UNO game."
  90.     unogame = UNO()
  91.     #Adding players.
  92.     unogame.addplayer('Blice')
  93.     unogame.addplayer('moot')
  94.  
  95.     print "Starting the game."
  96.     unogame.start()
  97.     print "Current discard is: " + unogame.discard
Add Comment
Please, Sign In to add comment