Skip to content

Instantly share code, notes, and snippets.

@jamad
Last active March 26, 2020 14:03
Show Gist options
  • Save jamad/6dc4eae34a1375aa80135ff446d971a3 to your computer and use it in GitHub Desktop.
Save jamad/6dc4eae34a1375aa80135ff446d971a3 to your computer and use it in GitHub Desktop.
def is_check(B):
def f(r,c,u,v):
T=[]
for i in range(1,8):
p=(r+i*u,c+i*v)
if D.get(p,' ')in'♛♝♞♜♟':break
T+=[p]
return T
D={}
for r in range(8):
for c in range(8):
if B[r][c]=='♔':k=(r,c)
D[(r,c)]=B[r][c]
for r,c in D:
S,e=[],D[(r,c)]
if e=='♛':S+=[(r-1,c-1),(r-1,c),(r-1,c+1),(r,c-1),(r,c+1),(r+1,c-1),(r+1,c),(r+1,c+1)]
if e in'♝♛':S+=f(r,c,1,1)+f(r,c,-1,-1)+f(r,c,-1,1)+f(r,c,1,-1)
if e=='♞':S+=[(r-2,c-1),(r-2,c+1),(r-1,c+2),(r+1,c+2),(r+2,c-1),(r+2,c+1),(r-1,c-2),(r+1,c-2)]
if e in'♜♛':S+=f(r,c,1,0)+f(r,c,-1,0)+f(r,c,0,1)+f(r,c,0,-1)
if e=='♟':S+=[(r+1,c-1),(r+1,c+1)]
if k in S:return 1
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment