Skip to main content

Shaping Computer's AI: cards 10


Let's give another closer look to the code we could use to make the computer decide what to play.

We have two players:
pc1 and pc2

let's say that pc1 has thrown 2
pc2 has ['4♥', '10♥', '3♣']

if we were pc2 we should play 10♥

How we can make it happen that the computer throws 10♥ ... we could check for the highest value with the same seed ♥... but if we have a 1♥ we should look for the points that 1 can make me earn (11) and not at the number (1). Same thing for 3... and there could be also 7♥ that is higher than 2♥, but this won't give me points as 7 gives 0 points... so I should write code do avoid this... it's a bit too long... so I decide to do another thing... wich is the best card I wish I have if pc1 plays a card of seed ♥? It is 1♥ and after that? 3♥ and so on until 8♥... after the 8♥ we have no points, so I stop the wishcards there.... so I make a list taking the seed ♥ (or any other seed that will come with the card that I pass as argument) and attaching to the seed the 1,3,10,9,8 values in this order. After that I check if pc2 has one of this iterating the list in that order. The first card I find is the best one and I break the code and return this value.

What if pc2 has no good card... we figure it out lately.



def checkpoints(scart,mycards):
    global mycard
    mycard = mycards
    'returns a card that can make you earn points'
    print("If pc1 throws this card:",scart)
    print("And pc2 has this cards:",mycards)
    print("We look at the seed, that is, ", scart[-1])
    colore = scart[-1] # here we take the seed of the card
    wishcards = [x+colore for x in ["1","3","10","9","8"]] # here we creare a list with cards from highest point to lower
    print("This are the cards that can make pc2 earn points:",wishcards)
    print("So... after chasing in the list of wishcards from best to last...")
    for card in wishcards:
        if card in mycards:
            break
    return "This is what pc2 must play:" + card


print(checkpoints("2♥",["4♥","10♥","3♣"]))

If pc1 throws this card: 2♥
And pc2 has this cards: ['4♥', '10♥', '3♣']
We look at the seed, that is,  ♥
This are the cards that can make pc2 earn points: ['1♥', '3♥', '10♥', '9♥', '8♥']
So... after chasing in the list of wishcards from best to last...
This is what pc2 must play:10♥

[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.