import numpy as np
class game:
def decode(self,ha):
self.mode=""
m1=ha[0:1]
if m1=="w":self.mode="先手"
if m1=="x":self.mode="後手"
if m1=="y":self.mode="先手勝利"
if m1=="z":self.mode="後手勝利"
self.hand=[]
for i in range(6):
if ha[i+1]=="p":self.hand.append("先手")
if ha[i+1]=="q":self.hand.append("後手")
self.pos=[]
for i in range(6):
if ha[i+7]=="a":self.pos.append(0)
if ha[i+7]=="b":self.pos.append(1)
if ha[i+7]=="c":self.pos.append(2)
if ha[i+7]=="d":self.pos.append(3)
if ha[i+7]=="e":self.pos.append(4)
if ha[i+7]=="f":self.pos.append(5)
if ha[i+7]=="g":self.pos.append(6)
if ha[i+7]=="h":self.pos.append(7)
if ha[i+7]=="i":self.pos.append(8)
if ha[i+7]=="j":self.pos.append(9)
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 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):
self.decode(self.hashx)
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月28日 09:51