def print_board(board):
    """Prints the 6x7 board with column headers."""
    print(" 1 2 3 4 5 6 7")
    for r in range(6):
        print("|" + "|".join(board[r]) + "|")
    print("---------------")

def get_valid_move(board, player):
    """Prompts user for a column 1-7 and validates the move."""
    while True:
        try:
            col = int(input(f"Player {player}, enter column (1-7): "))
            if 1 <= col <= 7:
                # Check if the top cell of the column is empty
                if board[0][col - 1] == ' ':
                    return col - 1
                else:
                    print("Column is full. Try again.")
            else:
                print("Column must be between 1 and 7.")
        except ValueError:
            print("Invalid input. Please enter a number.")

def drop_piece(board, col, piece):
    """Drops a piece into the lowest empty row of the specified column."""
    for r in range(5, -1, -1):
        if board[r][col] == ' ':
            board[r][col] = piece
            return

def check_win(board, piece):
    """Checks if the specified piece has four in a row."""
    # Horizontal
    for r in range(6):
        for c in range(4):
            if all(board[r][c + i] == piece for i in range(4)):
                return True
    # Vertical
    for r in range(3):
        for c in range(7):
            if all(board[r + i][c] == piece for i in range(4)):
                return True
    # Diagonal (down-right)
    for r in range(3):
        for c in range(4):
            if all(board[r + i][c + i] == piece for i in range(4)):
                return True
    # Diagonal (up-right)
    for r in range(3, 6):
        for c in range(4):
            if all(board[r - i][c + i] == piece for i in range(4)):
                return True
    return False

def is_draw(board):
    """Checks if the board is full."""
    return all(board[0][c] != ' ' for c in range(7))

def main():
    # Initialize 6x7 board with empty spaces
    board = [[' ' for _ in range(7)] for _ in range(6)]
    players = ['R', 'Y']
    turn = 0

    while True:
        print_board(board)
        player = players[turn % 2]
        
        col = get_valid_move(board, player)
        drop_piece(board, col, player)

        if check_win(board, player):
            print_board(board)
            print(f"Player {player} wins!")
            break

        if is_draw(board):
            print_board(board)
            print("It's a draw!")
            break

        turn += 1

if __name__ == "__main__":
    main()
