Skip to main content

Cards 9 (some more code in python)


Ok, so... we continue to code about our Briscola game... I will explain the rule of the game and this code later... for now I will give you this code that is partially working... we got to cover a lot of stuff... but the aim of this is to get use to the pythonic style of coding (according to me, of course).

I wish you have installed the jupyter notebook, because is very nice. Another advise is to use Sublime Text 3 (but any text editor is good there are a lot of great editors for free and even IDLE, that comes with python, is very good).

It's a bit longer, but I think I will make a lot of changes in the future. PC2 doesn' know always what to play, but I choose a case in wich he won... but if you try this code you will see that he doesn't know what card to throw... we will fix it.


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
    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}
    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 = []
    for i in cardschoice:
        pc1v += [val(i)]                      
    pcmin = min(pc1v)
    pc1_tbl = cardschoice[pc1v.index(pcmin)]

def whatIsIt(cs):
    'sort of switch'
    defs = {1:"Ha buttato un asso",
            2:"scartina",
            3:"Ha buttato un 3",
            4:"Scartina",
            5:"scartina",
            6:"scartina",
            7:"scartina",
            8:"Sono due punti",
            9:"Sono tre punti",
            10:"Sono 4 punti"}
    for value in defs:
        if value == cs:
            print(defs[cs])
    return defs[cs]

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

def isCar(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 cartePunti(card):
    'takes the color of the card and return a list of cards with points to answer to a scartina'
    colore = card[-1]
    cartep = [x+colore for x in ["1","3","10","9","8"]]
    return cartep


def pc2Turn():
    'the answer of pc2'
    if isBrisc(pc1_tbl):
        print("Pc1 throws a briscola ... to be continued ....")
    else:
        print("It is not a briscola")
        if isScart(pc1_tbl):
            print("He throwed a low card of",pc1_tbl[-1])
            for c in cartePunti(pc1_tbl):
                if c in pc2:
                    print(c, "<== tiro di PC2")
                    break
                else:
                    print("*")
        else:
            print("He throws a high card of a figure")

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()
8♥ BRISCOLA
['4♠', '4♣', '6♦'] PC1
['3♣', '9♥', '8♠'] PC2
4♠ <== tiro di PC1
It is not a briscola
He throwed a low card of ♠
*
*
*
*
8♠ <== tiro di PC2





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.