def Rook(piece, board): x0 = piece.x y0 = piece.y move = [] direction = [(1, 0), (0, -1), (-1, 0), (0, 1)] for i in direction: step = 0 while True: step += 1 if board.inside(x0 + step * i[0], y0 + step * i[1]): if board.pieces[x0 + step * i[0]][y0 + step * i[1]] == 0: move.append( gamePlay.Move('R', x0, y0, x0 + step * i[0], y0 + step * i[1], False)) elif board.pieces[x0 + step * i[0]][y0 + step * i[1]].side == piece.side: break else: move.append( gamePlay.Move('R', x0, y0, x0 + step * i[0], y0 + step * i[1], True)) break else: break return move
def allCastleMove(self, side): move = [] if side == 'w': if self.whiteKing.kingSideCastle and (self.pieces[5][0] == 0) and (self.pieces[6][0] == 0): subBoard = self.copy() subBoard.pieces[5][0] = Piece(5, 0, 'w', 'K') subBoard.pieces[6][0] = Piece(6, 0, 'w', 'K') moves = subBoard.allPossibleNonCastleMove('b') if (not subBoard.isControlled(4, 0, moves)) and ( not subBoard.isControlled(5, 0, moves)) and ( not subBoard.isControlled(6, 0, moves)): move.append(gamePlay.Move('K', 4, 0, 6, 0, False)) if self.whiteKing.queenSideCastle and ( self.pieces[3][0] == 0) and (self.pieces[2][0] == 0) and (self.pieces[1][0] == 0): subBoard = self.copy() subBoard.pieces[3][0] = Piece(3, 0, 'w', 'K') subBoard.pieces[2][0] = Piece(2, 0, 'w', 'K') moves = subBoard.allPossibleNonCastleMove('b') if (not subBoard.isControlled(4, 0, moves)) and ( not subBoard.isControlled(3, 0, moves)) and ( not subBoard.isControlled(2, 0, moves)): move.append(gamePlay.Move('K', 4, 0, 2, 0, False)) elif side == 'b': if self.blackKing.kingSideCastle and (self.pieces[5][7] == 0) and (self.pieces[6][7] == 0): subBoard = self.copy() subBoard.pieces[5][7] = Piece(5, 7, 'b', 'K') subBoard.pieces[6][7] = Piece(6, 7, 'b', 'K') moves = subBoard.allPossibleNonCastleMove('w') if (not subBoard.isControlled(4, 7, moves)) and ( not subBoard.isControlled(5, 7, moves)) and ( not subBoard.isControlled(6, 7, moves)): move.append(gamePlay.Move('K', 4, 7, 6, 7, False)) if self.blackKing.queenSideCastle and ( self.pieces[3][7] == 0) and (self.pieces[2][7] == 0) and (self.pieces[1][7] == 0): subBoard = self.copy() subBoard.pieces[3][7] = Piece(3, 7, 'b', 'K') subBoard.pieces[2][7] = Piece(2, 7, 'b', 'K') moves = subBoard.allPossibleNonCastleMove('w') if (not subBoard.isControlled(4, 7, moves)) and ( not subBoard.isControlled(3, 7, moves)) and ( not subBoard.isControlled(2, 7, moves)): move.append(gamePlay.Move('K', 4, 7, 2, 7, False)) return move
def getPlayerMove(board, side): moveInput = input('Your move is: ') if moveInput.lower() in ['q', 'quit', 'exit', ':q']: return 0 moveInput.replace(' ', '') try: x0 = ord(moveInput[0].lower()) - 97 y0 = int(moveInput[1]) - 1 x1 = ord(moveInput[2].lower()) - 97 y1 = int(moveInput[3]) - 1 except: print('Wrong input format, please type your move again.') return getPlayerMove(board, side) if not (board.inside(x0, y0) and board.inside(x1, y1)): print(f'Your move is out of the board, please try again.') return getPlayerMove(board, side) if board.pieces[x0][y0] == 0: print( f'No piece is found at {chr(x0+97)}{y0+1}, please input another square.' ) return getPlayerMove(board, side) move = gamePlay.Move(board.pieces[x0][y0].name, x0, y0, x1, y1, False) if move.isIn(board.allLegalMove(side)) != False: return move.isIn(board.allLegalMove(side)) else: print( f'{gamePlay.Move(board.pieces[x0][y0].name, x0, y0, x1, y1, False).display()} is not a legal move, please input another move.' ) return getPlayerMove(board, side)
def King(piece, board): x0 = piece.x y0 = piece.y move = [] direction = [(1, 1), (1, -1), (-1, -1), (-1, 1), (1, 0), (0, -1), (-1, 0), (0, 1)] for i in direction: if board.inside(x0 + i[0], y0 + i[1]): if board.pieces[x0 + i[0]][y0 + i[1]] == 0: move.append( gamePlay.Move('K', x0, y0, x0 + i[0], y0 + i[1], False)) elif board.pieces[x0 + i[0]][y0 + i[1]].side != piece.side: move.append( gamePlay.Move('K', x0, y0, x0 + i[0], y0 + i[1], True)) return move
def Knight(piece, board): x0 = piece.x y0 = piece.y move = [] relativeMove = [(1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1), (-2, 1), (-1, 2)] for (x, y) in relativeMove: if board.inside(x0 + x, y0 + y): if board.pieces[x0 + x][y0 + y] == 0: move.append( gamePlay.Move('N', x0, y0, x0 + x, y0 + y, False)) elif board.pieces[x0 + x][y0 + y].side != piece.side: move.append( gamePlay.Move('N', x0, y0, x0 + x, y0 + y, True)) return move
def pawn(piece, board): x0 = piece.x y0 = piece.y move = [] if piece.side == 'w': direction = 1 elif piece.side == 'b': direction = -1 if board.inside(x0, y0 + direction): #Normal move if board.pieces[x0][y0 + direction] == 0: move.append( gamePlay.Move('p', x0, y0, x0, y0 + direction, False)) if y0 == 1 and direction == 1 and board.pieces[x0][ 3] == 0: #Double move move.append(gamePlay.Move('p', x0, y0, x0, 3, False)) elif y0 == 6 and direction == -1 and board.pieces[x0][4] == 0: move.append(gamePlay.Move('p', x0, y0, x0, 4, False)) for i in [-1, 1]: #Normal take if board.inside(x0 + i, y0 + direction): if board.pieces[x0 + i][y0 + direction] != 0 and board.pieces[ x0 + i][y0 + direction].side != piece.side: move.append( gamePlay.Move('p', x0, y0, x0 + i, y0 + direction, True)) if y0 == 4: #En passant take for white if direction == 1 and board.enPassantPawn in [x0 - 1, x0 + 1]: move.append( gamePlay.Move('p', x0, y0, board.enPassantPawn, y0 + direction, True)) elif y0 == 3: #En passant take for black if direction == -1 and board.enPassantPawn in [x0 - 1, x0 + 1]: move.append( gamePlay.Move('p', x0, y0, board.enPassantPawn, y0 + direction, True)) return move