[Final story] Beginners tried Numeron AI with python

0th 1st 2nd 3rd Final story

About this article

Now that we've defined the required principal functions, we'll implement them slowly!

Creating a battle environment

For the time being, the following is a summary of the functions created so far and the determination of player numbers.

import random

def NUMERON(CALL,ANS):
    EAT=0
    BITE=0
    for i in range(3):
        if CALL[i]==ANS[i]:
            EAT+=1
        elif CALL[i] in ANS and CALL[i]!=ANS[i]:
            BITE+=1
    return [EAT,BITE]

def change(a,b,c):
    return [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]]
ALL=[]
for i in range(0,10):
    for j in range(i+1,10):
        for k in range(j+1,10):
            ALL+=change(i,j,k)

EB=[[3,0],[2,1],[2,0],[1,2],[1,1],[1,0],[0,3],[0,2],[0,1],[0,0]]

def CHECK(CALL,EAT,BITE,KOUHO):
    X=len(KOUHO)
    group=[]
    for i in range(X):
        if NUMERON(CALL,KOUHO[i])==[EAT,BITE]:
            group.append(KOUHO[i])
    return group

def BAD(CALL,KOUHO):
    CHECK_LIST=[0]*10
    for i in range(10):
        EAT=EB[i][0]
        BITE=EB[i][1]
        CHECK_LIST[i]=len(CHECK(CALL,EAT,BITE,KOUHO))
    return max(CHECK_LIST)

def CALL_LIST(KOUHO):
    LIST=[]
    EATLIST=[]
    cnt=1000
    for i in range(720):
        if BAD(ALL[i],KOUHO)<cnt:
            cnt=BAD(ALL[i],KOUHO)
            LIST=[]
            EATLIST=[]
            if len(CHECK(ALL[i],3,0,KOUHO))>=1:
                EATLIST.append(ALL[i])
            else:
                LIST.append(ALL[i])
        elif BAD(ALL[i],KOUHO)==cnt:
            if len(CHECK(ALL[i],3,0,KOUHO))>=1:
                EATLIST.append(ALL[i])
            else:
                LIST.append(ALL[i])
    if len(EATLIST)==0:
        return LIST
    else:
        return EATLIST

NUMBERS=[0,1,2,3,4,5,6,7,8,9]
random.shuffle(NUMBERS)
com=NUMBERS[0:3]

for i in range(10**9):
    print("Please enter your favorite 3-digit number with all numbers different")
    X=input()
    if len(X) != 3:
        print("Please use 3 digits")
    else:
        if int(X[0]) in NUMBERS and int(X[1]) in NUMBERS and int(X[2]) in NUMBERS:
            if X[0] == X[1] or X[0] == X[2] or X[2] == X[3]:
                print("Please enter all three numbers differently")
            else:
                player=[int(X[0]) , int(X[1]) , int(X[2])]
                break
        else:
            print("Please use a 3-digit "number"")

Let's continue writing the program.

Manage your turn

The turn is I will manage it with a variable called teban teban = 1: First move teban = -1: It will be the second turn. When the turn of each turn is over, you can manage it by setting the turn * -1.

So the continuation of the above program is as follows

for i in range(10*9):
   if teban==1:
First move processing

When finished, teban*-1
   else:
Post-turn processing

When finished, teban*-1

Therefore, all you have to do is create the first move (player) process and the second move (com) process.

First move processing

Let's write concretely. But this is easy "Enter the number you want to call ()" And Display "EAT, BITE" That's it. Let's write a little

        print("It's a player call")
        x=input()
        CALL=[int(x[0]),int(x[1]),int(x[2])]
        X=NUMERON(CALL,com)
        print(str(X[0])+"EAT//"+str(X[1])+"BITE")
        if X==[3,0]:
            print("player wins")
            exit()
        teban*=(-1)

This is fine. Let's go after the problem

Post-turn processing

The big difference is that the number to call is not input (). However, since I defined a function that lists the numbers to be called last time, it is ok to choose the numbers But as you all know, python is slow. If you check 720 pieces, you will have to wait a long time. So I'll just say the numbers randomly for the first move! (Because the first move doesn't change no matter what you say)

