Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class board:
- def __init__(self):
- self.board=[['O' for x in range(8)] for y in range(8)]
- self.game="on"
- self.turn="white"
- self.pieces=[]
- for i in range(8):
- self.add(pawn(6,i,"white"))
- self.add(pawn(1,i,"black"))
- self.add(knight(7,1,"white"))
- self.add(knight(7,6,"white"))
- self.add(knight(0,1,"black"))
- self.add(knight(0,6,"black"))
- self.add(rook(7,0,"white"))
- self.add(rook(7,7,"white"))
- self.add(rook(0,0,"black"))
- self.add(rook(0,7,"black"))
- self.add(bishop(7,2,"white"))
- self.add(bishop(7,5,"white"))
- self.add(bishop(0,2,"black"))
- self.add(bishop(0,5,"black"))
- self.add(king(7,3,"white"))
- self.add(queen(7,4,"white"))
- self.add(king(0,4,"black"))
- self.add(queen(0,3,"black"))
- self.draw()
- def print(self):
- print('\n\n'.join(['\t'.join(['{:4}'.format(item) for item in row]) for row in self.board]))
- def add(self, element):
- self.pieces.append(element)
- self.draw()
- def draw(self):
- for i in self.pieces:
- self.board[i.x][i.y]=i.id
- def getpiece(self, x, y):
- i=None
- for i in self.pieces:
- if i.x==x and i.y==y:
- return i
- def removepiece(self, x, y):
- d=0
- for i in self.pieces:
- if i.x==x and i.y==y:
- del self.pieces[d]
- self.board[x][y]='O'
- return
- d=d+1
- def blocked(self, basex, basey, newx, newy):
- if self.getpiece(basex, basey).name=="knight":
- return False
- else:
- if basex==newx:
- for i in range(basey,newy):
- if self.getpiece(basex+1,i)!=None:
- return True
- elif basey==newy:
- for i in range(basex,newx):
- if self.getpiece(i+1,basey)!=None:
- return True
- else:
- if newx-basex>0:
- incx=1
- else:
- incx=-1
- if newy-basey>0:
- incy=1
- else:
- incy=-1
- j=basey+incy
- i=basex+incx
- while j!=newy and i!=newx:
- if self.getpiece(i,j)!=None:
- return True
- i=i+incx
- j=j+incy
- return False
- def move(self,basex, basey, newx, newy):
- if self.getpiece(basex, basey)==None:
- print("You're trying to move nothing !")
- return False
- if self.getpiece(basex, basey).side!=self.turn:
- print("Move your own pieces !")
- return False
- if self.blocked(basex, basey, newx, newy):
- print("There is another piece on the road ! ")
- return False
- if basex>7 or basex<0 or basey>7 or basex<0:
- print("Out of Board movement !")
- return False
- elif self.board[newx][newy]!='O' and self.getpiece(basex, basey).side==self.getpiece(newx, newy).side:
- print("You can't eat from your own side !")
- return False
- else:
- for i in self.pieces:
- if i.x==basex and i.y==basey:
- if i.move(newx, newy):
- if self.turn=="white":
- self.turn="black"
- else:
- self.turn="white"
- self.board[basex][basey]='O'
- self.draw()
- print("\n\n")
- self.print()
- class piece:
- def __init__(self, x, y, side):
- self.moved=False
- self.side=side
- self.x=x
- self.y=y
- def describe(self):
- print(" I am a "+str(self.side)+" "+str(self.name)+" on X = "+str(self.x)+" and Y = "+str(self.y)+"\n")
- class pawn(piece):
- def __init__(self, x, y, side):
- super(pawn, self).__init__(x, y, side)
- self.name="pawn"
- self.id="W-P" if side=="white" else "B-P"
- def move(self, x, y):
- print(" x:"+str(x)+" self.x:"+str(self.x)+" y:"+str(y)+" self.y:"+str(self.y))
- 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 )):
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- 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 )):
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- class knight(piece):
- def __init__(self, x, y, side):
- super(knight, self).__init__(x, y, side)
- self.name="knight"
- self.id="W-KN" if side=="white" else "B-KN"
- def move(self, x, y):
- 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)):
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- class bishop(piece):
- def __init__(self, x, y, side):
- super(bishop, self).__init__(x, y, side)
- self.name="bishop"
- self.id="W-B" if side=="white" else "B-B"
- def move(self, x, y):
- if self.x-x==self.y-y or self.x-x==(-1)*self.y-y:
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- class rook(piece):
- def __init__(self, x, y, side):
- super(rook, self).__init__(x, y, side)
- self.name="rook"
- self.id="W-R" if side=="white" else "B-R"
- def move(self, x, y):
- if self.y==y or self.x==x:
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- class queen(piece):
- def __init__(self, x, y, side):
- super(queen, self).__init__(x, y, side)
- self.name="queen"
- self.id="W-Q" if side=="white" else "B-Q"
- def move(self, x, y):
- if (self.y==y or self.x==x) or (self.x-x==self.y-y or self.x-x==(-1)*self.y-y):
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- class king(piece):
- def __init__(self, x, y, side):
- super(king, self).__init__(x, y, side)
- self.name="king"
- self.id="W-KI" if side=="white" else "B-KI"
- def move(self, x, y):
- if self.x-x<2 and self.x-x>-2 and self.y-y<2 and self.y-y>-2:
- print("Enabled movement")
- self.x=x
- self.y=y
- self.moved=True
- return True
- else:
- print("Disabled movement")
- return False
- b=board()
- b.print()
- print("\nTHE FORMAT TO MOVE : InitialX,InitialY,DestionationX,DestinationY\nExample :1,0,2,0\n")
- while True:
- print(b.turn+"'s turn !")
- d=input(":")
- print(d)
- l=[]
- l=d.split(",")
- k=[]
- for i in l:
- k.append(int(i))
- b.move(k[0],k[1],k[2],k[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement