Now that we've defined the required principal functions, we'll implement them slowly!
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.
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.
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
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! !!
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
#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> <
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.
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 ...
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