chess4.py

Created by schraf

Created on September 04, 2022

9.39 KB


_R='queen'
_Q='king_w'
_P='bishop_w'
_O='knight_w'
_N='rook_w'
_M='king_b'
_L='bishop_b'
_K='knight_b'
_J='rook_b'
_I='king'
_H=None
_G='all'
_F='black'
_E='pawn_w'
_D='pawn_b'
_C='_'
_B=True
_A=False
from kandinsky import *
from ion import *
pawn=3584,4352,12672,65504,32800,32800,16448,8320,8320,8320,8320,8320,8320,8320,16448,98352,131080,524284,524290,1048577,2097151
knight=480,1840,3096,6220,12390,8194,24577,16497,49371,33166,33152,32960,32864,98352,65560,196616,131080,524284,524290,1048577,2097151
rook=101936,168232,160200,131080,131080,131056,32800,32800,32800,32800,32800,32800,32800,32800,32800,131056,131080,524284,524290,1048577,2097151
bishop=3584,4352,4352,4352,3584,4352,12672,24768,49248,32928,33056,33312,32800,49248,16448,49248,98352,524284,524290,1048577,2097151
queen=1024,2560,4352,130032,137992,263172,524290,524290,524290,524290,524290,524290,262148,262148,131080,131056,65552,524284,524290,1048577,2097151
king=917518,593426,561442,545090,544322,536962,524290,524290,262148,262148,131080,65552,65504,32800,16448,131056,65552,524284,524290,1048577,2097151
pos,initial_pos=[0,3],[]
lky,pion_selected,mvpi=_H,_H,_H
tour=0
roque_done=_A
popi=[[_J,_K,_L,_M,'queen_b',_L,_K,_J],[_D,_D,_D,_D,_D,_D,_D,_D],['','','','','','','',''],['','','','','','','',''],['','','','','','','',''],['','','','','','','',''],[_E,_E,_E,_E,_E,_E,_E,_E],[_N,_O,_P,'queen_w',_Q,_P,_O,_N]]
t_b,t_w=4800,4800
k_w_m,k_b_m,t1_w_m,t1_b_m,t2_w_m,t2_b_m=_A,_A,_A,_A,_A,_A
eat_pion=_A
p1,p2=(4,7),(0,7)
def gpoofpi(pion):
 pofpi=[]
 for i in range(8):
  for j in range(8):
   if pion!=_G:
    if popi[i][j]==pion:pofpi.append((j,i))
   elif popi[i][j]!='':pofpi.append((j,i))
 return pofpi
def gpiopo(p):return popi[p[1]][p[0]]
def swpopi(sender_pos,target_pos,eat=0,rock=0):
 global tour,eat_pion
 if not rock:
  if not eat:popi[sender_pos[1]][sender_pos[0]],popi[target_pos[1]][target_pos[0]]=popi[target_pos[1]][target_pos[0]],popi[sender_pos[1]][sender_pos[0]]
  else:popi[target_pos[1]][target_pos[0]]=popi[sender_pos[1]][sender_pos[0]];popi[sender_pos[1]][sender_pos[0]]='';eat_pion=_A
 tour+=1;menu()
def default_pions():
 for i in range(8):
  for j in range(8):
   if popi[i][j]!='':draw_pion(eval(popi[i][j].split(_C)[0]),d_p(j,i,3)[0],d_p(j,i,3)[1],get_font([j,i]),get_pion_color([j,i]))
def draw_pion(t,x,y,back=(0,)*3,font=(170,170,170)):
 for i in range(21):
  for j in range(21):fill_rect(x+j,y+i,1,1,t[i]>>20-j&1 and font or back)
def debug(i_pos,target_pos):draw_string('i_p:'+str(i_pos),221,90,_F);draw_string('t_p:'+str(target_pos),221,110,_F)
def gui():B='XXXsec';A='white';fill_rect(0,0,320,222,(49,46,43));plateau();draw_string('CHESS',244,3,A,(49,46,43));draw_string('Blacks:16',225,40,_F,(49,46,43));draw_string(B,225,64,_F,(49,46,43));draw_string('Round:',225,100,A,(49,46,43));fill_rect(290,100,20,20,(170,170,170)if tour%2==0 else _F);draw_string(B,225,140,(170,170,170),(49,46,43));draw_string('Whites:16',225,164,(170,170,170),(49,46,43));draw_string('nsi.xyz',235,201,A,(49,46,43));default_pions()
def menu():fill_rect(290,100,20,20,(170,170,170)if tour%2==0 else _F)
def plateau():
 for i in range(8):
  for j in range(8):fill_rect(3+j*27,3+i*27,27,27,get_font([j,i]))
