chess_minified.py

Created by aychille

Created on November 10, 2023

10.9 KB

A minified version of my chess game.


from math import*
from kandinsky import fill_rect as rtc,draw_string as string
from ion import keydown as k
from time import*
noir=(0,)*3
blanc=(235,)*3
rou=250,50,10
vert=70,225,30
colcase=255,0,0
caserouge=[]
caseverte=[]
caseposibl=[]
caseprotrouge=[]
caseprotverte=[]
casepossel=[]
x=y=5
s=t=pie=0
objet=0
objetsel=0
def case(x,y,col=colcase):rtc(23+27*x,-25+27*y,28,1,col);rtc(23+27*x,-25+27*y,1,28,col);rtc(50+27*x,-25+27*y,1,28,col);rtc(23+27*x,2+27*y,28,1,col)
def cl(x,y):case(x,y,(120,)*3)
def grid():
  for i in range(9):rtc(50+27*i,2,1,217,(120,)*3);rtc(50,2+27*i,217,1,(120,)*3)
  for i in range(4):
    for j in range(4):rtc(51+54*i,3+54*j,26,26,blanc);rtc(78+54*i,3+54*j,26,26,noir);rtc(78+54*i,30+54*j,26,26,blanc);rtc(51+54*i,30+54*j,26,26,noir)
