parties_echecs.py

Created by caucaucybu

Created on September 16, 2023

8.71 KB

Script complétant le script echecs.py.


try:from pieces_echecs import *
except ImportError:pass
def echec(x,y,tour,xy=0):
  for i in chpsb(x,y,"f",tour,0):
    if len(i)==3 and (i[2]=="f" or i[2]=="r"):
      if (i[0],i[1])!=xy:return 0
      else:break
  for i in chpsb(x,y,"t",tour,0):
    if len(i)==3 and (i[2]=="t" or i[2]=="r"):
      if (i[0],i[1])!=xy:return 0
      else:break
  for i in chpsb(x,y,"c",tour,0):
    if len(i)==3 and i[2]=="c":
      if (i[0],i[1])!=xy:return 0
      else:break
  for i in chpsb(x,y,"k",tour,0):
    if len(i)==3 and i[2]=="k":
      if (i[0],i[1])!=xy:return 0
      else:break
  for i in chpsb(x,y,"p",tour,0):
    if len(i)==3 and i[2]=="p":
      if (i[0],i[1])!=xy:return 0
      else:break
  return 1
def chpsb(x,y,typ,tour,ecs=1):
  try:from echecs import rb,rn,rbb,rnb,tgb,tgn,tdb,tdn
  except ImportError:from echecs_simp import rb,rn,rbb,rnb,tgb,tgn,tdb,tdn
  psb=[]
  if not tour:rt=(rn[0],rn[1])
  else:rt=(rb[0],rb[1])
  if typ=="p":
    if not tour:
      if not chercher(x-1,y):
        if ecs:pionn(x-1,y);ech=echec(rt[0],rt[1],tour,(x-1,y));depiece(x-1,y)
        else:ech=1
        if ech:psb.append((x-1,y))
        if x==6 and not chercher(x-2,y):
          if ecs:pionn(x-2,y);ech=echec(rt[0],rt[1],tour,(x-2,y));depiece(x-2,y)
          else:ech=1
          if ech:psb.append((x-2,y))
      for i in (-1,1):
        test=chercher(x-1,y+i)
        if inplt(y+i) and test and test[1]==-1:
          if ecs:ech=echec(rt[0],rt[1],tour,(x-1,y+i))
          else:ech=1
          if ech:psb.append((x-1,y+i,test[0]))
        if ecs and get(xn(x),yn(y+i))==grey and get(xn(x-2),yn(y+i))==grey and chercher(x,y+i)==("p",-1):
          depiece(x,y+i);pionn(x-1,y+i);ech=echec(rt[0],rt[1],tour);pionb(x,y+i);depiece(x-1,y+i)
          if ech:psb.append((x-1,y+i,"p",x,y+i))
    else:
      if not chercher(x+1,y):
        if ecs:pionb(x+1,y);ech=echec(rt[0],rt[1],tour,(x+1,y));depiece(x+1,y)
        else:ech=1
        if ech:psb.append((x+1,y))
        if x==1 and not chercher(x+2,y):
          if ecs:pionb(x+2,y);ech=echec(rt[0],rt[1],tour,(x+2,y));depiece(x+2,y)
          else:ech=1
          if ech:psb.append((x+2,y))
      for i in (-1,1):
        test=chercher(x+1,y+i)
        if inplt(y+i) and test and test[1]==0:
          if ecs:ech=echec(rt[0],rt[1],tour,(x+1,y+i))
          else:ech=1
          if ech:psb.append((x+1,y+i,test[0]))
        if ecs and get(xn(x),yn(y+i))==grey and get(xn(x+2),yn(y+i))==grey and chercher(x,y+i)==("p",0):
          depiece(x,y+i);pionn(x+1,y+i);ech=echec(rt[0],rt[1],tour);pionb(x,y+i);depiece(x+1,y+i)
          if ech:psb.append((x+1,y+i,"p",x,y+i))
  if typ=="t":
    x1=x+1;x2=x-1;y1=y+1;y2=y-1
    while x1<8:
      test=chercher(x1,y)
      if not test:
        if ecs:
          if not tour:tourn(x1,y)
          else:tourb(x1,y)
          ech=echec(rt[0],rt[1],tour,(x1,y));depiece(x1,y)
        else:ech=1
        if ech:psb.append((x1,y))
      elif test[1]!=tour:
        if ecs:ech=echec(rt[0],rt[1],tour,(x1,y))
        else:ech=1
        if ech:psb.append((x1,y,test[0]));break
      else:break
      x1+=1
    while x2>-1:
      test=chercher(x2,y)
      if not test:
        if ecs:
          if not tour:tourn(x2,y)
          else:tourb(x2,y)
          ech=echec(rt[0],rt[1],tour,(x2,y));depiece(x2,y)
        else:ech=1
        if ech:psb.append((x2,y))
      elif test[1]!=tour:
        if ecs:ech=echec(rt[0],rt[1],tour,(x2,y))
        else:ech=1
        if ech:psb.append((x2,y,test[0]));break
      else:break
      x2-=1
    while y1<8:
      test=chercher(x,y1)
      if not test:
        if ecs:
          if not tour:tourn(x,y1)
          else:tourb(x,y1)
          ech=echec(rt[0],rt[1],tour,(x,y1));depiece(x,y1)
        else:ech=1
        if ech:psb.append((x,y1))
      elif test[1]!=tour:
        if ecs:ech=echec(rt[0],rt[1],tour,(x,y1))
        else:ech=1
        if ech:psb.append((x,y1,test[0]));break
      else:break
      y1+=1
    while y2>-1:
      test=chercher(x,y2)
      if not test:
        if ecs:
          if not tour:tourn(x,y2)
          else:tourb(x,y2)
          ech=echec(rt[0],rt[1],tour,(x,y2));depiece(x,y2)
        else:ech=1
        if ech:psb.append((x,y2))
      elif test[1]!=tour:
        if ecs:ech=echec(rt[0],rt[1],tour,(x,y2))
        else:ech=1
        if ech:psb.append((x,y2,test[0]));break
      else:break
      y2-=1
  if typ=="c":
    for i in ((2,1),(2,-1),(-2,1),(-2,-1),(1,2),(-1,2),(1,-2),(-1,-2)):
      x0=x+i[0];y0=y+i[1];test=chercher(x0,y0)
      if inplt(x0) and inplt(y0):
        if not test:
          if ecs:
            if not tour:cavaliern(x0,y0)
            else:cavalierb(x0,y0)
            ech=echec(rt[0],rt[1],tour,(x0,y0));depiece(x0,y0)
          else:ech=1
          if ech:psb.append((x0,y0))
        elif test[1]!=tour:
          if ecs:ech=echec(rt[0],rt[1],tour,(x0,y0))
          else:ech=1
          if ech:psb.append((x0,y0,test[0]))
  if typ=="f":
    for i in range(1,8):
      if inplt(x+i) and inplt(y+i):
        test=chercher(x+i,y+i)
        if not test:
          if ecs:
            if not tour:foun(x+i,y+i)
            else:foub(x+i,y+i)
            ech=echec(rt[0],rt[1],tour,(x+i,y+i));depiece(x+i,y+i)
          else:ech=1
          if ech:psb.append((x+i,y+i))
        elif test[1]!=tour:
          if ecs:ech=echec(rt[0],rt[1],tour,(x+i,y+i))
          else:ech=1
          if ech:psb.append((x+i,y+i,test[0]));break
        else:break
      else:break
    for i in range(1,8):
      if inplt(x-i) and inplt(y-i):
        test=chercher(x-i,y-i)
        if not test:
          if ecs:
            if not tour:foun(x-i,y-i)
            else:foub(x-i,y-i)
            ech=echec(rt[0],rt[1],tour,(x-i,y-i));depiece(x-i,y-i)
          else:ech=1
          if ech:psb.append((x-i,y-i))
        elif test[1]!=tour:
          if ecs:ech=echec(rt[0],rt[1],tour,(x-i,y-i))
          else:ech=1
          if ech:psb.append((x-i,y-i,test[0]));break
        else:break
      else:break
    for i in range(1,8):
      if inplt(x+i) and inplt(y-i):
        test=chercher(x+i,y-i)
        if not test:
          if ecs:
            if not tour:foun(x+i,y-i)
            else:foub(x+i,y-i)
            ech=echec(rt[0],rt[1],tour,(x+i,y-i));depiece(x+i,y-i)
          else:ech=1
          if ech:psb.append((x+i,y-i))
        elif test[1]!=tour:
          if ecs:ech=echec(rt[0],rt[1],tour,(x+i,y-i))
          else:ech=1
          if ech:psb.append((x+i,y-i,test[0]));break
        else:break
      else:break
    for i in range(1,8):
      if inplt(x-i) and inplt(y+i):
        test=chercher(x-i,y+i)
        if not test:
          if ecs:
            if not tour:foun(x-i,y+i)
            else:foub(x-i,y+i)
            ech=echec(rt[0],rt[1],tour,(x-i,y+i));depiece(x-i,y+i)
          else:ech=1
          if ech:psb.append((x-i,y+i))
        elif test[1]!=tour:
          if ecs:ech=echec(rt[0],rt[1],tour,(x-i,y+i))
          else:ech=1
          if ech:psb.append((x-i,y+i,test[0]));break
        else:break
      else:break
  if typ=="r":psb=chpsb(x,y,"t",tour)+chpsb(x,y,"f",tour)
  if typ=="k":
    for i in ((1,0),(-1,0),(-1,1),(-1,-1),(1,1),(1,-1)):
      x0=x+i[0];y0=y+i[1]
      if inplt(x0) and inplt(y0):
        test=chercher(x0,y0)
        if ecs:ech=echec(x0,y0,tour)
        else:ech=1
        if not test:
          if ech:psb.append((x0,y0))
        elif test[1]!=tour and ech:psb.append((x0,y0,test[0]))
    y0=y+1
    if inplt(x) and inplt(y0):
      test=chercher(x,y0)
      if ecs:ech=echec(x,y0,tour)
      else:ech=1
      if not test:
        if ech:
          psb.append((x,y0))
          if not tour:
            if ecs and echec(x,y,tour) and rnb and tdn and chercher(7,7)==("t",0) and not chercher(7,5) and echec(x,y0+1,tour):psb.append((x,y0+1,"roque"))
          else:
            if ecs and echec(x,y,tour) and rbb and tdb and chercher(0,7)==("t",-1) and not chercher(0,5) and echec(x,y0+1,tour):psb.append((x,y0+1,"roque"))
      elif test[1]!=tour and ech:psb.append((x,y0,test[0]))
    y0=y-1
    if inplt(x) and inplt(y0):
      test=chercher(x,y0)
      if ecs:ech=echec(x,y0,tour)
      else:ech=1
      if not test:
        if ech:
          psb.append((x,y0))
          if not tour:
            if ecs and echec(x,y,tour) and rnb and tgn and chercher(7,0)==("t",0) and not chercher(7,2) and echec(x,y0-1,tour) and not chercher(7,1):psb.append((x,y0-1,"roque"))
          else:
            if ecs and echec(x,y,tour) and rbb and tgb and chercher(0,0)==("t",-1) and not chercher(0,2) and echec(x,y0-1,tour) and not chercher(0,1):psb.append((x,y0-1,"roque"))
      elif test[1]!=tour and ech:psb.append((x,y0,test[0]))
  return psb
pieces_noires=[(7,0,"k")]
pieces_blanches=[(0,0,"k"),(0,1,"f"),(0,2,"f")]
pieces_mangees_noires=[]
pieces_mangees_blanches=[]
autres_parametres=[0, 0, 0, 0, (0, 4), (7, 4), 1, 1, 1, 1, 1, 1, [-5, -5, (0, 0, 0)], [-5, -6, 'nopiece', (0, 0, 0)], -1, -10, 0]