def d_p(p_x=0,p_y=0,pion=0):return[3+27*p_x+pion,3+27*p_y+pion]
def get_font(p):return((119,149,86)if p[0]%2==0 else(235,236,208))if p[1]%2!=0 else(235,236,208)if p[0]%2==0 else(119,149,86)
def get_pion_color(p):return(170,170,170)if gpiopo(p).split(_C)[1]=='w'else(0,)*3
def roque(sender_pos,target_pos):
 pion=gpiopo(sender_pos).split(_C)
 if pion[0]!=_I:return _A
 diff=abs(sender_pos[0]-target_pos[0])
 if pion[1]=='w':
  if 2<=diff<=4 and not k_w_m and(target_pos==(7,7)and not t2_w_m or target_pos==(0,7)and not t1_w_m)and nopibta(sender_pos,target_pos,1):
   if target_pos==(0,7):popi[7][2],popi[7][3]=popi[sender_pos[1]][sender_pos[0]],popi[7][0];popi[7][0],popi[sender_pos[1]][sender_pos[0]]='','';return _B
  return _A
def legit_move(sender_pos,target_pos):
 pion=gpiopo(sender_pos).split(_C)
 if pion[0]=='pawn':
  if pion[1]=='b':
   dist=2 if sender_pos[1]==1 else 1
   if 0<target_pos[1]-sender_pos[1]<=dist and sender_pos[0]==target_pos[0]:return _B
   return _A
  else:
   dist=-2 if sender_pos[1]==6 else-1
   if dist<=target_pos[1]-sender_pos[1]<0 and sender_pos[0]==target_pos[0]:return _B
   return _A
 if pion[0]=='rook':
  if sender_pos[0]==target_pos[0]or sender_pos[1]==target_pos[1]:return _B
  return _A
 if pion[0]=='knight':
  diff_x,diff_y=abs(sender_pos[0]-target_pos[0]),abs(sender_pos[1]-target_pos[1])
  if diff_x==1 and diff_y==2 or diff_x==2 and diff_y==1:return _B
  return _A
 if pion[0]=='bishop':
  if abs(target_pos[0]-sender_pos[0])==abs(target_pos[1]-sender_pos[1]):return _B
  return _A
 if pion[0]==_R:
  if abs(target_pos[0]-sender_pos[0])==abs(target_pos[1]-sender_pos[1])or(sender_pos[0]==target_pos[0]or sender_pos[1]==target_pos[1]):return _B
  return _A
 if pion[0]==_I:
  if abs(sender_pos[0]-target_pos[0])<2 and abs(sender_pos[1]-target_pos[1])<2:return _B
  return _A
def nopibta(sender_pos,target_pos,rock=0):
 global eat_pion;all_pos_between_target=[];pion=gpiopo(sender_pos).split(_C)
 if gpiopo(target_pos)!=''and pion[1]==gpiopo(target_pos).split(_C)[1]and not rock:return _A
 for i in range(target_pos[1],sender_pos[1])if target_pos[1]+1-sender_pos[1]<0 else range(sender_pos[1]+-1*(target_pos[1]+1-sender_pos[1]==0),target_pos[1]+1):
  for j in range(target_pos[0],sender_pos[0])if target_pos[0]+1-sender_pos[0]<0 else range(sender_pos[0]+-1*(target_pos[0]+1-sender_pos[0]==0),target_pos[0]+1):
   if not rock:
    if legit_move(sender_pos,[j,i])and list(sender_pos)!=[j,i]and list(target_pos)!=[j,i]:
     if pion[0]==_R or pion[0]==_I:
      if abs(target_pos[0]-sender_pos[0])==abs(target_pos[1]-sender_pos[1]):
       if abs(j-sender_pos[0])==abs(i-sender_pos[1]):all_pos_between_target.append([j,i])
      if sender_pos[0]==target_pos[0]or sender_pos[1]==target_pos[1]:all_pos_between_target.append([j,i])
     else:all_pos_between_target.append([j,i])
   elif list(sender_pos)!=[j,i]and list(target_pos)!=[j,i]:all_pos_between_target.append([j,i])
 print(all_pos_between_target)
 for i in range(len(all_pos_between_target)):
  if gpiopo(all_pos_between_target[i])!='':return _A
 if gpiopo(target_pos)!=''and pion[1]!=gpiopo(target_pos).split(_C)[1]:eat_pion=1
 return _B
def king_is_vulnerable():
 all_moves=[]
 for k in range(len(gpoofpi(_G))):
  print(k)
  for i in range(8):
   for j in range(8):
    if legit_move(gpoofpi(_G)[k],[j,i])and nopibta(gpoofpi(_G)[k],[j,i])and list(gpoofpi(_G)[k])!=[j,i]:all_moves.append([j,i])
 king_w_pos=list(gpoofpi(_Q)[0]);king_b_pos=list(gpoofpi(_M)[0]);print(all_moves,king_w_pos,king_b_pos)
 if king_w_pos in all_moves or king_b_pos in all_moves:return _B
 return _A
