def play_game(): """Launch Chessington!""" window = tk.Tk() window.title('Chessington') window.resizable(False, False) board = Board.at_starting_position() from_square = None to_squares = [] def generate_click_handler(clicked_square: Square): def handle_click(): nonlocal window, board, from_square, to_squares clicked_piece = board.get_piece(clicked_square) # If making an allowed move, then make it if from_square is not None and clicked_square in to_squares: board.get_piece(from_square).move_to(board, clicked_square) from_square, to_squares = None, [] # If clicking on a piece whose turn it is, get its allowed moves elif clicked_piece is not None and clicked_piece.player == board.current_player: from_square = clicked_square to_squares = clicked_piece.get_available_moves(board) # Otherwise reset everthing to default else: from_square, to_squares = None, [] update_pieces_and_colours(window, board) highlight_squares(window, board, from_square, to_squares) return handle_click # Create the board for row in range(BOARD_SIZE): for col in range(BOARD_SIZE): square = Square(row, col) frame = tk.Frame(window, width=WINDOW_SIZE, height=WINDOW_SIZE, name=square_id(square)) frame.grid_propagate(False) # disables resizing of frame frame.columnconfigure(0, weight=1) # enables button to fill frame frame.rowconfigure(0, weight=1) # any positive number is OK # The board has (0, 0) in the bottom left. # TKinter Grid has (0, 0) in the top left. # Map between these: gui_row = BOARD_SIZE - square.row - 1 frame.grid(row=gui_row, column=square.col) btn = tk.Button(frame, command=generate_click_handler(square), name='button') btn.grid(sticky='wens') update_pieces_and_colours(window, board) window.mainloop()
def test_new_board_has_white_pieces_at_bottom(): # Arrange board = Board.at_starting_position() # Act piece = board.get_piece(Square.at(0, 0)) # Assert assert piece.player == Player.WHITE
def test_new_board_has_black_pieces_at_top(): # Arrange board = Board.at_starting_position() # Act piece = board.get_piece(Square.at(7, 0)) # Assert assert piece.player == Player.BLACK
def test_pieces_can_be_moved_on_the_board(): # Arrange board = Board.at_starting_position() from_square = Square.at(1, 0) piece = board.get_piece(from_square) # Act to_square = Square.at(3, 0) board.move_piece(from_square, to_square) assert board.get_piece(from_square) is None assert board.get_piece(to_square) is piece
def play_game(): psg.ChangeLookAndFeel('GreenTan') board = Board.at_starting_position() board_layout = render_board(board) window = psg.Window('Chessington', default_button_element_size=(12, 1), auto_size_buttons=False).Layout(board_layout) from_square = None to_squares = [] def handle_click(row, col): nonlocal window, board, from_square, to_squares clicked_piece = board.get_piece(Square.at(row, col)) # if board.current_player == Player.BLACK: # board.move_piece(bot_move_from_to[0], bot_move_from_to[1]) # If making an allowed move, then make it if from_square is not None and any(s.row == row and s.col == col for s in to_squares): board.get_piece(from_square).move_to(board, Square.at(row, col)) from_square, to_squares = None, [] # If clicking on a piece whose turn it is, get its allowed moves elif clicked_piece is not None and clicked_piece.player == board.current_player: from_square = Square.at(row, col) to_squares = clicked_piece.get_available_moves(board) # Otherwise reset everthing to default else: from_square, to_squares = None, [] while True: # Check for a square being clicked on and react appropriately button, _ = window.Read() if button is not None: handle_click(*button) # Update the UI highlight_squares(window, from_square, to_squares) # if board.current_player == Player.BLACK: # bot_move_from_to = board.get_bot_move_squares() # piece_moves = board.get_piece(bot_move_from_to[0]).get_available_moves(board) # highlight_squares(window, bot_move_from_to[0], piece_moves) square_in_check = board.in_check() if square_in_check is not None: set_square_colour(window, square_in_check, CHECK_SQUARE_COLOUR) update_pieces(window, board)
def play_game(): psg.ChangeLookAndFeel('GreenTan') board = Board.at_starting_position() board_layout = render_board(board) window = psg.Window('Chessington', default_button_element_size=(12, 1), auto_size_buttons=False).Layout(board_layout) from_square = None to_squares = [] def handle_click(row, col): nonlocal window, board, from_square, to_squares clicked_piece = board.get_piece(Square.at(row, col)) # If making an allowed move, then make it if from_square is not None and any(s.row == row and s.col == col for s in to_squares): board.get_piece(from_square).move_to(board, Square.at(row, col)) from_square, to_squares = None, [] # If clicking on a piece whose turn it is, get its allowed moves elif clicked_piece is not None and clicked_piece.player == board.current_player: from_square = Square.at(row, col) to_squares = clicked_piece.get_available_moves(board) # Otherwise reset everthing to default else: from_square, to_squares = None, [] # def activate_bot(window, board, , ): # move = ChessBotStronk().do_smart_move(board) # handle_click(*move[0]) # highlight_squares(window, from_square, to_squares) # update_pieces(window, board) # # button, _ = window.Read() # if button is not None: # handle_click(*move[1]) # highlight_squares(window, from_square, to_squares) # update_pieces(window, board) button, _ = window.Read() while True: counter = 0 if board.current_player == Player.WHITE: ''' Bot code ''' player = Player.WHITE opponent = Player.BLACK move = ChessBotStronk(player, opponent).get_move(board) if move is not None: handle_click(*move[0]) highlight_squares(window, from_square, to_squares) button, _ = window.Read(timeout=40) # if button is not None: handle_click(*move[1]) highlight_squares(window, from_square, to_squares) update_pieces(window, board) else: button, _ = window.Read() ''' Player code ''' # button, _ = window.Read() # if button is not None: # handle_click(*button) # highlight_squares(window, from_square, to_squares) # update_pieces(window, board) if board.current_player == Player.BLACK: """ Bot code """ player = Player.BLACK opponent = Player.WHITE move = NuChessBotStronk(player, opponent).get_move(board) if move is not None: handle_click(*move[0]) highlight_squares(window, from_square, to_squares) button, _ = window.Read(timeout=40) # if button is not None: handle_click(*move[1]) highlight_squares(window, from_square, to_squares) update_pieces(window, board) else: button, _ = window.Read() # Update the UI highlight_squares(window, from_square, to_squares) update_pieces(window, board)