Based on that, the processing of the second turn is

        if count==0:
            random.shuffle(KOUHO)
            x=KOUHO[0]
            count+=1
        else:
            com_CALL=CALL_LIST(KOUHO)
            random.shuffle(com_CALL)
            x=com_CALL[0]
        print("It is a call of com")
        print(x)
        X=NUMERON(x,player)
        KOUHO=CHECK(x,X[0],X[1],KOUHO)
        print(str(X[0])+"EAT//"+str(X[1])+"BITE")
        if X==[3,0]:
            print("com victory")
            exit()
        else:
            print("remaining"+str(len(KOUHO))+"Street")

That's all there is to it! !!

Completion program

import random

def NUMERON(CALL,ANS):
    EAT=0
    BITE=0
    for i in range(3):
        if CALL[i]==ANS[i]:
            EAT+=1
        elif CALL[i] in ANS and CALL[i]!=ANS[i]:
            BITE+=1
    return [EAT,BITE]

def change(a,b,c):
    return [[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]]
ALL=[]
for i in range(0,10):
    for j in range(i+1,10):
        for k in range(j+1,10):
            ALL+=change(i,j,k)

EB=[[3,0],[2,1],[2,0],[1,2],[1,1],[1,0],[0,3],[0,2],[0,1],[0,0]]

def CHECK(CALL,EAT,BITE,KOUHO):
    X=len(KOUHO)
    group=[]
    for i in range(X):
        if NUMERON(CALL,KOUHO[i])==[EAT,BITE]:
            group.append(KOUHO[i])
    return group

def BAD(CALL,KOUHO):
    CHECK_LIST=[0]*10
    for i in range(10):
        EAT=EB[i][0]
        BITE=EB[i][1]
        CHECK_LIST[i]=len(CHECK(CALL,EAT,BITE,KOUHO))
    return max(CHECK_LIST)

def CALL_LIST(KOUHO):
    LIST=[]
    EATLIST=[]
    cnt=1000
    for i in range(720):
        if BAD(ALL[i],KOUHO)<cnt:
            cnt=BAD(ALL[i],KOUHO)
            LIST=[]
            EATLIST=[]
            if len(CHECK(ALL[i],3,0,KOUHO))>=1:
                EATLIST.append(ALL[i])
            else:
                LIST.append(ALL[i])
        elif BAD(ALL[i],KOUHO)==cnt:
            if len(CHECK(ALL[i],3,0,KOUHO))>=1:
                EATLIST.append(ALL[i])
            else:
                LIST.append(ALL[i])
    if len(EATLIST)==0:
        return LIST
    else:
        return EATLIST

NUMBERS=[0,1,2,3,4,5,6,7,8,9]
random.shuffle(NUMBERS)
com=NUMBERS[0:3]

for i in range(10**9):
    print("Please enter your favorite 3-digit number with all numbers different")
    X=input()
    if len(X) != 3:
        print("Please use 3 digits")
    else:
        if int(X[0]) in NUMBERS and int(X[1]) in NUMBERS and int(X[2]) in NUMBERS:
            if X[0] == X[1] or X[0] == X[2] or X[1] == X[2]:
                print("Please enter all three numbers differently")
            else:
                player=[int(X[0]) , int(X[1]) , int(X[2])]
                break
        else:
            print("Please use a 3-digit "number"")
teban=1
KOUHO=ALL
count=0
for i in range(10**9):
    if teban==1:
        print("It's a player call")
        x=input()
        CALL=[int(x[0]),int(x[1]),int(x[2])]
        X=NUMERON(CALL,com)
        print(str(X[0])+"EAT//"+str(X[1])+"BITE")
        if X==[3,0]:
            print("player wins")
            exit()
        teban*=(-1)
    else:
        if count==0:
            random.shuffle(KOUHO)
            x=KOUHO[0]
            count+=1
        else:
            com_CALL=CALL_LIST(KOUHO)
            random.shuffle(com_CALL)
            x=com_CALL[0]
        print("It is a call of com")
        print(x)
        X=NUMERON(x,player)
        KOUHO=CHECK(x,X[0],X[1],KOUHO)
        print(str(X[0])+"EAT//"+str(X[1])+"BITE")
        if X==[3,0]:
            print("com victory")
            exit()
        else:
            print("remaining"+str(len(KOUHO))+"Street")
        teban*=(-1)

