Skip to main content

Cards 12 - New Ai for pc1 too


Ok, so... now the pc1 has his decision list made... in a similar way that was done for pc2...
pc1 checks for a little card... if not goes into the briscole... if not he is obliged to play a high card... so we covered almost all decision making in the first hand of the game for pc1.

After that the pc2 plays his cards... for now, he is able to answer only to cards with low values... but next time we will complete the first match at least... then I think we will stop and go forward with other pythonic coding style.

# briscola 3

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    
    shuffle(cards)
    briscola = cards.pop()
    pc1 = [cards.pop() for x in range(3)]
    pc2 = [cards.pop() for x in range(3)]
    print(briscola,"BRISCOLA")
    print()
    print("[PC1]")
    print("His cards",pc1)

    for c in pc1_match1():
        print("Looking for a...",c)
        if c in pc1:
            pc1_tbl = c
            print("********* found! ************")
            if pc1_tbl[-1]==briscola[-1]:
                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)+color for x in scartine for color in colori]
    l_briscole = [str(x)+briscola[-1] for x in briscole]
    l_carichi = [str(x)+color for x in carichi for color in colori]
    pc1_seq = l_scartine + l_briscole + l_carichi
    return pc1_seq
    
    
def pc2_match1():
    'Card sequence for pc2 playing decision'  
    cardscolors = ['♥','♦','♣','♠']
    cardscolors.pop(cardscolors.index(briscola[-1])) # Erase briscola
    cardswithpoints = [str(x)+pc1_tbl[-1] for x in [1,3,10,9,8]]      # List victory n points
    two_ten = [str(x)+cc for x in [2,4,5,6,7,8,9,10] for cc in cardscolors] # List with no points
    briscole = [str(x)+briscola[-1] for x in [2,4,5,6,8,7,9,10,3,1]]       # goodbye briscole
    cardscolors.pop(cardscolors.index(pc1_tbl[-1])) # Erase briscola
    carichi = [str(x)+cc for x in [3,1] for cc in cardscolors]
    pc2_seq = cardswithpoints + two_ten + briscole + carichi
    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):
        print("[PC2]\nHis cards",pc2)
        for c in pc2_match1():
            print("Looking for a",c)
            if c in pc2:
                print("********* found! ************")
                pc2_tbl=c
                print(c)
                break
            else:
                pass
        print("[PC1]",pc1_tbl,"[PC2]",pc2_tbl)
        print("Now we need code to see who wins")
    else:
        print("Code missing...")


def main():
    start()
    pc2AI()

if __name__=='__main__':
    main()
3♠ BRISCOLA

[PC1]
His cards ['10♣', '9♣', '6♣']
Looking for a... 2♥
Looking for a... 2♦
Looking for a... 2♣
Looking for a... 4♥
Looking for a... 4♦
Looking for a... 4♣
Looking for a... 5♥
Looking for a... 5♦
Looking for a... 5♣
Looking for a... 6♥
Looking for a... 6♦
Looking for a... 6♣
********* found! ************
[PC2]
His cards ['9♦', '7♥', '3♦']
Looking for a 1♣
Looking for a 3♣
Looking for a 10♣
Looking for a 9♣
Looking for a 8♣
Looking for a 2♥
Looking for a 2♦
Looking for a 2♣
Looking for a 4♥
Looking for a 4♦
Looking for a 4♣
Looking for a 5♥
Looking for a 5♦
Looking for a 5♣
Looking for a 6♥
Looking for a 6♦
Looking for a 6♣
Looking for a 7♥
********* found! ************
7♥
[PC1] 6♣ [PC2] 7♥
Now we need code to see who wins

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.