def pion(x,y,col):rtc(x,y,21,-3,col);rtc(x+2,y-3,17,-2,col);rtc(x+6,y-5,9,-16,col);rtc(x+2,y-11,17,-5,col);rtc(x+4,y-9,13,-10,col)
def king(x,y,col):rtc(x,y,21,-3,col);rtc(x+2,y-3,17,-2,col);rtc(x+4,y-5,13,-5,col);rtc(x+1,y-14,6,6,col);rtc(x+14,y-14,7,6,col);rtc(x+9,y-23,3,15,col);rtc(x+5,y-20,11,3,col)
def tour(x,y,col):rtc(x,y,21,-3,col);rtc(x+2,y-3,17,-2,col);rtc(x+4,y-5,13,-12,col);rtc(x+3,y-17,15,-1,col);rtc(x+1,y-18,19,-2,col);rtc(x+1,y-20,3,-4,col);rtc(x+6,y-20,3,-4,col);rtc(x+12,y-20,3,-4,col);rtc(x+17,y-20,3,-4,col)
def fou(x,y,col):rtc(x,y,21,-3,col);rtc(x+3,y-3,15,-2,col);rtc(x+7,y-5,7,-5,col);rtc(x+3,y-10,15,-1,col);rtc(x+2,y-11,17,-2,col);rtc(x+6,y-13,9,-5,col);rtc(x+7,y-18,7,-2,col);rtc(x+9,y-20,3,-2,col)
def queen(x,y,col):rtc(x,y,21,-2,col);rtc(x+2,y-2,17,-1,col);rtc(x+7,y-3,7,-7,col);rtc(x+5,y-10,11,-1,col);rtc(x+3,y-11,15,-2,col);rtc(x+8,y-13,5,-3,col);rtc(x+6,y-16,9,-4,col);rtc(x+9,y-20,3,-3,col)
def knight(x,y,col):rtc(x,y,21,-3,col);rtc(x+2,y-3,17,-2,col);rtc(x+4,y-5,17,-2,col);rtc(x+6,y-7,16,-2,col);rtc(x+8,y-9,15,-4,col);rtc(x+2,y-13,19,-3,col);rtc(x,y-16,19,-3,col);rtc(x+2,y-19,14,-2,col);rtc(x+4,y-21,9,-2,col)
class Jeu:
  def __init__(self,x,y,color,type):self.x=x;self.y=y;self.color=color;self.type=type;self.pos=10*x+y
  def draw(self):self.type(26+self.x*27,1+27*self.y,self.color)
  def ocuppe():
    global caserouge,caseverte,caseposibl;caserouge=[];caseverte=[]
    for i in L:
      if i.color==rou:caserouge.append(i.pos)
      elif i.color==vert:caseverte.append(i.pos)
  def posibil(self):
    global caseprotrouge,caseprotverte,caseposibl;caseposibl=[];Jeu.ocuppe()
    if self.type==knight:
      if self.color==rou:
        for i in[-1,1]:
          for j in[-2,2]:
            posibl1=self.pos+10*i+j;posibl2=self.pos+10*j+i
            if caserouge.count(posibl1)==0 and 11<=posibl1<=88 and 1<=posibl1%10<=8:caseposibl.append(posibl1)
            if caserouge.count(posibl2)==0 and 11<=posibl2<=88 and 1<=posibl2%10<=8:caseposibl.append(posibl2)
      elif self.color==vert:
        for i in[-1,1]:
          for j in[-2,2]:
            posibl1=self.pos+10*i+j;posibl2=self.pos+10*j+i
            if caseverte.count(posibl1)==0 and 11<=posibl1<=88 and 1<=posibl1%10<=8:caseposibl.append(posibl1)
            if caseverte.count(posibl2)==0 and 11<=posibl2<=88 and 1<=posibl2%10<=8:caseposibl.append(posibl2)
    elif self.type==pion:
      if self.color==rou:
        if caserouge.count(self.pos-1)==caseverte.count(self.pos-1)==0:
          caseposibl.append(self.pos-1)
          if self.y==7 and caserouge.count(self.pos-2)==caseverte.count(self.pos-2)==0:caseposibl.append(self.pos-2)
        if caseverte.count(self.pos-11)==1:caseposibl.append(self.pos-11)
        if caseverte.count(self.pos+9)==1:caseposibl.append(self.pos+9)
      elif self.color==vert:
        if caserouge.count(self.pos+1)==caseverte.count(self.pos+1)==0:
          caseposibl.append(self.pos+1)
          if self.y==2 and caserouge.count(self.pos+2)==caseverte.count(self.pos+2)==0:caseposibl.append(self.pos+2)
        if caserouge.count(self.pos+11)==1:caseposibl.append(self.pos+11)
        if caserouge.count(self.pos-9)==1:caseposibl.append(self.pos-9)
    if self.type==tour or self.type==queen:
      caseposibl.append(self.pos)
      if self.color==rou:
        for i in[-1,1]:
          posx=self.x;posy=self.y
          for j in range(1,8):
            if caseposibl.count(posx*10+self.y)==1 and 1<=posx+i<=8:
              if caserouge.count((posx+i)*10+self.y)==caseverte.count((posx+i)*10+self.y)==0:caseposibl.append((posx+i)*10+self.y);posx+=i
              elif caserouge.count((posx+i)*10+self.y)==1:caseprotrouge.append((posx+i)*10+self.y);break
              elif caseverte.count((posx+i)*10+self.y)==1:caseposibl.append((posx+i)*10+self.y);break
          for j in range(1,8):
            if caseposibl.count(posy+10*self.x)==1 and 1<=posy+i<=8:
              if caserouge.count(posy+i+10*self.x)==caseverte.count(posy+i+10*self.x)==0:caseposibl.append(posy+i+10*self.x);posy+=i
              elif caserouge.count(posy+i+10*self.x)==1:caseprotrouge.append(posy+i+10*self.x);break
              elif caseverte.count(posy+i+10*self.x)==1:caseposibl.append(posy+i+10*self.x);break
      if self.color==vert:
        for i in[-1,1]:
          posx=self.x;posy=self.y
          for j in range(1,8):
            if caseposibl.count(posx*10+self.y)==1 and 11<=(posx+i)*10<=88:
              if caserouge.count((posx+i)*10+self.y)==caseverte.count((posx+i)*10+self.y)==0:caseposibl.append((posx+i)*10+self.y);posx+=i
              elif caseverte.count((posx+i)*10+self.y)==1:caseprotverte.append((posx+i)*10+self.y);break
              elif caserouge.count((posx+i)*10+self.y)==1:caseposibl.append((posx+i)*10+self.y);break
          for j in range(1,8):
            if caseposibl.count(posy+10*self.x)==1 and 1<=posy+i<=8:
              if caserouge.count(posy+i+10*self.x)==caseverte.count(posy+i+10*self.x)==0:caseposibl.append(posy+i+10*self.x);posy+=i
              elif caseverte.count(posy+i+10*self.x)==1:caseprotverte.append(posy+i+10*self.x);break
              elif caserouge.count(posy+i+10*self.x)==1:caseposibl.append(posy+i+10*self.x);break
      caseposibl.remove(self.pos)
    if self.type==fou or self.type==queen:
      caseposibl.append(self.pos)
      if self.color==rou:
        for i in[-1,1]:
          posx=self.x*10;posy=self.y
          for j in range(1,8):
            if caseposibl.count(posx+self.y)==1 and 11<=posx+11*i+self.y<=88 and 1<=(posx+11*i+self.y)%10<=8:
              if caserouge.count(posx+11*i+self.y)==caseverte.count(posx+11*i+self.y)==0:caseposibl.append(posx+11*i+self.y);posx+=11*i
              elif caserouge.count(posx+11*i+self.y)==1:caseprotrouge.append(posx+i*11+self.y);break
              elif caseverte.count(posx+11*i+self.y)==1:caseposibl.append(posx+11*i+self.y);break
          for j in range(1,8):
            if caseposibl.count(posy+10*self.x)==1 and 11<=10*self.x+posy+9*i<=88 and 1<=(posy+9*i+self.x*10)%10<=8:
              if caserouge.count(posy+9*i+10*self.x)==0 and caseverte.count(posy+9*i+10*self.x)==0:caseposibl.append(posy+9*i+10*self.x);posy+=9*i
              elif caserouge.count(posy+9*i+10*self.x)==1:caseprotrouge.append(posy+i*9+self.x);break
              elif caseverte.count(posy+9*i+10*self.x)==1:caseposibl.append(posy+9*i+10*self.x);break
      if self.color==vert:
        for i in[-1,1]:
          posx=self.x*10;posy=self.y
          for j in range(1,8):
            if caseposibl.count(posx+self.y)==1 and 11<=posx+11*i+self.y<=88 and 1<=(posx+11*i+self.y)%10<=8:
              if caserouge.count(posx+11*i+self.y)==0 and caseverte.count(posx+11*i+self.y)==0:caseposibl.append(posx+11*i+self.y);posx+=11*i
              elif caseverte.count(posx+11*i+self.y)==1:caseprotverte.append(posx+i*11+self.y);break
              elif caserouge.count(posx+11*i+self.y)==1:caseposibl.append(posx+11*i+self.y);break
          for j in range(1,8):
            if caseposibl.count(posy+10*self.x)==1 and 11<=10*self.x+posy+9*i<=88 and 1<=(posy+9*i+self.x*10)%10<=8:
              if caserouge.count(posy+9*i+10*self.x)==0 and caseverte.count(posy+9*i+10*self.x)==0:caseposibl.append(posy+9*i+10*self.x);posy+=9*i
              elif caseverte.count(posy+9*i+10*self.x)==1:caseprotverte.append(posy+i*9+self.x);break
              elif caserouge.count(posy+9*i+10*self.x)==1:caseposibl.append(posy+9*i+10*self.x);break
      caseposibl.remove(self.pos)
    elif self.type==king:
      Jeu.caseprot()
      if self.color==rou:
        for i in[-1,0,1]:
          for j in[-10,0,10]:
            if caseprotverte.count(self.pos+i+j)==0 and caserouge.count(self.pos+i+j)==0 and 1<=(self.pos+i+j)%10<=8:caseposibl.append(self.pos+i+j)
      if self.color==vert:
        for i in[-1,0,1]:
          for j in[-10,0,10]:
            if caseprotrouge.count(self.pos+i+j)==0 and caseverte.count(self.pos+i+j)==0 and 1<=(self.pos+i+j)%10<=8:caseposibl.append(self.pos+i+j)
  def caseprot():
    global caseprotrouge,caseprotverte,caseposibl;caseprotrouge=[];caseprotverte=[]
    for i in[-1,0,1]:
      for j in[-10,0,10]:caseprotrouge.append(k1.pos+i+j);caseprotverte.append(k2.pos+i+j)
    for i in p:
      if i.color==rou:caseprotrouge.append(i.pos+9);caseprotrouge.append(i.pos-11)
      if i.color==vert:caseprotverte.append(i.pos-9);caseprotverte.append(i.pos+11)
    n=len(L)-len(p)-2
    for i in range(n):
      L[i+2].posibil()
      if L[i+2].color==rou:caseprotrouge+=caseposibl
      if L[i+2].color==vert:caseprotverte+=caseposibl
      caseposibl=[]
  def draw_pieces():
    for i in L:i.draw()
  def draw_posibl(self,list):
    self.posibil()
    for i in list:rtc(34+27*(i//10),-16+27*(i%10),9,9,(0,0,255))
  def pieces(pos):
    global pie,objet
    for i in L:
      if i.pos==pos:objet=i;pie=1;break
      else:pie=0
k1=Jeu(4,8,rou,king)
k2=Jeu(4,1,vert,king)
q1=Jeu(5,8,rou,queen)
q2=Jeu(5,1,vert,queen)
t1=Jeu(1,8,rou,tour)
t2=Jeu(8,8,rou,tour)
t11=Jeu(1,1,vert,tour)
t12=Jeu(8,1,vert,tour)
f1=Jeu(3,8,rou,fou)
f2=Jeu(6,8,rou,fou)
f11=Jeu(3,1,vert,fou)
f12=Jeu(6,1,vert,fou)
c1=Jeu(2,8,rou,knight)
c2=Jeu(7,8,rou,knight)
c11=Jeu(2,1,vert,knight)
c12=Jeu(7,1,vert,knight)
p=[n for n in range(16)]
for i in range(8):p[i]=Jeu(i+1,7,rou,pion);p[i+8]=Jeu(i+1,2,vert,pion)
L=[k1,k2,q1,q2,t1,t2,t11,t12,f1,f2,f11,f12,c1,c2,c11,c12]+p
grid()
Jeu.draw_pieces()
case(x,y)
while True:
  if L.count(k1)==0:rtc(0,0,320,222,blanc);string(80,100,'Victroire des Vert !')
  if L.count(k2)==0:rtc(0,0,320,222,blanc);string(80,100,'Victroire des Rouges !')
  if k(0):
    if x>=2:cl(x,y);x-=1;case(x,y);sleep(.1)
  if k(3):
    if x<=7:cl(x,y);x+=1;case(x,y);sleep(.1)
  if k(1):
    if y>=2:cl(x,y);y-=1;case(x,y);sleep(.1)
  if k(2):
    if y<=7:cl(x,y);y+=1;case(x,y);sleep(.1)
  if k(4):
    Jeu.ocuppe();Jeu.pieces(10*x+y)
    if s==1:
      if casepossel.count(10*x+y)==1:
        if pie==1:L.remove(objet)
        objetsel.x=x;objetsel.y=y;objetsel.pos=10*x+y;objetsel=None;objet=None;casepossel=[];t+=1
      else:objetsel=None;casepossel=[]
      s=0;grid();Jeu.draw_pieces();case(x,y)
    Jeu.pieces(10*x+y)
    if s==0:
      if pie==1 and(t%2==0 and objet.color==rou)+(t%2==1 and objet.color==vert):objetsel=objet;objetsel.posibil();casepossel=caseposibl;objetsel.draw_posibl(casepossel);s=1
      else:0
    sleep(.15)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.