import link
def hash(m1,h1,p1):
ha=""
if m1=="先手":ha=ha+"w"
if m1=="後手":ha=ha+"x"
if m1=="先手勝利":ha=ha+"y"
if m1=="後手勝利":ha=ha+"z"
for h2 in h1:
if h2=="先手":ha=ha+"p"
if h2=="後手":ha=ha+"q"
alp=["a","b","c","d","e","f","g","h","i","j"]
for p2 in p1:
ha=ha+alp[p2]
return ha
koma=["王","金","銀","王","金","銀"]
ban=[]
ban.append([0,0])
for y1 in [1,2,3]:
for x1 in [1,2,3]:
ban.append([x1,y1])
pos=[2,1,3,8,7,9]
hand=["先手","先手","先手","後手","後手","後手"]
mode="先手"
tr=0
dic=[]
gotox=[]
ha=hash(mode,hand,pos)
dic.append(ha)
game=0
while game<100:
dx=dic[tr]
don=0
if "y" in dx:don=100
if "z" in dx:don=100
if don<50:
sub=link.link()
sub.ban=ban
sub.koma=koma
sub.makedata(dx)
pol_hash=sub.pol_hash
sig=[]
for px in pol_hash:
h=0
if px in dic:h=100
if h<50:dic.append(px)
sig.append(px)
gotox.append(sig)
if don>50:gotox.append([])
tr=tr+1
if tr>10000:game=10000
f=open("dic.txt","w",encoding="utf-8")
for d in dic:
f.write(d)
f.write("\n")
f.close()
f1=open("link.txt","w",encoding="utf-8")
for gx in gotox:
for gxs in gx:
f1.write(gxs)
f1.write("\n")
f1.close()
import numpy as np
class link:
def hash(self,m1,h1,p1):
ha=""
if m1=="先手":ha=ha+"w"
if m1=="後手":ha=ha+"x"
if m1=="先手勝利":ha=ha+"y"
if m1=="後手勝利":ha=ha+"z"
for h2 in h1:
if h2=="先手":ha=ha+"p"
if h2=="後手":ha=ha+"q"
alp=["a","b","c","d","e","f","g","h","i","j"]
for p2 in p1:
ha=ha+alp[p2]
return ha
def decode(self,ha):
self.hand=[]
for i in range(6):
self.hand.append("")
self.pos=[]
for i in range(6):
self.pos.append(0)
if ha[0:1]=="w":self.mode="先手"
if ha[0:1]=="x":self.mode="後手"
if ha[0:1]=="y":self.mode="先手勝利"
if ha[0:1]=="z":self.mode="後手勝利"
for i in range(6):
if ha[i+1:i+2]=="p":self.hand[i]="先手"
if ha[i+1:i+2]=="q":self.hand[i]="後手"
for i in range(6):
if ha[i+7:i+8]=="a":self.pos[i]=0
if ha[i+7:i+8]=="b":self.pos[i]=1
if ha[i+7:i+8]=="c":self.pos[i]=2
if ha[i+7:i+8]=="d":self.pos[i]=3
if ha[i+7:i+8]=="e":self.pos[i]=4
if ha[i+7:i+8]=="f":self.pos[i]=5
if ha[i+7:i+8]=="g":self.pos[i]=6
if ha[i+7:i+8]=="h":self.pos[i]=7
if ha[i+7:i+8]=="i":self.pos[i]=8
if ha[i+7:i+8]=="j":self.pos[i]=9
return ha
def win(self,mode,h1):
mx=mode
mx1=mx
if mx1=="後手":mx="先手"
if mx1=="先手":mx="後手"
if h1[0]=="後手":mx="後手勝利"
if h1[3]=="先手":mx="先手勝利"
return mx
def rev(self):
pp=[0,0,0,0,0,0,0,0,0,0]
for i in range(6):
sx=self.pos_ch[i]
pp[sx]=pp[sx]+1
ch=0
for i in range(6):
h=0
sx=self.pos_ch[i]
if pp[sx]>1:h=100
if self.hand_ch[i]==self.mode:h=0
if h>50:self.hand_ch[i]=self.mode
if h>50:self.pos_ch[i]=0
def check(self,px):
pp=[0,0,0,0,0,0,0,0,0,0]
for i in range(6):
sx=px[i]
if self.hand[i]==self.mode:pp[sx]=pp[sx]+1
ch=0
for i in range(1,10):
if pp[i]>1:ch=100
return ch
def pointx(self,d):
m=-1
for i in range(10):
if self.ban[i]==d:
m=i
return m
def select(self):
koma_sel=[]
for u in range(6):
h=0
if self.hand[u]==self.mode:
h=h+1
if self.pos[u]>0:
h=h+1
if h==2:
koma_sel.append(u)
return koma_sel
def makedir(self,h1,k1):
dir=[]
if k1=="王":
dir.append([1,1])
dir.append([1,-1])
dir.append([-1,1])
dir.append([-1,-1])
dir.append([1,0])
dir.append([0,1])
dir.append([-1,0])
dir.append([0,-1])
h=0
if k1=="歩":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
h=0
if k1=="歩":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,-1])
h=0
if k1=="金":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,1])
dir.append([-1,1])
h=0
if k1=="金":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,-1])
dir.append([-1,-1])
h=0
if k1=="銀":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([1,1])
dir.append([-1,1])
dir.append([-1,-1])
dir.append([1,-1])
h=0
if k1=="銀":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,-1])
dir.append([1,-1])
dir.append([-1,-1])
dir.append([-1,1])
dir.append([1,1])
h=0
if k1=="桂":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([1,2])
dir.append([-1,2])
h=0
if k1=="桂":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([1,-2])
dir.append([-1,-2])
h=0
if k1=="香":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,2])
h=0
if k1=="香":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,-1])
dir.append([0,-2])
if k1=="飛":
dir.append([1,0])
dir.append([0,1])
dir.append([-1,0])
dir.append([0,-1])
dir.append([2,0])
dir.append([0,2])
dir.append([-2,0])
dir.append([0,-2])
if k1=="角":
dir.append([1,1])
dir.append([1,-1])
dir.append([-1,1])
dir.append([-1,-1])
dir.append([2,2])
dir.append([2,-2])
dir.append([-2,2])
dir.append([-2,-2])
h=0
if k1=="と":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,1])
dir.append([-1,1])
h=0
if k1=="と":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,-1])
dir.append([-1,-1])
h=0
if k1=="成香":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,1])
dir.append([-1,1])
h=0
if k1=="成香":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,-1])
dir.append([-1,-1])
h=0
if k1=="成桂":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,1])
dir.append([-1,1])
h=0
if k1=="成桂":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,-1])
dir.append([-1,-1])
h=0
if k1=="成銀":
h=h+1
if h1=="先手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,1])
dir.append([-1,1])
h=0
if k1=="成銀":
h=h+1
if h1=="後手":
h=h+1
if h==2:
dir.append([0,1])
dir.append([0,-1])
dir.append([1,0])
dir.append([-1,0])
dir.append([1,-1])
dir.append([-1,-1])
if k1=="竜王":
dir.append([1,1])
dir.append([1,-1])
dir.append([-1,1])
dir.append([-1,-1])
dir.append([1,0])
dir.append([0,1])
dir.append([-1,0])
dir.append([0,-1])
dir.append([2,0])
dir.append([0,2])
dir.append([-2,0])
dir.append([0,-2])
if k1=="竜馬":
dir.append([1,1])
dir.append([1,-1])
dir.append([-1,1])
dir.append([-1,-1])
dir.append([1,0])
dir.append([0,1])
dir.append([-1,0])
dir.append([0,-1])
dir.append([2,2])
dir.append([-2,-2])
dir.append([-2,2])
dir.append([2,-2])
return dir
def makedata(self,ha):
self.decode(ha)
koma_sel=self.select()
koma_pol=[]
pos_pol=[]
for n in koma_sel:
dx=self.makedir(self.hand[n],self.koma[n])
for d1 in dx:
p1=self.pos[n]
x1=self.ban[p1][0]+d1[0]
y1=self.ban[p1][1]+d1[1]
p2=self.pointx([x1,y1])
if p2>0:koma_pol.append(n)
if p2>0:pos_pol.append(p2)
sig=[]
for i in range(len(koma_pol)):
px=[0,0,0,0,0,0]
for j in range(6):
px[j]=self.pos[j]
p1=koma_pol[i]
p2=pos_pol[i]
px[p1]=p2
sig.append(px)
sigx=[]
for px in sig:
if self.check(px)<50:sigx.append(px)
pol_pos=[]
pol_hand=[]
pol_mode=[]
for px in sigx:
self.pos_ch=[]
self.hand_ch=[]
for i in range(6):
self.pos_ch.append(px[i])
self.hand_ch.append(self.hand[i])
self.rev()
pol_pos.append(self.pos_ch)
pol_hand.append(self.hand_ch)
mx=self.win(self.mode,self.hand_ch)
pol_mode.append(mx)
self.pol_hash=[]
for i in range(len(pol_mode)):
self.pol_hash.append(self.hash(pol_mode[i],pol_hand[i],pol_pos[i]))
最終更新:2018年02月22日 23:33