def test_king_cannot_move_to_occupied_cell(self): board = chess.Board() king_white = chess.King('d', 4, chess.WHITE, board) pawn_white = chess.Pawn('d', 3, chess.WHITE, board) true_moves = set([('d',5), ('c',4), ('e',4), ('c',3), ('c',5), ('e',3), ('e',5)]) board.set_piece(king_white) board.set_piece(pawn_white) self.assertEqual(true_moves, king_white.valid_moves())
def test_knight_cannot_move_to_occupied_cell(self): board = chess.Board() knight_white = chess.Knight('d', 4, chess.WHITE, board) pawn_white = chess.Pawn('c', 6, chess.WHITE, board) true_moves = set([('e', 6), ('f', 5), ('f', 3), ('e', 2), ('c', 2), ('b', 3), ('b', 5)]) board.set_piece(knight_white) board.set_piece(pawn_white) self.assertEqual(true_moves, knight_white.valid_moves())
def init(self, num_boards=None): ''' Initialize game. Can initialize with more game boards for more players! ''' if num_boards is not None: self.num_boards = num_boards self.player_last_move = {} self.board = {} self.board_size = [8 * self.num_boards, 8] self.num_players = self.num_boards * 2 for who, (x, y0, y1) in enumerate([(0, 0, 1), (0, 7, 6), (8, 0, 1), (8, 7, 6)][:self.num_players]): for dx, piece in enumerate(chess.first_row): p = piece(who, (x + dx, y0), self) if piece == chess.King: p.on_die = lambda who=who: self.king_captured(who) chess.Pawn(who, (x + dx, y1), self) for x in self.on_init: x()
def test_black_pawn_can_move_forward_by_one_or_two(self): board = chess.Board() pawn = chess.Pawn('a', 7, chess.BLACK, board) board.set_piece(pawn) self.assertEqual([('a', 6), ('a', 5)], pawn.valid_moves())
def test_white_pawn_can_move_forward_by_one_or_two(self): board = chess.Board() board.init_pieces() pawn = chess.Pawn('a', 2, chess.WHITE, board) board.set_piece(pawn) self.assertEqual([('a', 3), ('a', 4)], pawn.valid_moves())