def all_conditions_to_move(i_p,t_p):
 global k_w_m,k_b_m,t1_w_m,t1_b_m,t2_w_m,t2_b_m,roque_done
 if nopibta(i_p,t_p)and i_p!=t_p and legit_move(i_p,t_p):
  if i_p==[0,0]:t1_b_m=_B
  if i_p==[3,0]:k_b_m=_B
  if i_p==[7,0]:t2_b_m=_B
  if i_p==[0,7]:t1_w_m=_B
  if i_p==[4,7]:k_w_m=_B
  if i_p==[7,7]:t2_w_m=_B
  return _B
 if roque(tuple(i_p),tuple(t_p)):roque_done=_B;return _B
 print(roque(tuple(i_p),tuple(t_p)));print(i_p,t_p);return _A
def get_old_pos(key):return[pos[0]+(-1 if key==3 else+1 if key!=1 and key!=2 else 0),pos[1]+(-1 if key==2 else+1 if key!=0 and key!=3 else 0)]
def update_pos_pion(old_p,p,rock=0):
 if not rock:fill_rect(d_p(old_p[0])[0],d_p(0,old_p[1])[1],27,27,get_font(old_p));fill_rect(d_p(p[0])[0],d_p(0,p[1])[1],27,27,(186,202,43));fill_rect(d_p(p[0])[0]+3,d_p(0,p[1])[1]+3,21,21,get_font(p));draw_pion(eval(gpiopo(p).split(_C)[0]),d_p(p[0],0,3)[0],d_p(0,p[1],3)[1],get_font(p),get_pion_color(p))
 else:default_pions()
def mvcu(key,select_mode=0,leave_select_mode=0):
 global pion_selected,initial_pos,roque_done
 if leave_select_mode:
  if all_conditions_to_move(initial_pos,pos):print('1',popi);swpopi(initial_pos,pos,1*eat_pion,1*roque_done);print('2',popi);update_pos_pion(initial_pos,pos,1*roque_done);roque_done=0;print('3',popi)
 else:
  if select_mode:fill_rect(d_p(pos[0])[0],d_p(0,pos[1])[1],27,27,(206,222,63));draw_pion(eval(gpiopo(pos).split(_C)[0]),d_p(pos[0],0,3)[0],d_p(0,pos[1],3)[1],(206,222,63),get_pion_color(pos));initial_pos=[pos[0],pos[1]];pion_selected=_B
  if not select_mode and not pion_selected:
   old_pos=get_old_pos(key);fill_rect(d_p(old_pos[0])[0],d_p(0,old_pos[1])[1],27,27,get_font(old_pos))
   if gpiopo(old_pos)!='':draw_pion(eval(gpiopo(old_pos).split(_C)[0]),d_p(old_pos[0],0,3)[0],d_p(0,old_pos[1],3)[1],get_font(old_pos),get_pion_color(old_pos))
  if not select_mode:
   fill_rect(d_p(pos[0])[0],d_p(0,pos[1])[1],27,27,(186,202,43));fill_rect(d_p(pos[0])[0]+3,d_p(0,pos[1])[1]+3,21,21,get_font(pos))
   if gpiopo(pos)!='':draw_pion(eval(gpiopo(pos).split(_C)[0]),d_p(pos[0],0,3)[0],d_p(0,pos[1],3)[1],get_font(pos),get_pion_color(pos))
   debug(initial_pos,pos);pion_selected=_A
def legit_turn(t):
 team_pion=gpiopo(pos).split(_C)[1]
 if team_pion=='w'and t%2==0 or team_pion=='b'and t%2!=0:return _B
 return _A
def chess():
 gui();global pos,lky,mvpi
 while not keydown(5):
  if keydown(3)and pos[0]<7 and lky!=3:pos[0]+=1;mvcu(3);lky=3
  if keydown(0)and pos[0]>0 and lky!=0:pos[0]-=1;mvcu(0);lky=0
  if keydown(1)and pos[1]>0 and lky!=1:pos[1]-=1;mvcu(1);lky=1
  if keydown(2)and pos[1]<7 and lky!=2:pos[1]+=1;mvcu(2);lky=2
  if keydown(52)and lky!=52:
   if not mvpi and gpiopo(pos)!=''and legit_turn(tour):mvcu(42,1);mvpi=_B
   elif mvpi:mvcu(42,0,1);mvpi=_A
   lky=52
  if not(keydown(0)or keydown(1)or keydown(2)or keydown(3)or keydown(52)):lky=_H
chess()

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.