Skip to main content

Games of Cards II

Hi!

While I am having a little pause from the blog, I am still shaping sometime the code of Games of Cards. So, here it is with some other functions whose aim is to make the code more neat.

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 goodcards(card):
    'makes list of cards to make points'
    cpnt = [str(n)+card[-1] for n in [1,3,10,9,8]]
    return cpnt

def scartine():
    colors = ['♥','♦','♣','♠']
    colors.pop(colors.index(briscola[-1]))
    from2_10 = [str(x)+cc for x in [2,4,5,6,7,8,9,10] for cc in colors]
    return from2_10

def carichi():
    colors = ['♥','♦','♣','♠']
    colors.pop(colors.index(briscola[-1])) # Erase briscola
    carichi = [str(x)+cc for x in [3,1] for cc in colors]
    return carichi

def start():
    'Create cards, shuffle them, pick pc1 (3 cards) and briscola, then choose the card to play'
    global cards,pc1,pc2,briscola,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}   # dictonary for getting cards points    
    shuffle(cards)                                         # mixing cards
    briscola = cards.pop()
    pc1 = [cards.pop() for x in range(3)]
    pc2 = [cards.pop() for x in range(3)]
    print()
    print("PC1 cards",pc1,end='')

    for c in pc1_match1():
        print(">"+c,end='')
        if c in pc1:
            pc1_tbl = c
            print("*")
            if isBrisc(c):
                print("It is a briscola!")
            else:
                pass
            break

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 pc1_match1():
    'Card sequence for pc2 playing decision'
    scartine = [2,4,5,6,7,8,9,10]
    briscole = [2,4,5,6,7,8,9,10,3,1]
    carichi = [3,1]
    colori = ['♥','♦','♣','♠']
    colori.pop(colori.index(briscola[-1]))
    l_scartine = [str(x)+c for x in scartine for c in colori]
    l_briscole = [str(b)+briscola[-1] for b in briscole]
    l_carichi = [str(x)+c for x in carichi for c in colori]
    pc1_seq = l_scartine + l_briscole + l_carichi
    return pc1_seq
    
    
def pc2_match1():
    'Card sequence for pc2 playing decision'  
    listapunti = goodcards(pc1_tbl)
    listascartine = scartine()
    listabriscole = [str(x)+briscola[-1] for x in [2,4,5,6,8,7,9,10,3,1]]
    listacarichi = carichi()
    pc2_seq = listapunti + listascartine + listabriscole + listacarichi
    return pc2_seq


def pc2AI():
    'the answer of pc2'
    global pc2_tbl
    if isBrisc(pc1_tbl):
        print("I need code for this...")

    elif isScart(pc1_tbl):              # If pc1 play a 0 point card
        print("PC2 cards",pc2,end='')   # pc2 searcha a card of the same color that gives him points
        for c in pc2_match1():
            print(">"+c,end='')         # while he search we print the cards
            if c in pc2:
                print("*")              # when he finds a card in the list matching with pc2 cards... print *
                pc2_tbl=c
                print(c)
                break
            else:
                pass
        print("[PC1]",pc1_tbl,"[PC2]",pc2_tbl)
        print("Now we see who wins:")
    else:
        print("Code missing...")


def whoWins(a,b):
    'who wins between card a and b'
    print("[",a,b,"]     ",end ='')
    if a[-1]==b[-1]:
        if val(a)>=val(b):
            print("Pc1 wins with",a)
            return a
        else:
            print("Pc2 wins with",b)
            return b
    else:
        if b[-1]==briscola[-1]:
            print("[Pc2 wins with a briscola]",b)
            return b
        else:
            print("Pc1 wins with",a)
            return(a)
        
        
def main():
    start()
    # pc1choosefirst()
    # pc2answers()
    # 
    pc2AI()
    whoWins(pc1_tbl, pc2_tbl)
    
if __name__=='__main__':
    main()
PC1 cards ['6♣', '3♠', '1♥']>2♥>2♦>2♣>4♥>4♦>4♣>5♥>5♦>5♣>6♥>6♦>6♣*
PC2 cards ['1♣', '9♠', '10♥']>1♣*
1♣
[PC1] 6♣ [PC2] 1♣
Now we see who wins:
[ 6♣ 1♣ ]     Pc2 wins with 1♣

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.