import numpy as np
import game
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="先手"
dic=[""]
point=[50]
sub=game.game()
sub.koma=koma
sub.pos=pos
sub.ban=ban
sub.hand=hand
sub.mode=mode
sub.dic=dic
sub.point=point
sub.makedata()
pos=sub.pos
mode=sub.mode
hand=sub.hand
print(mode)
import numpy as np
class game:
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 change(self,px):
pp=[0,0,0,0,0,0,0,0,0,0]
for i in range(6):
sx=px[i]
pp[sx]=pp[sx]+1
chx=0
for i in range(1,10):
if pp[i]>1:chx=i
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):
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)
pol_hash=[]
for i in range(len(pol_mode)):
pol_hash.append(self.hash(pol_mode[i],pol_hand[i],pol_pos[i]))
pol_point=[]
for hh in pol_hash:
sxs=0
if hh in self.dic:sxs=self.dic.index(hh)
pol_point.append(self.point[sxs])
q=[]
for i in range(len(pol_point)):
p1=pol_point[i]
for j in range(p1):
q.append(i)
sxs=np.random.choice(q)
self.hand=pol_hand[sxs]
self.pos=pol_pos[sxs]
self.mode=pol_mode[sxs]
最終更新:2018年02月17日 18:42