Skip to main content

Who wins

Well, we want to make something different this time. We want to make the computer decide with another system... trying to see wich is the best decision for him, instead of doing lists of choices, from the best to the worst like we did before. So, to start doing this, let's create a function that is able to understand who wins among two cards... this will be the foundation of a new way of "thinking" for the pc2 in answer to the card of pc1... In the output the last card of each row is the card that wins the match.




# different way of choosing a card, making computer think what has more worth

from random import shuffle

def start():
    global cards,pcval,briscola,pc1
    cards = [str(num)+color for num in range(1,11) for color in ['♥','♦','♣','♠']]
    pcval = {1:11,2:-4,3:10,4:-3,5:-2,6:-1,7:1,8:2,9:3,10:4} # points of cards 
    shuffle(cards)
    briscola = cards.pop()
    pc1 = [cards.pop() for x in range(3)]

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

def whoWins(a,b):
    'who wins between card a and b'
    global x
    print("Briscola:"+briscola,end=',"..............')
    print("[",a,b,"]     ",end ='')
    if a[-1]==b[-1]:
        if val(a)>=val(b):
            print("",a)
            return a
        else:
            print("[*]",b)
            return b
    else:
        if b[-1]==briscola[-1]:
            print("[**]",b)
            return b
        else:
            print("",a)
            return(a)


# testiamo pc1match
def main():
    start()
    print(briscola)
    print(pc1)
    print(pc1match1())

for i in range(20):
    start()
    whoWins(cards.pop(), cards.pop())
print("[*] = wins PC2 with a greater card")
print("[**] = wins PC2 with a briscola card")
Briscola:4♣,"..............[ 8♥ 3♥ ]     [*] 3♥
Briscola:7♥,"..............[ 6♣ 4♦ ]      6♣
Briscola:8♥,"..............[ 7♠ 4♣ ]      7♠
Briscola:8♦,"..............[ 1♠ 10♦ ]     [**] 10♦
Briscola:10♣,"..............[ 5♣ 8♣ ]     [*] 8♣
Briscola:1♣,"..............[ 2♣ 7♦ ]      2♣
Briscola:6♥,"..............[ 4♣ 10♣ ]     [*] 10♣
Briscola:1♦,"..............[ 10♦ 4♦ ]      10♦
Briscola:2♥,"..............[ 5♥ 5♠ ]      5♥
Briscola:10♣,"..............[ 8♦ 7♣ ]     [**] 7♣
Briscola:2♣,"..............[ 1♣ 3♠ ]      1♣
Briscola:3♥,"..............[ 9♠ 10♥ ]     [**] 10♥
Briscola:1♦,"..............[ 8♠ 2♥ ]      8♠
Briscola:10♦,"..............[ 2♥ 5♣ ]      2♥
Briscola:4♥,"..............[ 3♣ 7♥ ]     [**] 7♥
Briscola:9♥,"..............[ 10♣ 4♣ ]      10♣
Briscola:4♥,"..............[ 1♥ 9♥ ]      1♥
Briscola:3♥,"..............[ 9♣ 8♦ ]      9♣
Briscola:6♥,"..............[ 7♠ 2♠ ]      7♠
Briscola:6♣,"..............[ 3♠ 9♣ ]     [**] 9♣
[*] = wins PC2 with a greater card
[**] = wins PC2 with a briscola card

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.