Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class player():
- def __init__(self, uno):
- self.game = uno
- self.hand = []
- self.turn = 0
- def play(self, card):
- #Play a card.
- #Can only play if the discard is the same number or color.
- if not card in self.hand:
- #Doesn't have that card.
- return 1
- if not self.turn:
- #Not your turn.
- return 2
- if card[0] == self.game.discard[0] or card[1] == self.game.discard[1]:
- #LEGAL.
- self.game.discard = card
- self.hand.remove(card)
- if len(self.game.players.keys()) > self.game.turn + 1:
- self.game.turn += 1
- nextplayer = self.game.players.keys()[self.game.turn]
- self.game.players[nextplayer].turn = 1
- else:
- self.game.turn = 0
- self.turn = 0
- else:
- #ILEGAL.
- return 3
- class UNO():
- def __init__(self):
- self.players = {}
- self.gendeck()
- self.discard = ''
- self.turn = 0
- def gendeck(self):
- colors = ['r', 'g', 'b', 'y']
- cards = [str(num) for num in range(1,10)] #NUMBERS
- cards += ['r', 's', '+2'] #SPECIALS
- self.deck = [color+card for card in cards*2 for color in colors] + ['0'+color for color in colors] + ['w+4', 'w']*4
- random.shuffle(self.deck)
- def draw(self, num=1):
- #Draw some amount of cards.
- return [self.deck.pop() for x in range(0,num)]
- def addplayer(self, nick):
- #Add a player.
- if nick in self.players:
- return 1
- else:
- self.players[nick] = player(self)
- def start(self):
- #Start the game.
- if len(self.players) < 2:
- return 1
- #Lay down the discard.
- self.discard = self.draw()[0]
- #Deal the cards.
- for player in self.players:
- self.players[player].hand = self.draw(7)
- #Set the first players turn.
- firstplayer = self.players.keys()[0]
- self.players[firstplayer].turn = 1
- def currentplayer(self):
- #Shortcut to see who's turn it is.
- return self.players.keys()[self.turn]
- if __name__ == '__main__':
- print "Making a new UNO game."
- unogame = UNO()
- #Adding players.
- unogame.addplayer('Blice')
- unogame.addplayer('moot')
- print "Starting the game."
- unogame.start()
- print "Current discard is: " + unogame.discard
Add Comment
Please, Sign In to add comment