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

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.