# -*- coding: UTF-8 -*-

import random

class Card:
        def __init__(self, shape=None, number=None):
                self.shape = shape
                self.number = number
        def __str__(self):
                s = '%s %s' % (self.shape, self.number)
                return s

def shuffleCards():
        cards = []
        cardsNums = range(2,11) + ['J', 'Q', 'K', 'Q', 'A']
        cardShapes = ['â~W~F','â~Y| ','â~Y£','â~Y¥']

        for number in cardsNums:
                for shape in cardShapes:
                        cards.append(Card(shape, number))

        random.shuffle(cards)
        return cards

def printCardDeck(pcCards, myCards, cardOnTable):
        jobs = [pcCards, [cardOnTable], myCards]

        for job in jobs:
                if job != None:
                        printDeck(job)

def printDeck(cards):
        leftUp = 'â~T~L'
        post = 'â~T~B'
        rightUp = 'â~T~P'
        leftDown = 'â~T~T'
        rightDown = 'â~T~X'

        crossBar = 'â~T~@'*len(cards)*3
        allCards = ''

        for card in cards:
                allCards += str(card) + ', '

        allCards = allCards[0:len(allCards)-2]

        upperCrossBar = leftUp+crossBar+rightUp
        lowerCrossBar = leftDown+crossBar+rightDown
        spaces = ' ' * (len(upperCrossBar)-len(allCards)-4)
        allCards += spaces

        print upperCrossBar
        print '%s%s%s' % (post, allCards, post)
        print lowerCrossBar

##############################################################
def game():
        cards =  shuffleCards()

        pcCards = cards[1:10]
        myCards = cards[11:20]
        cardOnTable = cards[0]

        numCardsRemoved = 0
        printCardDeck(pcCards, myCards, cardOnTable)

        while not (len(pcCards)==0 or len(myCards)==0):
                cardOnTable, myResult = userProc(cardOnTable, myCards)
                cardOnTable, pcResult = pcProc(cardOnTable, pcCards)

                numCardsRemoved = myResult + pcResult

                if numCardsRemoved == 0:
                        break

                printCardDeck(pcCards, myCards, cardOnTable)

def userProc(cardOnTable, myCards):
        cnt = 0
        while True:
                select = input('card index (-1:skip) : ')

                if select == -1 :
                        return cardOnTable, cnt

                if not canHandOut(cardOnTable, myCards[select]):
                        print 'you cannot handout that card'
                        continue
                else:
                        cardOnTable = myCards.pop(select)
                        cnt += 1
                        printDeck([cardOnTable])
                        printDeck(myCards)
                        continue

def pcProc(cardOnTable, pcCards):
        cnt = 0
        removed = False
        while True:
                for index in range(0, len(pcCards)):
                        if canHandOut(cardOnTable, pcCards[index]):
                                cardOnTable = pcCards.pop(index)
                                removed = True
                                break
                if removed:
                        removed = False
                        continue

                return cardOnTable, cnt

def canHandOut(cardOnTable, myCard):
        if cardOnTable.shape==myCard.shape or cardOnTable.number==myCard.number:
                return True
        else:
                return False
##############################################################

if __name__=='__main__':
        game()