Let's play a little!

Let's play a little

#Please enter your favorite 3-digit number with all numbers different
017

And it's the call's turn

#It's a player call
012
#1EAT//0BITE
#It is a call of com
#[2, 7, 3]
#0EAT//1BITE
#252 remaining streets

I'm 1-0, so it should be superior to com. Go at 034

#It's a player call
034
#0EAT//0BITE
#It is a call of com
#[5, 3, 7]
#1EAT//0BITE
#72 ways left

034 is 0-0, isn't it? In other words, it is x1y or xy2 and 0,3,4 is not used. The other party is narrowing down to 72 ways, so you can not be alert Go at 567

#It's a player call
567
#0EAT//1BITE
#It is a call of com
#[0, 6, 7]
#2EAT//0BITE
#8 ways left

Don't play around with the remaining 8 ways () To summarize the information 1,2 5,6,7 8,9 One is used from each ... Go to the doggy at 618.

#It's a player call
618
#2EAT//0BITE
#It is a call of com
#[0, 4, 1]
#1EAT//1BITE
#1 way left

It seems that you can hit the opponent next turn () However, I am also cornering. If 6 and 8 are EATs, a contradiction will occur, so 1 EAT is confirmed. In other words, there are two choices, 619 or 718 (probably) Go by 718

#It's a player call
718
#3EAT//0BITE
#player wins

Oh, I won. (I can win)

Well, I'm happy that it works as it should> <

Finally

It was the first time to create AI (Is it okay to say AI? I don't understand the technical terms well), but it was quite good. I'm satisfied with it because I can guess it 6 times. I thought that if I did my best even for one month of program history, I would be able to do something about it.

Task

However, the second call is slow anyway. If the first move is 0-1 there are 252 candidates, so When you think about your next move, you're doing 720 * 10 * 252 = 1814400 calculations internally. From now on, my goal is to study a lot of algorithms and reduce the amount of calculation. At this rate, 4-digit AI will be difficult ...

Summary

Thank you for browsing so far. I was confident that I was able to program to the end even though it was not connected. However, I would like to become stronger in the program, so I would appreciate it if you could give me a comment with the love of guidance and encouragement.

Thank you for your relationship! !!

Recommended Posts

[Final story] Beginners tried Numeron AI with python
[Episode 2] Beginners tried Numeron AI with python
[Episode 0] Beginners tried Numeron AI with python
[Episode 1] Beginners tried Numeron AI with python
3. 3. AI programming with Python
I tried fp-growth with python
I tried scraping with Python
Make Puyo Puyo AI with Python
Stumble story with Python array
I tried gRPC with Python
I tried scraping with python
[Ipdb] Web development beginners tried to summarize debugging with Python
I tried web scraping with python.
[Small story] Get timestamp with Python
I tried running prolog with python 3.8.2.
python beginners tried to find out
I tried SMTP communication with Python
I tried to solve the ant book beginner's edition with python
[Pandas] I tried to analyze sales data with Python [For beginners]
INSERT into MySQL with Python [For beginners]
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried scraping Yahoo News with Python
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
How Python beginners get started with Python with Progete
Build AI / machine learning environment with Python
I refactored "I tried to make Othello AI when programming beginners studied python"
[For beginners] Try web scraping with Python
#I tried something like Vlookup with Python # 2
Machine learning beginners tried to make a horse racing prediction model with python
I tried to predict next year with AI
I tried hundreds of millions of SQLite with python
Causal reasoning and causal search with Python (for beginners)
I tried "differentiating" the image with Python + OpenCV
[Python3] A story stuck with time zone conversion
Python beginners tried to code some energy drinks
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
~ Tips for Python beginners from Pythonista with love ① ~
I tried function synthesis and curry with python
A story stuck with handling Python binary data
I tried to output LLVM IR with Python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python
I tried playing mahjong with Python (single mahjong edition)
[Small story] Test image generation with Python / OpenCV
What Python beginners got hooked on with Django
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
~ Tips for Python beginners from Pythonista with love ② ~
[Introduction for beginners] Working with MySQL in Python
Basic story of inheritance in Python (for beginners)
FizzBuzz with Python3
Scraping with Python