Advertisement
RayanRam

Chess Python

Dec 30th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.78 KB | None | 0 0
  1. class board:
  2.     def __init__(self):
  3.         self.board=[['O' for x in range(8)] for y in range(8)]
  4.         self.game="on"
  5.         self.turn="white"
  6.         self.pieces=[]
  7.         for i in range(8):
  8.             self.add(pawn(6,i,"white"))
  9.             self.add(pawn(1,i,"black"))
  10.  
  11.         self.add(knight(7,1,"white"))
  12.         self.add(knight(7,6,"white"))
  13.         self.add(knight(0,1,"black"))
  14.         self.add(knight(0,6,"black"))
  15.  
  16.         self.add(rook(7,0,"white"))
  17.         self.add(rook(7,7,"white"))
  18.         self.add(rook(0,0,"black"))
  19.         self.add(rook(0,7,"black"))
  20.  
  21.         self.add(bishop(7,2,"white"))
  22.         self.add(bishop(7,5,"white"))
  23.         self.add(bishop(0,2,"black"))
  24.         self.add(bishop(0,5,"black"))
  25.  
  26.         self.add(king(7,3,"white"))
  27.         self.add(queen(7,4,"white"))
  28.         self.add(king(0,4,"black"))
  29.         self.add(queen(0,3,"black"))
  30.         self.draw()
  31.  
  32.     def print(self):
  33.         print('\n\n'.join(['\t'.join(['{:4}'.format(item) for item in row]) for row in self.board]))
  34.  
  35.     def add(self, element):
  36.         self.pieces.append(element)
  37.         self.draw()
  38.  
  39.     def draw(self):
  40.         for i in self.pieces:
  41.             self.board[i.x][i.y]=i.id
  42.  
  43.     def getpiece(self, x, y):
  44.         i=None
  45.         for i in self.pieces:
  46.             if i.x==x and i.y==y:
  47.                 return i
  48.  
  49.     def removepiece(self, x, y):
  50.         d=0
  51.         for i in self.pieces:
  52.             if i.x==x and i.y==y:
  53.                 del self.pieces[d]
  54.                 self.board[x][y]='O'
  55.                 return
  56.             d=d+1
  57.  
  58.     def blocked(self, basex, basey, newx, newy):
  59.         if self.getpiece(basex, basey).name=="knight":
  60.             return False
  61.         else:
  62.             if basex==newx:
  63.                 for i in range(basey,newy):
  64.                     if self.getpiece(basex+1,i)!=None:
  65.                         return True
  66.             elif basey==newy:
  67.                 for i in range(basex,newx):
  68.                     if self.getpiece(i+1,basey)!=None:
  69.                         return True
  70.             else:
  71.                 if newx-basex>0:
  72.                     incx=1
  73.                 else:
  74.                     incx=-1
  75.                 if newy-basey>0:
  76.                     incy=1
  77.                 else:
  78.                     incy=-1
  79.  
  80.                 j=basey+incy
  81.                 i=basex+incx
  82.                 while j!=newy and i!=newx:
  83.                     if self.getpiece(i,j)!=None:
  84.                         return True
  85.                     i=i+incx
  86.                     j=j+incy
  87.  
  88.             return False
  89.  
  90.     def move(self,basex, basey, newx, newy):
  91.         if self.getpiece(basex, basey)==None:
  92.             print("You're trying to move nothing !")
  93.             return False
  94.         if self.getpiece(basex, basey).side!=self.turn:
  95.             print("Move your own pieces !")
  96.             return False
  97.         if self.blocked(basex, basey, newx, newy):
  98.             print("There is another piece on the road ! ")
  99.             return False
  100.         if basex>7 or basex<0 or basey>7 or basex<0:
  101.             print("Out of Board movement !")
  102.             return False
  103.         elif self.board[newx][newy]!='O' and self.getpiece(basex, basey).side==self.getpiece(newx, newy).side:
  104.             print("You can't eat from your own side !")
  105.             return False
  106.         else:
  107.             for i in self.pieces:
  108.                 if i.x==basex and i.y==basey:
  109.                     if i.move(newx, newy):
  110.                         if self.turn=="white":
  111.                             self.turn="black"
  112.                         else:
  113.                             self.turn="white"
  114.                         self.board[basex][basey]='O'
  115.                         self.draw()
  116.  
  117.         print("\n\n")
  118.         self.print()
  119.  
  120.  
  121. class piece:
  122.     def __init__(self, x, y, side):
  123.         self.moved=False
  124.         self.side=side
  125.         self.x=x
  126.         self.y=y
  127.  
  128.     def describe(self):
  129.         print(" I am a "+str(self.side)+" "+str(self.name)+" on X = "+str(self.x)+" and Y = "+str(self.y)+"\n")
  130.  
  131.  
  132. class pawn(piece):
  133.     def __init__(self, x, y, side):
  134.         super(pawn, self).__init__(x, y, side)
  135.         self.name="pawn"
  136.         self.id="W-P" if side=="white" else "B-P"
  137.  
  138.     def move(self, x, y):
  139.         print(" x:"+str(x)+"  self.x:"+str(self.x)+" y:"+str(y)+" self.y:"+str(self.y))
  140.         if not self.moved and self.y==y and ((self.side=="black" and self.x-x>-3 and self.x-x<0) or (self.side=="white" and self.x-x>0 and self.x-x<3 )):
  141.             print("Enabled movement")
  142.             self.x=x
  143.             self.y=y
  144.             self.moved=True
  145.             return True
  146.         elif self.y==y and ((self.side=="black" and self.x-x>-2 and self.x-x<0) or (self.side=="white" and self.x-x>0 and self.x-x<2 )):
  147.             print("Enabled movement")
  148.             self.x=x
  149.             self.y=y
  150.             self.moved=True
  151.             return True
  152.         else:
  153.             print("Disabled movement")
  154.             return False
  155.    
  156. class knight(piece):
  157.     def __init__(self, x, y, side):
  158.         super(knight, self).__init__(x, y, side)
  159.         self.name="knight"
  160.         self.id="W-KN" if side=="white" else "B-KN"
  161.  
  162.     def move(self, x, y):
  163.         if ((self.x-x==2 or self.x-x==-2) and (self.y-y==1 or self.y-y==-1)) or ((self.y-y==2 or self.y-y==-2) and (self.x-x==1 or self.x-x==-1)):
  164.             print("Enabled movement")
  165.             self.x=x
  166.             self.y=y
  167.             self.moved=True
  168.             return True
  169.         else:
  170.             print("Disabled movement")
  171.             return False
  172.  
  173.  
  174. class bishop(piece):
  175.     def __init__(self, x, y, side):
  176.         super(bishop, self).__init__(x, y, side)
  177.         self.name="bishop"
  178.         self.id="W-B" if side=="white" else "B-B"
  179.  
  180.     def move(self, x, y):
  181.         if self.x-x==self.y-y or self.x-x==(-1)*self.y-y:
  182.             print("Enabled movement")
  183.             self.x=x
  184.             self.y=y
  185.             self.moved=True
  186.             return True
  187.         else:
  188.             print("Disabled movement")
  189.             return False
  190.  
  191. class rook(piece):
  192.     def __init__(self, x, y, side):
  193.         super(rook, self).__init__(x, y, side)
  194.         self.name="rook"
  195.         self.id="W-R" if side=="white" else "B-R"
  196.  
  197.     def move(self, x, y):
  198.         if self.y==y or self.x==x:
  199.             print("Enabled movement")
  200.             self.x=x
  201.             self.y=y
  202.             self.moved=True
  203.             return True
  204.         else:
  205.             print("Disabled movement")
  206.             return False
  207.  
  208. class queen(piece):
  209.     def __init__(self, x, y, side):
  210.         super(queen, self).__init__(x, y, side)
  211.         self.name="queen"
  212.         self.id="W-Q" if side=="white" else "B-Q"
  213.  
  214.     def move(self, x, y):
  215.         if (self.y==y or self.x==x) or (self.x-x==self.y-y or self.x-x==(-1)*self.y-y):
  216.             print("Enabled movement")
  217.             self.x=x
  218.             self.y=y
  219.             self.moved=True
  220.             return True
  221.         else:
  222.             print("Disabled movement")
  223.             return False
  224.  
  225. class king(piece):
  226.     def __init__(self, x, y, side):
  227.         super(king, self).__init__(x, y, side)
  228.         self.name="king"
  229.         self.id="W-KI" if side=="white" else "B-KI"
  230.  
  231.     def move(self, x, y):
  232.         if self.x-x<2 and self.x-x>-2 and self.y-y<2 and self.y-y>-2:
  233.             print("Enabled movement")
  234.             self.x=x
  235.             self.y=y
  236.             self.moved=True
  237.             return True
  238.         else:
  239.             print("Disabled movement")
  240.             return False
  241. b=board()
  242. b.print()
  243. print("\nTHE FORMAT TO MOVE : InitialX,InitialY,DestionationX,DestinationY\nExample :1,0,2,0\n")
  244. while True:
  245.     print(b.turn+"'s turn !")
  246.     d=input(":")  
  247.     print(d)
  248.     l=[]
  249.     l=d.split(",")
  250.     k=[]
  251.     for i in l:
  252.         k.append(int(i))
  253.     b.move(k[0],k[1],k[2],k[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement