def test_bishop_valid_moves_two(): board = Board() bishop = Bishop(4, 4, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[3, 3], [2, 2], [1, 1], [0, 0], [3, 5], [2, 6], [1, 7], [5, 5], [6, 6], [7, 7], [5, 3], [6, 2], [7, 1]] assert actual == expected
def test_bishop_valid_moves(): board = Board() bishop = Bishop(1, 3, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[0, 2], [0, 4], [2, 4], [3, 5], [4, 6], [5, 7], [2, 2], [3, 1], [4, 0]] assert actual == expected
def test_bishop_attacking(): board = Board() bishop = Bishop(4, 3, 'w') board.board[4][3] = bishop board.board[5][4] = Pawn(5, 4, 'b') bishop.valid_moves(board) actual = bishop.attack_list expected = [[5, 4]] assert actual == expected
def test_bishop_no_moves_after_attacking(): board = Board() bishop = Bishop(4, 3, 'w') board.board[4][3] = bishop board.board[5][4] = Pawn(5, 4, 'b') bishop.valid_moves(board) actual = bishop.move_list expected = [[3, 2], [2, 1], [1, 0], [3, 4], [2, 5], [1, 6], [0, 7], [5, 2], [6, 1], [7, 0]] assert actual == expected
def test_pins(): board = Board() king = King(7, 4, 'w') board.board[7][4] = king bishop = Bishop(6, 4, 'w') board.board[6][4] = bishop b_rook = Rook(5, 4, 'b') board.board[5][4] = b_rook bishop.valid_moves(board) b_rook.valid_moves(board) bishop.test_if_your_king_is_in_check(board) actual = bishop.move_list expected = [] assert actual == expected
def is_controlled_sq(self, req_pos, turn): ''' Checks whether a square is controlled by a piece. 'turn' means controlled by piece of which colour/turn ''' # Along knight routes (L) knight = Knight(0) knight.set_pos(req_pos) # print(self.get_sq_notation(req_pos)) # print() for move in knight.valid_moves(): # print(self.get_sq_notation(move), end=': ') piece = self.fetch_piece(move) # print(piece) if piece and type(piece).__name__ == 'Knight' and piece.colour == turn: # self.pos_of_piece_causing_check = move # print('yeah') del knight return True # Along diagonals bishop = Bishop(0) bishop.set_pos(req_pos) for move in bishop.valid_moves(): piece = self.fetch_piece(move) if piece and \ (type(piece).__name__ == 'Bishop' or type(piece).__name__ == 'Queen') and \ piece.colour == turn and \ not piece.is_path_obstructed(self, req_pos, move): # print('yes') # self.pos_of_piece_causing_check = move del bishop # print('Obstruct') return True # Along ranks and files rook = Rook(0) rook.set_pos(req_pos) for move in rook.valid_moves(): # print(self.get_sq_notation(move), end=": ") piece = self.fetch_piece(move) # print(piece) if piece and (type(piece).__name__ == 'Rook' or type(piece).__name__ == 'Queen') and \ piece.colour == turn and \ not piece.is_path_obstructed(self, req_pos, move): # self.pos_of_piece_causing_check = move # print('here') del rook return True # King opposition king = King() king.set_pos(req_pos) for move in king.valid_moves(): piece = self.fetch_piece(move) if piece and type(piece).__name__ == 'King' and piece.colour == turn: if not self.is_controlled_sq(move, self.turn): # print('here') # self.pos_of_piece_causing_check = move del king return True # Pawns if turn == 'White': pawn = Pawn(colour='Black') else: pawn = Pawn() pawn.set_pos(req_pos) for move in pawn.valid_moves(self.is_flipped): # If directly in front/back if move[0] - req_pos[0] == 0: continue piece = self.fetch_piece(move) if piece and type(piece).__name__ == 'Pawn' and piece.colour == turn: # self.pos_of_piece_causing_check = move del pawn return True del knight, bishop, rook, king, pawn # print('Nope') return False
def valid_moves(self): valids = [] valids = Bishop.valid_moves(self) valids.extend(Rook.valid_moves(self)) return valids