Skip to main content

Cards 11 - New code



So, the computer seems to have understood how to play not just if he can make points, but also if he must "let it go"... because he has not good cards... let's see the complete code... we will explain how it works lately

from random import shuffle

def val(card):
    'gives the value of the card as return value'
    valore = pcval[int(card[:-1])]
    return valore

def num(card):
    'gives the value of the card as return value'
    valore = int(card[:-1])
    return valore

def listcol(cards=[]):
    'returns a list of col from a list of cards'
    cardscol = []
    for i in cards:
        cardscol += [i[-1]]
    return cardscol

def start():
    'Create cards, shuffle them, pick pc1 (3 cards) and briscola, then choose the card to play'
    global cards,pc1,pc2,briscola,cardschoice,pc1_tbl,pcval,dictworst,otherseed
    cards = [str(num)+color for num in range(1,11) for color in ['♥','♦','♣','♠']]
    pcval = {1:11,2:0,3:10,4:0,5:0,6:0,7:0,8:2,9:3,10:4} # points of cards

    
    dictworst = {7:[2,4,5,6], 6:[2,4,5], 5:[2,4], 4:[2], 2:[11]} # cards to play with scartina
    otherseed = {7:[7,8,9,10],6:[6,7,8,9,10],5:[5,6,7,8,9],4:[4,5,6,7,8,9,10],2:[2,3,4,5,6,7,8,9,10]}
    
    shuffle(cards)
    briscola = cards.pop()
    pc1 = [cards.pop() for x in range(3)]
    pc2 = [cards.pop() for x in range(3)]
    cardschoice = []
    for c in pc1:
        card_color = c[-1]
        briscola_color = briscola[-1]
        match = card_color == briscola_color
        if not match:
            cardschoice += [c]     
    pc1v = []
    if cardschoice!=[]:
        for i in cardschoice:
            pc1v += [val(i)]                      
        pcmin = min(pc1v)
        pc1_tbl = cardschoice[pc1v.index(pcmin)]
    else:
        print("PC1 has only briscole and he does not know what to do, some code please!")

def isScart(cs):
    scartine = 2,4,5,6,7
    if num(cs) in scartine:
        return True
    else:
        return False

def isCarico(cs):
    carico = 1,3
    if num(cs) in carico:
        return True
    else:
        return False

def isFig(cs):
    figure = 8,9,10
    if num(cs) in figure:
        return True
    else:
        return False

def isBrisc(cs):
    if cs[-1] == briscola[-1]:
        return True
    else:
        return False

def take(cardontable,listofcards):
    'creates a list of cards to play against a card of the other player'
    seeds = ['♥','♦','♣','♠']
    seed = cardontable[-1]
    seeds.pop(seeds.index(briscola[-1]))
    worstnum = dictworst[num(cardontable)] 
    # create a list with the scartine of various colors
    worstlist = [str(x)+s for x in worstnum for s in seeds]
    worsts = otherseed[num(cardontable)]
    seeds.pop(seeds.index(cardontable[-1]))
    otherslist = [str(x)+s for x in worsts for s in seeds]                    
    # the cards that gives points
    wish = [str(x)+seed for x in listofcards]
    # adding the cards that don't gives points that you play if don't have the previous
    wish = wish + worstlist + otherslist
    print("Order of cards PC2 would play if he had:\n",wish)
    return wish


def pc2Turn():
    'the answer of pc2'
    if isBrisc(pc1_tbl):
        print("Pc1 throws a briscola ... there is not code for this yet... this will not be printed ....")
    else:
        print("PC1 never plays briscola until we don't code this move")
        if isScart(pc1_tbl):
            print("He throwed a low card of",pc1_tbl[-1])
            for c in take(pc1_tbl,[1,3,10,9,8]):
                if c in pc2:
                    print(c, "<== tiro di PC2")
                    break
                else:
                    pass
        elif isFig(pc1_tbl):
            print("PC1 played a figure... but he won't until we code this")
        elif isCarico(pc1_tbl):
            pass
        else:
            print("PC2 does not know what to play... write some code, please.")

def main():
    start()                 # cards,pc1,briscola
    print(briscola,"BRISCOLA")
    print(pc1, "PC1")
    print(pc2, "PC2")
    print(pc1_tbl,"<== tiro di PC1")
    pc2Turn()

if __name__=='__main__':
    main()
4♣ BRISCOLA
['6♠', '1♣', '5♥'] PC1
['4♥', '5♦', '9♦'] PC2
6♠ <== tiro di PC1
PC1 never plays briscola until we don't code this move
He throwed a low card of ♠
Order of cards PC2 would play if he had:
 ['1♠', '3♠', '10♠', '9♠', '8♠', '2♥', '2♦', '2♠', '4♥', '4♦', '4♠', '5♥', '5♦', '5♠', '6♥', '6♦', '7♥', '7♦', '8♥', '8♦', '9♥', '9♦', '10♥', '10♦']
4♥ <== tiro di PC2


to be continued ...

Comments